如何 select 带有复选框的 ng2-smart-table 组件中的多行?

How to select multiple rows in ng2-smart-table component with checkbox?

我正在使用 ng2-smart-table 来自 https://akveo.github.io/ng2-smart-table/#/documentation

现场演示: http://akveo.com/ngx-admin/pages/tables/smart-table

请帮我解决以下问题:

  1. 我想 select 多行并调用一个函数所以我需要在 ng2-smart-table?

  2. 我可以 select 多行并在 select 行上调用一个函数吗?

我在 .ts 和 .html 文件中编写了以下代码:

聪明-table-component.ts:

 actions: {
      add: false,
      edit: true,
      delete: false,
      custom: [{ name: 'ourCustomAction'}],
      position: 'left',
      columnTitle: 'Actions'

    },   

    mode: 'external',

聪明-table-component.html:

   <nb-card-body>
    <ng2-smart-table [settings]="settings" allowFiltering='true' allowPaging='true' [source]="windchillData"
     (deleteConfirm)="onDeleteConfirm($event)" (custom)="onCustomAction($event)" (edit)="onEdit($event)">
    </ng2-smart-table>
  </nb-card-body>

我不太熟悉这个库,但以下内容应该有所帮助:

html

<button (click)="logAllSelectedRows()">log All selected</button>
<ng2-smart-table [settings]="settings" [source]="source" (deleteConfirm)="onDeleteConfirm($event)" (rowSelect)="rowSelectedHandler($event)">

ts

  selectedRows = [];
  rowSelectedHandler(rowData:{isSelected:boolean, data:any}){
    if(rowData.isSelected === false){
      /*remove row*/
      this.selectedRows = this.selectedRows.filter((rowItem)=>rowItem .id !== rowData.data.id)
    }else {
      /*add row*/
      this.selectedRows = [...this.selectedRows, ...rowData.data];
      console.log('added rowdata');
    }
  }

  logAllSelectedRows(){
      console.log(this.selectedRows);
  }

在你的 ng2-smart-table settings ([settings]="settings") 中,添加这一行以启用多行选择:

selectMode: 'multi',

然后,在您的 模板 中,将 (userRowSelect) 事件添加到您的 ng2-smart-table:

<ng2-smart-table [settings]="settings" allowFiltering='true' allowPaging='true' [source]="windchillData" (deleteConfirm)="onDeleteConfirm($event)" (custom)="onCustomAction($event)" (edit)="onEdit($event)" (userRowSelect)="onUserRowSelect($event)"></ng2-smart-table>

在您的组件中,在每个 (userRowSelect):

处更新选定行的列表
private selectedRows: any;

// ...

onUserRowSelect(event) {
    this.selectedRows = event.selected;
}

仍然在组件中,添加一个函数来对选定的行执行您想要的操作:

public doSomething() {
    // Do something with `this.selectedRows`
}

添加一个按钮,当它被点击时调用你的函数!

<button (click)="doSomething()"> Run code on selected rows! </button>

1- 我想 select 多行并调用一个函数所以我需要在 ng2-smart-table? 中编写这段代码

答案:

对于 select 在 ng2-smart-table 中设置多行,您需要在 settings 对象中添加配置。

示例:

settings = {
    // This `selectMode` is the configuration for selecting multiple rows in the table using checkbox
    selectMode: 'multi',
    delete: {
      confirmDelete: true,

      deleteButtonContent: 'Delete data',
      saveButtonContent: 'save',
      cancelButtonContent: 'cancel'
    },
    add: {
      confirmCreate: true,
    },
    edit: {
      confirmSave: true,
    },
    columns: {
      // columns configuration
    },
  };

2- 我可以 select 多行并在 select 行上调用一个函数吗?

ng2-smart-table 有一个事件要获取 userSelectedRows,我们可以使用该事件获取所有 selected 行,然后调用一个函数对所有 select编辑行。

示例:

  • 第一步:在模板中添加事件处理器
<ng2-smart-table [settings]="settings" allowFiltering='true' allowPaging='true' [source]="windchillData" (deleteConfirm)="onDeleteConfirm($event)" (custom)="onCustomAction($event)" (edit)="onEdit($event)" (userRowSelect)="onUserRowSelect($event)"></ng2-smart-table> 
  • 第 2 步:在 component.ts 中创建事件处理程序以获取 selected 行
onUserRowSelect(event) {
    this.selectedRows = event.selected;
}
  • 第 3 步:创建按钮并调用一个函数来处理 selected 行

按钮在 html

<button (click)="onClick()"> Get Selected </button>

component.ts

中的点击处理程序
onClick() {
    // It will console all the selected rows
    console.log(this.selectedRows);
  }

在这里你可以看到这个在工作:https://stackblitz.com/edit/example-ng2-smart-table-18qsws