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();
  }
Ivan Vilch
  • 86
  • 1
  • 4

2 Answers2

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.

Ivan Vilch
  • 86
  • 1
  • 4
0

I guess that you are looking for that:

 applyFilter(filterValue: string) {

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

 }
Ztemps
  • 196
  • 4
  • 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 – Ivan Vilch Apr 23 '19 at 15:24
  • Then you need to improve the if conditions no? if (!!filterValue && filterValue !== 'undefined') that, takes into account if the user write undefined as string. – Ztemps Apr 23 '19 at 15:32
  • 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 – Ivan Vilch Apr 23 '19 at 15:51