根据PrimeNG的文档,它们具有2个用于表中行扩展的属性。 onRowExpand和onRowCollapse。我想更改单击以展开的当前行的背景颜色。
我的html:
<p-table [columns]="cols" [value]="cars" dataKey="vin" expandableRows="true" styleClass="x-expandable-row"
(onRowExpand)="onRowExpandCallback($event)" (onRowCollapse)="onRowCollapseCallback($event)">
<ng-template pTemplate="header">
<tr>
<th style="width: 2.25em"></th>
<th>Make</th>
<th>Model</th>
<th>Color</th>
</tr>
</ng-template>
<ng-template pTemplate="body" let-car let-expanded="expanded">
<tr>
<td class="x-expand-handeler-row" >
<a href="#" [pRowToggler]="car">
<i [ngClass]="expanded ? 'fas fa-minus' : 'fas fa-plus'"></i>
</a>
</td>
<td>{{car.make}}</td>
<td>{{car.model}}</td>
<td>{{car.color}}</td>
</tr>
</ng-template>
<ng-template pTemplate="rowexpansion" let-car>
<td [attr.colspan]="4" class="x-expanded-row">
<label>Row Details Go Here</label>
</td>
</ng-template>
</p-table>
我的ts文件:
onRowExpandCallback(e) {
//need to find the best way to add and remove a class on expanded row
e.originalEvent.currentTarget.parentElement.parentElement.parentElement.parentElement.bgColor = "#edf6fa";
}
onRowCollapseCallback(e) {
//need to find the best way to add and remove a class on expanded row
e.originalEvent.currentTarget.parentElement.parentElement.bgColor = '#fff';
}
我一直在玩我添加的“ parentElements”数量,但是我没有运气。我的代码在PrimeNG数据表中仅使用2个parentElement。
答案 0 :(得分:1)
如果您在代码中注意到使用了“扩展”的局部变量,则可以轻松地像使用以下方法来切换图标一样切换类:
<i [ngClass]="expanded ? 'fas fa-minus' : 'fas fa-plus'"></i>
您无需处理onRowExpand和onRowCollapse事件
在tr标签上添加[ngClass] =“ expanded?'edf6faClass':'fffClass'”
<p-table [columns]="cols" [value]="cars" dataKey="vin" expandableRows="true" styleClass="x-expandable-row" (onRowExpand)="onRowExpandCallback($event)" (onRowCollapse)="onRowCollapseCallback($event)">
<ng-template pTemplate="header">
<tr>
<th style="width: 2.25em"></th>
<th>Make</th>
<th>Model</th>
<th>Color</th>
</tr>
</ng-template>
<ng-template pTemplate="body" let-car let-expanded="expanded">
<tr [ngClass]="expanded ? 'edf6faClass' : 'fffClass'">
<td class="x-expand-handeler-row">
<a href="#" [pRowToggler]="car">
<i [ngClass]="expanded ? 'fas fa-minus' : 'fas fa-plus'"></i>
</a>
</td>
<td>{{car.make}}</td>
<td>{{car.model}}</td>
<td>{{car.color}}</td>
</tr>
</ng-template>
<ng-template pTemplate="rowexpansion" let-car>
<td [attr.colspan]="4" class="x-expanded-row">
<label>Row Details Go Here</label>
</td>
</ng-template>
</p-table>