无法在Angular 6的ng2-smart-table中获取删除事件

时间:2018-09-06 11:35:42

标签: angular delete-row ng2-smart-table

我正在使用ng2-smart-table在angular 6应用中显示数据。现在,我无法获取删除事件。我已经设置了这样的设置对象。

columns: {
  ... // Columns... 
},
edit: {
  confirmSave: true,
  editButtonContent: '...',
  saveButtonContent: '...',
  cancelButtonContent: '...'
},  
delete: {
   confirmDelete : true,
   deleteButtonContent: '...',
   saveButtonContent: '...',
   cancelButtonContent: '...'
},

在HTML文件中,我对此进行了设置。

<ng2-smart-table [settings]="Settings" [source]="DataSource" (createConfirm)="onCreate($event)" (confirmDelete)="onRoleDelete($event)"></ng2-smart-table>

在.ts文件中

onRoleDelete(event) {
   alert("Delete works");
}

我检查了它的git并也参考了其他示例。我也能够获得编辑事件,但不能仅获得删除事件。

删除图标也在HTML文件中查看。

1 个答案:

答案 0 :(得分:1)

尝试使用deleteConfirm代替confirmDelete

  

请参考Demo以获得更好的理解

DEMO

删除mode: 'external',

<ng2-smart-table [settings]="settings" [source]="DataSource" (createConfirm)="onCreate($event)" (deleteConfirm)="onRoleDelete($event)"></ng2-smart-table>

配置:

  settings = {
    delete: {
      confirmDelete: true,
    },
    add: {
      confirmCreate: true,
    },
    edit: {
      confirmSave: true,
    },
    columns: {
      id: {
        title: 'ID',
      },
      name: {
        title: 'Full Name',
      },
      username: {
        title: 'User Name',
      },
      email: {
        title: 'Email',
      },
    },
  };

  data = [
    {
      id: 1,
      name: "Leanne Graham",
      username: "Bret",
      email: "Sincere@april.biz"
    },
    {
      id: 2,
      name: "Ervin Howell",
      username: "Antonette",
      email: "Shanna@melissa.tv"
    },

    // ... list of items

    {
      id: 11,
      name: "Nicholas DuBuque",
      username: "Nicholas.Stanton",
      email: "Rey.Padberg@rosamond.biz"
    }
  ];

  onDeleteConfirm(event) {
    console.log("Delete Event In Console")
    console.log(event);
    if (window.confirm('Are you sure you want to delete?')) {
      event.confirm.resolve();
    } else {
      event.confirm.reject();
    }
  }

  onCreateConfirm(event) {
    console.log("Create Event In Console")
    console.log(event);

  }

  onSaveConfirm(event) {
    console.log("Edit Event In Console")
    console.log(event);
  }

参考---> https://github.com/akveo/ng2-smart-table/blob/master/src/app/pages/examples/various/advanced-example-confirm.component.ts