3

我一直在寻找 mat 表中列上的过滤器的任何示例,但到目前为止还没有找到任何示例。我确实提供了一些有关方法的零碎信息,filterPredicate但没有完全了解如何处理它。

我的需要是单个列应该有一个过滤器,比如一个文本框或一个复选框,基于该过滤器dataSource将被过滤。

堆栈闪电战

4

1 回答 1

4
displayedColumns: string[] = ['position', 'name', 'weight', 'symbol'];
dataSource = new MatTableDataSource(ELEMENT_DATA);

applyFilter(filterValue: string) {
this.dataSource.filter = filterValue.trim().toLowerCase();
this.dataSource.filterPredicate = (data: Element, filter: string) => 
data.name.toLowerCase().indexOf(filter) != -1;
}

export interface Element {
name: string;
position: number;
weight: number;
symbol: string;
}

const ELEMENT_DATA: Element[] = [
{position: 1, name: 'hydrogen', weight: 1.0079, symbol: 'Hi up'},
{position: 2, name: 'Helium', weight: 4.0026, symbol: 'Hi upp'},
{position: 3, name: 'Lithium', weight: 6.941, symbol: 'Li'},
{position: 4, name: 'Beryllium', weight: 9.0122, symbol: 'Be'},
{position: 5, name: 'Boron', weight: 10.811, symbol: 'B'},
{position: 6, name: 'Carbon', weight: 12.0107, symbol: 'C'},
{position: 7, name: 'Nitrogen', weight: 14.0067, symbol: 'N'},
{position: 8, name: 'Oxygen', weight: 15.9994, symbol: 'O'},
{position: 9, name: 'Fluorine', weight: 18.9984, symbol: 'F'},
{position: 10, name: 'Neon', weight: 20.1797, symbol: 'Ne'},
];

// html

 <mat-form-field>
  <input matInput (keyup)="applyFilter($event.target.value)" placeholder="Filter">
</mat-form-field>

我只过滤了名称字段。这样你可以在任何列中过滤

于 2018-06-27T08:39:12.950 回答