简体   繁体   English

突出显示primeng表中的选定行

[英]Highlight selected row in primeng table

I am using primeng p-table and using the checkbox selector for selecting the rows as follows.我正在使用 primeng p-table并使用复选框选择器来选择行,如下所示。

<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.我已经检查了其他用于选择行的选项,例如Multiple Selection without MetaKey ,但它没有Select All选项。

How can I do this ?我怎样才能做到这一点 ? Any help will be appreciated.任何帮助将不胜感激。 Thanks谢谢

This is taken straight from the primeng documentation.这是直接取自primeng文档。 Your row is not well formed, you are missing a wrapping "tr" under the body template.您的行格式不正确,您在正文模板下缺少包装“tr”。 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.您可以在代码中添加一些逻辑,用于根据以下示例中的 selectedCar 将复选框标记为选中。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM