3 回答
TA贡献2019条经验 获得超9个赞
有一个在线示例,说明如何刷新垫桌的视图。Mat Table 本身并不知道数据源是否已更改,因此您基本上必须触发更改检测。下面的示例直接来自示例项目....
export interface Element {
name: string;
position: number;
weight: number;
symbol: string;
}
const ELEMENT_DATA: Element[] = [
{position: 1, name: 'Hydrogen', weight: 1.0079, symbol: 'H'},
{position: 2, name: 'Helium', weight: 4.0026, symbol: 'He'},
{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'}
];
只是一个正确定义的接口和示例数据集
// Displayed Columns, not important for the example
displayedColumns = ['position', 'name', 'weight', 'symbol'];
// We define the initial datasource
dataSource = new MatTableDataSource(ELEMENT_DATA);
那么他们在示例中所做的,基本上是刷新数组的引用...
addElement() {
// With the array function push() we make it possible for the Mat-Tables to refresh the view
ELEMENT_DATA.push({position: 1, name: 'Hydrogen', weight: 1.0079, symbol: 'H'})
this.dataSource = new MatTableDataSource(ELEMENT_DATA);
}
TA贡献1780条经验 获得超4个赞
将数据检索代码放入单独的方法中,并在 POST 订阅中重用它。
onSubmit() {
if (this.formdata.invalid) {
return;
}
this.adminCategory.createExamCategory(this.formdata.value).subscribe(reponse => {
console.log(reponse.message);
this.getData();
});
}
ngOnInit() {
this.columnsToDisplay = ['id', 'examCategoryName', 'isActive', 'Action'];
this.getData();
}
private getData() {
this.adminCategory.getExamCategory().subscribe(
(reponse: any) => {
this.ExamCategoryData = reponse.examCategoryList;
this.dataSource = new MatTableDataSource(this.ExamCategoryData);
this.dataSource.paginator = this.paginator;
},
error => {
console.log(error);
}
);
}
添加回答
举报