gpt4 book ai didi

javascript - $event 使用 PrimeNG 以 Angular 绑定(bind)

转载 作者:太空狗 更新时间:2023-10-29 18:29:12 27 4
gpt4 key购买 nike

根据 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 个父元素。

最佳答案

如果您在代码中注意到您使用它“扩展”了局部变量,您可以轻松地切换类,就像您使用切换图标的方式一样:

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

关于javascript - $event 使用 PrimeNG 以 Angular 绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51073465/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com