Home > OS >  How to remove table selectionMode completely based on a condition?
How to remove table selectionMode completely based on a condition?

Time:08-12

p-table allows us to add selectionMode property when we want to enable row selection. This can take 'single' or 'multiple' as possible values. What I want is to disable row selection completely if a variable _selectionMode is NONE

The only way I can think is by adding the p-table component conditionally like this:

<p-table *ngIf="_selectedMode !== 'NONE'"
    [value]="products"
    selectionMode="single"
    [(selection)]="selectedProduct1"
    dataKey="code"
  >
<p-table *ngIf="_selectedMode === 'NONE'"
    [value]="products"
    [(selection)]="selectedProduct1"
    dataKey="code"

but if I do this, I have to duplicate all the attribute bindings on p-table (and there are quite a lot of them in my project). Is there a better way of disabling row selection conditionally?

Simple stackblitz is here

CodePudding user response:

set selectionMode to none for no selection.

Reference here do upvote!

<p-toast></p-toast>
<button (click)="selectionMode = 'single'">single</button>
<button (click)="selectionMode = 'none'">none</button>
<button (click)="selectionMode = 'multiple'">multiple</button>
<div >
  <h5>Single</h5>
  <p>
    In single mode, a row is selected on click event of a row. If the row is
    already selected then the row gets unselected.
  </p>
  <p-table
    [value]="products"
    [selectionMode]="selectionMode"
    [(selection)]="selectedProduct1"
    dataKey="code"
  >
    <ng-template pTemplate="header">
      <tr>
        <th>Code</th>
        <th>Name</th>
        <th>Category</th>
        <th>Quantity</th>
      </tr>
    </ng-template>
    <ng-template pTemplate="body" let-product>
      <tr [pSelectableRow]="product">
        <td>{{ product.code }}</td>
        <td>{{ product.name }}</td>
        <td>{{ product.category }}</td>
        <td>{{ product.quantity }}</td>
      </tr>
    </ng-template>
  </p-table>
</div>

stackblitz

CodePudding user response:

This can be achieved by setting the selectionMode property to null (via property binding) as shown here. Additionally, you need to set pSelectableRowDisabled to true for each row

  • Related