Filtering specific column in Angular Material Table with filtering in angular?

前端 未结 4 873
温柔的废话
温柔的废话 2021-02-19 00:44

I am using mat-table. It has a filter which works fine.

Filter happened against this below data (All columns)

const ELEMENT_DATA: Element[] = [
  {posit         


        
4条回答
  •  我寻月下人不归
    2021-02-19 01:28

    Material has implemented its own search feature you can override it by updating the filterPredicate property on the date source. Call the generateTable function on ngOninit() life cycle.

    generateTable(tableData: any) {
        this.dataSource = new MatTableDataSource(tableData);
        this.dataSource.paginator = this.paginator;
        this.dataSource.sort = this.sort;
        this.dataSource.filterPredicate = (data: any, filter: string) => {
          console.log(data);
          console.log(filter);
          let matchFound = false;
          for (let column of this.displayedColumns) {
            if(column in data) {
              if(data[column]) {
                matchFound = (matchFound || data[column].toString().trim().toLowerCase().indexOf(filter.trim().toLowerCase()) !== -1)
              }
            }
          }
          return matchFound;
        }
      }
    

    Whenever you wish to apply the filter, update the filter property on dataSource to the string value to be searched.

    applyFilter(searchValue: any) {
        this.dataSource.filter = this.filterText.trim().toLowerCase();
        }
    

提交回复
热议问题