angular - Angular Material Dialog:如何在 afterClosed 更改后更新表格行中的数据?
问题描述
关闭表中的模式对话框后不更新行。行中this.usersData
已更新,但表中未更新。我如何更新行?
usersData: MatTableDataSource<any>;
listUsers() {
this.usersService.usersList(limit, page, param)
.subscribe((res) => {
this.usersData = new MatTableDataSource(res.rows);
},
(err) => {
console.log('listUsers_err', err);
}
);
}
editUser(id, i) {
const dialogConfig = {
disableClose: true,
autoFocus: false,
data: {
userId: id
}
};
const dialogRef = this.dialog.open(UserEditComponent, dialogConfig);
dialogRef.afterClosed().subscribe(result => {
this.usersData.data[i] = result; // <--- not updated in table aftrer close modal
});
}
"@angular/material": "^11.0.3",
解决方案
如果提供了数据数组,则必须在添加、删除或移动数组的对象时通知表。这可以通过调用renderRows()
将呈现自上次表呈现以来的差异的函数来完成。如果数据数组引用发生变化,表会自动触发对行的更新,表api...
export class Component {
@ViewChild(MatTable) table: MatTable<any>;
...
editUser(id, i) {
...
dialogRef.afterClosed().subscribe(result => {
this.usersData.data[i] = result; // <--- not updated in table aftrer close modal
this.table.renderRows();
});
}
}
推荐阅读
- google-play-services - 禁用 Cast SDK 数据分析
- openldap - 使用 OpenLDAP 的 Freeradius3 身份验证
- javascript - 在javascript中每秒从txt加载行到MYSQL
- sql - 用于获取特定模式的 SQL 命令
- testing - 这个问题有什么解决方案吗..我想通过使用 appium 在 testng.xml 中运行多个类以实现移动自动化
- regex - Sublime Text 3:使用简单的正则表达式创建片段
- scala - 在 Scala 中使用 QueryParamDecoder 解码可选的查询参数
- python - 在 Kivy 应用程序启动时满足特定条件时如何自动弹出警报
- arrays - 如何从 JSON 数组中返回特定范围的索引?
- python - ValueError:要解包的值太多(预期为 2):pandas