angular - 在删除 Angular Material 时更新 Mat Dialog 数据
问题描述
我已经实现了一个垫子对话表。它有 3 列当name, email and delete icon
我单击删除时,它会确认我们是否要删除,并且在删除时,它会从数据库中删除该项目。但我无法立即看到更改,我必须关闭对话框并再次打开才能看到更改。如何立即更新更改。交易代码:
//parent component has a master table. On click of name we open mat table with nested user list to delete from
delete(name){
//backend logic
if(response.status === 400){
//show error message
} else {
//open dialog table
this.openDialog(userData);
}} })}
openDialog(us, c){
// some logic
const dialogRef = this.dialog.open(DialogExample, {
width: '700px',
height: '500px',
data: {arrayDialog: this.allUsers, c}
});
dialogRef.afterClosed().subscribe(result => {
console.log('the dialog was closed');
this.allUsers = [];
});}}
//child component to open mat dialog table
@Component({
selector: 'dialog--example',
templateUrl: 'dialog.html',
})
export class DialogExample {
readonly userColumns: string[] = ['name', 'email', 'delete'];
delete(email, uName){
//some logic
confirmationDialog(e,c) {
const dialogRef = this.dialog.open(ConfirmationDialog,{
data:{
message: 'Please confirm the deletion',
buttonText: {
ok: 'Delete',
cancel: 'Cancel'
}} });
dialogRef.afterClosed().subscribe((confirmed: boolean) => {
if (confirmed) {
//After closing the confirmation dialog, here I want to update the mat dialog table to show updated data excluding the deleted user details.
} });}}
@Component({
selector: 'deleteconfirm',
templateUrl: 'deleteconfirm.html',
})
export class ConfirmationDialog {
returns true if confirmed
returns false if cancelled
我该如何更新它?我应该调用delete(name)
函数吗?我试过了,但是因为它在父组件上,所以它给出了找不到它的错误。我该如何进行谢谢。
解决方案
在对话框关闭时将已删除的记录 ID 返回给父级,并使用已删除的记录 ID 过滤数据
父组件.ts
openDialog(us, c){
// some logic
const dialogRef = this.dialog.open(DialogExample, {
width: '700px',
height: '500px',
data: {arrayDialog: this.allUsers, c}
});
dialogRef.afterClosed().subscribe(result => {
console.log('the dialog was closed');
this.allUsers =this.allUsers.filter(item=>item.id!==result.id);
});
}}
dialogExample.component.ts
export class DialogExample {
constructor(private dialogExampleRef: MatDialogRef<DialogExample>) { }
readonly userColumns: string[] = ['name', 'email', 'delete'];
delete(email, uName){
//some logic
confirmationDialog(e,c) {
const dialogRef = this.dialog.open(ConfirmationDialog,{
data:{
message: 'Please confirm the deletion',
buttonText: {
ok: 'Delete',
cancel: 'Cancel'
}} });
dialogRef.afterClosed().subscribe((confirmed: boolean) => {
if (confirmed) {
//After closing the confirmation dialog, here I want to update the mat dialog table to show updated data excluding the deleted user details.
this.dialogExampleRef.close({ e })
} });}}
推荐阅读
- javascript - chrome上的getDisplayMedia拆分选择窗口
- rust - Rust 链表不能借用上一个和下一个元素作为可变的(只需要不可变的引用)
- java - 引起:java.sql.SQLException:ORA-06550:第 1 行,第 7 列:PLS-00306:调用“PR_SP_FAHMI”时参数的数量或类型错误
- javascript - window.moveTo() 在 Firefox 中不会超过我屏幕的一半
- python-3.x - Python 3 中有异步抽象基类吗?
- ios - 如何在 StackView 中停止排列的子视图剪辑
- xamarin - 如何导航离开 MasterDetailPage 但不重置导航堆栈
- angular - 错误:AADSTS50058:已发送静默登录请求,但没有用户登录 - 移动 safari 浏览器中的 Angular ADAL 身份验证问题
- data-mining - ECLAT算法找到最大和闭合频繁集
- html - 响应式视频部分都具有 100vh 和 100vw 而不使用位置:固定