【发布时间】:2021-08-08 13:11:14
【问题描述】:
列过滤器工作得很好,但默认过滤器(输入字段搜索)根本不起作用。我尝试了几件事,但没有任何结果。 我希望我的输入字段具有搜索两个列的默认行为,即 allAssociates 和 dept 列以及各个列过滤器。 它也不会抛出任何错误。 自定义列过滤器参考来自所以我没有在这里包括: https://www.freakyjolly.com/angular-material-table-custom-filter-using-select-box/#Adding_Filters_on_Table
filterTable 方法没有按预期工作 我会很感激任何建议..
完整代码:https://stackblitz.com/edit/angular-material-b6qdyt?file=app%2Fapp.component.html
分享了下面的sn-p:
HTML
<div>
<mat-form-field
*ngFor="let filter of filterSelectObj"
style="margin-left: 15px;">
<mat-label> Filter {{filter.name}} </mat-label>
<select
matNativeControl
name="{{filter.columnProp}}"
[(ngModel)]="filter.modelValue"
(change)="filterChange(filter,$event)">
<option value=""> All </option>
<option
[value]="item"
*ngFor="let item of filter.options">
{{item}}
</option>
</select>
</mat-form-field>
<mat-icon>search</mat-icon>
<input
type="text"
placeholder="All Associates/Dept"
#input
(keyUp)="filterTable($event.target.value)">
<button
mat-stroked-button
color="warn"
(click)="resetFilters(input)">
Reset
</button>
</div>
TS 文件:
ngOnInit() {
//fetch data from Service
const deptData = this.deptService.getData();
this.dataSource = new MatTableDataSource(deptData);
this.dataSource.filterPredicate = this.createFilter();
this.filterSelectObj.filter((o) => {
o.options = this.getFilterObject(deptData, o.columnProp);
});
}
filterTable(input: string) {
this.dataSource.filterPredicate =
(data: DepartmentData, f: string) => !f || (data.allAssociates || data.dept) == f;
input = input.trim(); // Remove whitespace
input = input.toLowerCase(); // MatTableDataSource defaults to lowercase matches
this.dataSource.filter = input;
}
【问题讨论】:
-
您使用的是哪个版本的 Angular 和 Angular Material?
-
材料 - 11.2.12 和 Angular - 11.2.13
-
您提供的 stackblitz 运行 Angular V4 和 Material v2 ????????♂️
标签: angular angular-material angular-filters angular-material-table