【问题标题】:Update the Mat Dialog data on deletion Angular Material在删除 Angular Material 时更新 Mat Dialog 数据
【发布时间】:2020-11-12 11:41:33
【问题描述】:

我已经实现了一个垫子对话表。它有 3 列 name, email and delete icon 当我单击删除时,它会确认我们是否要删除,并且在删除时,它会从数据库中删除该项目。但我无法立即看到更改,我必须关闭对话框并再次打开它才能看到更改。如何立即更新更改。 TS 代码:

//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) 函数吗?我试过了,但由于它在父组件上,它给出了找不到它的错误。我该怎么做谢谢。

【问题讨论】:

    标签: angular typescript angular-material angular8


    【解决方案1】:

    在对话框关闭时将已删除的记录 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 })
        } });}}
    

    【讨论】:

    • 如果没有id怎么办?我实际上是通过emailc 值来检查要删除哪个用户。那我该如何完成呢?任何建议表示赞赏。
    • 几件事,我将如何向父组件发送数据?我的意思是我得到了 confirmed 的真实价值,然后我必须实施一些东西吗?
    • 一旦确认您可以将值(电子邮件或任何唯一值)返回给父级 ` this.dialogRef.close({ email}), if not confirmed you can simply close the DialogExample component by this.dialogRef.close({ email:null}) ` ,然后在 parent 中您可以过滤用户列表
    • 你能告诉我代码中的item是什么吗?
    • 我尝试实施更新后的解决方案,但它只是关闭了整个对话框,实际上并没有删除数据。我只想关闭确认对话框并更新 mat-dialog-table
    猜你喜欢
    • 1970-01-01
    • 2018-10-03
    • 2021-04-22
    • 2021-07-08
    • 2018-10-17
    • 1970-01-01
    • 1970-01-01
    • 2020-07-02
    • 2018-10-17
    相关资源
    最近更新 更多