我无法刷新Mat表的数据源。我已经这样声明了:
dataSource: MatTableDataSource<Custom_Data> = new MatTableDataSource();
其中Custom_Data
是我使用自定义字段创建的接口。在ngOnInit中,我从服务器获取数据并执行以下操作:
this.networkService.getCapitoliSelectItem(this.idContesto).subscribe(d => {
this.tableData= d;
this.tableData.forEach(c => {
//manipulate data
});
this.dataSource = new MatTableDataSource(this.tableData);
//put here to trigger detection of changes, not workin
this.changeDetector.detectChanges();
}
该表未显示任何数据。然后,我有了一个过滤器方法,因为在表的标题中,我有两个输入字段,用于过滤表中的数据。在这种情况下,如果我写一些东西,则将调用方法过滤器,并且我的数据将显示在表中,并且一切正常。
过滤方法为:
applyFilter(filterValue: string, column: string) {
filterValue = filterValue.trim(); // Remove whitespace
filterValue = filterValue.toLowerCase(); // MatTableDataSource defaults to lowercase matches
if(Number(filterValue.length) != 0){
this.dataSource.data = this.tableData.filter(d => d[column].toLowerCase().indexOf(filterValue)>-1);
} else {
this.dataSource.data = this.tableData;
}
}
为什么这样做有效,而不是ngOnInit方法?我还尝试了this.dataSource.data = this.tableData
而不是this.dataSource = new MatTableDataSource(this.tableData);
,但是没有用。