3 回答

TA貢獻1854條經(jīng)驗 獲得超8個贊
您有兩種選擇:
將發(fā)送到 POST 請求的數(shù)據(jù)直接添加到表中。
在 POST 刷新表后獲取整個數(shù)據(jù)集。

TA貢獻2019條經(jīng)驗 獲得超9個贊
有一個在線示例,說明如何刷新墊桌的視圖。Mat Table 本身并不知道數(shù)據(jù)源是否已更改,因此您基本上必須觸發(fā)更改檢測。下面的示例直接來自示例項目....
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'}
];
只是一個正確定義的接口和示例數(shù)據(jù)集
// Displayed Columns, not important for the example
displayedColumns = ['position', 'name', 'weight', 'symbol'];
// We define the initial datasource
dataSource = new MatTableDataSource(ELEMENT_DATA);
那么他們在示例中所做的,基本上是刷新數(shù)組的引用...
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條經(jīng)驗 獲得超4個贊
將數(shù)據(jù)檢索代碼放入單獨的方法中,并在 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);
}
);
}
添加回答
舉報