custom filter in mat-table

后端 未结 4 800
深忆病人
深忆病人 2020-11-29 01:54

I\'m using mat-table. It has a filter which works fine with doc example:

From https://material.angular.io/components/table/overview, the original code is:



        
4条回答
  •  再見小時候
    2020-11-29 02:43

    I found the solution here.

    It's necessary to rewrite filterPredicate, and just use it as usual, filterPredicate needs to return true when filter passes and false when it doesn't

    export interface Element {
     name: string;
     position: number;
     weight: number;
     symbol: string;
    }
    
    
    dataSource = new MatTableDataSource(ELEMENT_DATA);
    /* configure filter */
    this.dataSource.filterPredicate = 
      (data: Element, filter: string) => data.name.indexOf(filter) != -1;
    
    
    applyFilter(filterValue: string) {
       filterValue = filterValue.trim(); // Remove whitespace
       filterValue = filterValue.toLowerCase(); // MatTableDataSource defaults to lowercase matches
       this.dataSource.filter = filterValue;
     }
    

提交回复
热议问题