问题
I would like to filter my mat-table result by column using the predicate filter. Im already using a simple filter but it filtering all the data in all the column. I search for similar topics but i don't know how use it. I try to repeat my code for all the column but doesn't work..
See code below :
<mat-form-field>
<input matInput (keyup)="applyFilter($event.target.value)" placeholder="Filter">
</mat-form-field>
<div class="example-container mat-elevation-z8">
<div class="example-header">
<div>
<mat-table [dataSource]="dataSource" matSort>
<ng-container matColumnDef="aa">
<mat-header-cell *matHeaderCellDef mat-sort-header>aa title</mat-header-cell>
<mat-cell *matCellDef="let user"> {{user.aa}} </mat-cell>
</ng-container>
<ng-container matColumnDef="bb">
<mat-header-cell *matHeaderCellDef mat-sort-header> bb tilte </mat-header-cell>
<mat-cell *matCellDef="let user"> {{user.bb}} </mat-cell>
</ng-container>
<mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
<mat-row *matRowDef="let row; columns: displayedColumns;"></mat-row>
</mat-table>
<mat-paginator [pageSizeOptions]="[5, 10, 25, 100]"></mat-paginator>
</div>
</div>
</div>
and my .ts :
export class MynewComponent implements OnInit {
dataSource = new MatTableDataSource();
displayedColumns = ['aa', 'bb'];
@ViewChild(MatPaginator) paginator: MatPaginator;
@ViewChild(MatSort) sort: MatSort;
constructor(private materialService: MaterialService) {}
ngOnInit() {
this.materialService.getUser().subscribe(value =>{this.dataSource.data = value;});
this.dataSource.paginator = this.paginator;
this.dataSource.sort = this.sort;
}
applyFilter(filterValue: string) {
filterValue = filterValue.trim(); // Remove whitespace
filterValue = filterValue.toLowerCase(); // Datasource defaults to lowercase matches
this.dataSource.filter = filterValue;
if (this.dataSource.paginator) {
this.dataSource.paginator.firstPage();
}
}
}
export class UserDataSource extends DataSource<any> {
constructor(private materialService: MaterialService) {
super();
}
connect(): Observable<MaterialModel[]> {
return this.materialService.getUser();
}
disconnect() {}
}
回答1:
HTML Add 7 diff input fields also in applyFilter
pass input value & a key of that column as string
<mat-form-field>
<input matInput (keyup)="applyFilter($event.target.value, 'aa')" placeholder="Filter">
</mat-form-field>
<mat-form-field>
<input matInput (keyup)="applyFilter($event.target.value, 'bb')" placeholder="Filter">
</mat-form-field>
<mat-form-field>
<input matInput (keyup)="applyFilter($event.target.value, 'col3')" placeholder="Filter">
</mat-form-field>
<mat-form-field>
<input matInput (keyup)="applyFilter($event.target.value, 'col4')" placeholder="Filter">
</mat-form-field>
<mat-form-field>
<input matInput (keyup)="applyFilter($event.target.value, 'col5')" placeholder="Filter">
</mat-form-field>
<mat-form-field>
<input matInput (keyup)="applyFilter($event.target.value, 'col6')" placeholder="Filter">
</mat-form-field>
<mat-form-field>
<input matInput (keyup)="applyFilter($event.target.value, 'col7')" placeholder="Filter">
</mat-form-field>
You can find other ways of writing HTML according to your requirement of design.
component
Create filter obj having key
: Column key & value
: filter value
filterObj = {};
applyFilter(filterValue: string, key: string) {
this.filterObj = {
value: filterValue.trim().toLowerCase(),
key: key
}
this.dataSource.filter = filterValue;
if (this.dataSource.paginator) {
this.dataSource.paginator.firstPage();
}
}
update filterPredicate
as:
this.dataSource.filterPredicate = (data, filter) => {
if(data[this.filterObj['key']] && this.filterObj['key']) {
return data[this.filterObj['key']].toLowerCase().includes(this.filterObj['value']);
}
return false;
}
来源:https://stackoverflow.com/questions/50639743/filterpredicate-table-by-colomn-with-angular-material