最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Highlight selected row in primeng table - Stack Overflow

programmeradmin2浏览0评论

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

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

Share Improve this question asked Sep 30, 2020 at 5:57 Happy CoderHappy Coder 4,70215 gold badges87 silver badges179 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

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>
发布评论

评论列表(0)

  1. 暂无评论