Search code examples
angulartypescriptng2-smart-table

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


I am using ng2-smart-table from https://akveo.github.io/ng2-smart-table/#/documentation

Live Demo: http://akveo.com/ngx-admin/pages/tables/smart-table

Please help me with below questions:

  1. I want to select multiple rows and call one function so where I need to write this code in ng2-smart-table?

  2. Can I select multiple rows and call one function on selected rows ?

I have written below code in .ts and .html files:

smart-table-component.ts:

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

    },   

    mode: 'external',

smart-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>

Solution

  • 1- I want to select multiple rows and call one function so where I need to write this code in ng2-smart-table?

    Answer:

    For selecting multiple rows in ng2-smart-table, you need to add configuration in your settings Object.

    Example:

    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- Can I select multiple rows and call one function on selected rows ?

    ng2-smart-table have an event to get userSelectedRows, we can use that event to get all the selected rows and then call a function to do something with all selected rows.

    Example:

    • Step1: Add event handler in the template
    <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> 
    
    • Step2: Create event handler in component.ts to get the selected rows
    onUserRowSelect(event) {
        this.selectedRows = event.selected;
    }
    
    • Step3: Create button and call a function to do something with selected rows

    Button in html

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

    Click handler in component.ts

    onClick() {
        // It will console all the selected rows
        console.log(this.selectedRows);
      }
    

    Here you can see this in working: https://stackblitz.com/edit/example-ng2-smart-table-18qsws