0
votes

I am using primeng p-table and using the checkbox selector for selecting the rows as follows.

<p-table [value]="data" [paginator]="true" [loading]="loading" [(selection)]="selectedItems" #dt>
<ng-template pTemplate="header">
   <tr>
      <th style="width: 3rem">
         <p-tableHeaderCheckbox></p-tableHeaderCheckbox>
      </th>
      <th>Header 1</th>
      <th>Header 2</th>
   </tr>
</ng-template>
<ng-template pTemplate="body" let-row>
   <td>
      <p-tableCheckbox [value]="row"></p-tableCheckbox>
   </td>
   <td>Column 1</td>
   <td>Column 2</td>
</ng-template>
</p-table>

This is working an I am able to select the rows by clicking on the checkbox. But I need to have the following things also.

  1. Select the row by clicking anywhere on the row(also tick the checkbox)
  2. Highlight the selected row.

I have checked other options for selecting the rows like Multiple Selection without MetaKey, but it doesn't have a Select All option.

How can I do this ? Any help will be appreciated. Thanks

1

1 Answers

1
votes

This is taken straight from the primeng documentation. Your row is not well formed, you are missing a wrapping "tr" under the body template. You basically need to set the selection mode and the selectable row attribute, and that should highlight the row. You can add some logic in the code for marking the checkbox as selected based on selectedCar in the example below.

<p-table [columns]="cols" [value]="cars" selectionMode="single" [(selection)]="selectedCar">
    <ng-template pTemplate="header" let-columns>
        <tr>
            <th *ngFor="let col of columns">
                {{col.header}}
            </th>
        </tr>
    </ng-template>
    <ng-template pTemplate="body" let-rowData let-columns="columns">
        <tr [pSelectableRow]="rowData">
            <td *ngFor="let col of columns">
                {{rowData[col.field]}}
            </td>
        </tr>
    </ng-template>
</p-table>