如何在主要表格中点击时获得tr的第一个td值

时间:2019-12-06 03:13:26

标签: javascript typescript angular7 primeng primeng-datatable

如何在单击条件下获取tr的第一个td值。如果我单击第一个td,我想获取角度为7的值。请不要为tr编写任何内联代码。我们只需要通过像find table这样的编码来使用。如何通过viedchild或元素引用使用p-table id =“ dt”来做到这一点?

演示:(try)

app.component.ts:

@ViewChild('dt') public dataTable: DataTable;

app.component.html:

<p-table id="dt" [columns]="cols" [value]="cars" [scrollable]="true" [rows]="10"  [virtualRowHeight]="30" [loading]="loading"
    [virtualScroll]="true" (onLazyLoad)="loadCarsLazy($event)" [lazy]="true" [totalRecords]="totalRecords">

    <ng-template pTemplate="header" let-columns>
        <tr>
            <th *ngFor="let col of cols">
                {{col.header}} 
            </th>
        </tr>
    </ng-template>
    <ng-template pTemplate="body" let-rowData let-columns="columns">
        <tr style="height:30px">
            <td *ngFor="let col of cols">
                {{rowData[col.field]}}
            </td>
        </tr>
    </ng-template>
</p-table> 

注意: 请不要这样写内联代码:通过打字稿或JavaScript来完成。

<ng-container *ngFor="let col of cols; let i = index;">
  <td (click)="handleClick(i)">
     {{rowData[col.field]}} 
  </td>
</ng-container>

1 个答案:

答案 0 :(得分:1)

primeng表已经具有选择功能,只需设置选择模式和属性即可绑定所选项目

<p-table selectionMode="single" [(selection)]="selectedCar">
....
</p-table>

<ng-template pTemplate="header" let-columns>
    <tr>
        <th *ngFor="let col of cols">
            {{col.header}} 
        </th>
    </tr>
</ng-template>
<ng-template pTemplate="body" let-rowData let-columns="columns">
    <tr style="height:30px" [pSelectableRow]="rowData">
        <td *ngFor="let col of cols">
            {{rowData[col.field]}}
        </td>
    </tr>
</ng-template>

  

这是您如何在clikc上设置所选项目的方法,它将绑定到   由[(selection)]='...'定义的所选属性不是   [pSelectableRow]="rowData"会将Holde项目设置为选中状态,但   您可以将选定的项目设置为仅tr的第一个colume数据   像这样[pSelectableRow]="rowData['vin']"

demo ⚡

检查? this是否有其他选择情况,例如具有复选框或按钮