0

please help. Is there any way I can exclude the undefined and null from filtering? So, if the cell value is null or undefined it's not being shown when the user types "null" or "undefined" in search input.

Incoming table data:

dataSource: MatTableDataSource

The below method is applied on input:

  applyFilter(filterValue: string) {
    this.dataSource.filter = filterValue.trim().toLowerCase();
  }

3 Answers 3

1

I found the answer, in case anybody is looking for it.

  applyFilter(filterValue: string) {
    this.dataSource.data.forEach(element => {
      for (const key in element) {
        if (!element[key] || element[key] === null) {
          element[key] = '';
        }
      }
    });
    this.dataSource.filter = filterValue.trim().toLowerCase();
  }

applyFilter() function is added to input, and it takes input value as argument. On filtering you need to check the incoming data array of objects (it will be your rows in Material Table), and for each object property check if it is null or undefined. If yes - assign empty string. This empty string will be then concatinated together with other values by material for filtering.

Sign up to request clarification or add additional context in comments.

1 Comment

While an acceptable solution, I believe a better approach is using the filterPredicate.
0

I guess that you are looking for that:

 applyFilter(filterValue: string) {

    if (!!filterValue) {
       this.dataSource.filter = filterValue.trim().toLowerCase();
    }

 }

3 Comments

It checks whether user puts something in input, and I need other behavior. The cell in table has value undefined or null (in the input data object some keys does not have value or has the value but null). BUt material filter method takes undefined as string, and if user puts "undefined" in input it shows this cell. But it shouldn't, since in fact there is no value in the cell
Then you need to improve the if conditions no? if (!!filterValue && filterValue !== 'undefined') that, takes into account if the user write undefined as string.
user may put just "d" and since d is a part of string "undefined" it's being shown after filtering. I need to exclude the cell data from filtering, not work with filter(input string in fact), user should be able to put anything
0

My approach would be to use the filterPredicate and filter property, and then coalesce (i.e. use the ?? and '') the data/values so that undefined becomes the empty string you need to compare. For example:

// config filter via some form control:
this.filterInputFormControl.valueChanges.subscribe((searchString) => {
  this.filteredValues['col_A'] = searchString;
  this.filteredValues['col_B'] = searchString;
  this.dataSource.filter = JSON.stringify(this.filteredValues);
});

// filter predicate:
this.dataSource.filterPredicate = (data: any, filter: string): boolean => {
  const searchString = JSON.parse(filter);
  return (
    (data.col_B ?? '').toString().trim().toLowerCase().indexOf(searchString.col_B.toLowerCase()) !== -1 ||
    (data.col_A ?? '').toString().trim().toLowerCase().indexOf(searchString.col_A.toLowerCase()) !== -1
  );
};

1 Comment

I agree that filterPredicate will be the only correct usage :) no sense to overwrite the datasource.data, I was young :D But your answer is a bit confusing in some moments. I am not sure why do u need the stringify/parse, why do u bind to col_B/col_A, this is not extendable, not generic and cant be reused.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.