我已经实现了一个mat对话框表。它具有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)
函数。我试过了,但是因为它在父组件上,它给出了找不到它的错误。我应该如何进行感谢。
答案 0 :(得分:0)
在对话框关闭时将已删除的记录ID返回父级,并使用已删除的记录ID过滤数据
parent.component.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 })
} });}}