我有一个可滚动的 TreeTable与 rowHover
- 属性定义如下:
<p-treeTable [value]="items" [columns]="scrollableCols" [resizableColumns]="true" [frozenColumns]="frozenCols" [scrollable]="true" scrollHeight="550px" frozenWidth="600px" [rowHover]="true" *ngIf="submitFinished">
所以我设置了第二列和第三列的背景——我这样做是这样的:
<td style="text-align: center; height: 40px" [ngStyle]="{'background-color' : (col.field=='diff') ? ((getColumnValue(rowData, columns, i) > 0) ?
'#e2fee2' : ((getColumnValue(rowData, columns, i) < 0) ? '#ffa695' : 'white') ) : 'white'}"> <!-- this code only applies on each 2nd and 3rd column, not the first one. The second is always white, the third is either #ffa695, #e2fee2 or white - the first still has its default value -->
结果完全没问题,遗憾的是 rowHover
不仅悬停在我没有更改背景的单元格上。有没有机会避免这种情况并仍然悬停整行?如果您在行悬停时看不到彩色背景,那也没关系。
编辑:
我假设它这样做是因为应用了 [ngStyle]
“保持跟踪”字段的值,然后对其应用颜色,因此即使是来自 rowHover
的选择应该被覆盖。可悲的是,我不知道如何解决这个问题。
这是我希望它看起来像的样子:(通过不应用上述 [ngStyle]
的 <td>
创建的示例)
不过,代码如果有帮助的话:
完整的可滚动主体(应该应用的地方):
<ng-template pTemplate="body" let-rowData="rowData" let-columns="columns">
<tr *ngIf="!rowData.hours"><td *ngFor="let col of columns" style="height: 60px" class="psp-row-cell"></td></tr>
<tr *ngIf="rowData.hours">
<ng-template ngFor let-i="index" let-col [ngForOf]="columns">
<ng-template [ngIf]="rowDataIsEmpty(rowData, col)" [ngIfElse]="editableColumn">
<td class="blocked-cell" style="height: 40px">
</td>
</ng-template>
<ng-template #editableColumn>
<ng-template [ngIf]="col.field=='plan'" [ngIfElse]="blockedCell">
<td style="text-align: center; height: 40px" [ttEditableColumn]="rowData">
<p-treeTableCellEditor>
<ng-template pTemplate="input">
<input pInputText type="text" style="text-align: center" [ngModel]="getColumnValue(rowData, columns, i)" (ngModelChange)="setColumnValue(rowData, columns, i, $event)" [ngStyle]="{'width':'100%'}">
</ng-template>
<ng-template pTemplate="output">{{getColumnValue(rowData, columns, i)}}</ng-template>
</p-treeTableCellEditor>
</td>
</ng-template>
<ng-template #blockedCell>
<td style="text-align: center; height: 40px" [ngStyle]="{'background-color' : (col.field=='diff') ? ((getColumnValue(rowData, columns, i) > 0) ?
'#e2fee2' : ((getColumnValue(rowData, columns, i) < 0) ? '#ffa695' : 'white') ) : 'white'}">
{{getColumnValue(rowData, columns, i)}}
</td>
</ng-template>
</ng-template>
</ng-template>
</tr>
</ng-template>
getColumnValue:
getColumnValue(rowData: any[], columns: any, i: number): number {
let col = columns[i];
let val;
if (col.field == 'diff') {
col = columns[i-2];
val = rowData['hours'].find(entry => entry.year === col.year && entry.month === col.month)[col.field];
col = columns[i-1];
val = val - rowData['hours'].find(entry => entry.year === col.year && entry.month === col.month)[col.field];
} else {
val = rowData['hours'].find(entry => entry.year === col.year && entry.month === col.month)[col.field];
}
return val;
}
最佳答案
正如假设的那样,background-color
似乎在被 rowHover
更改时被 [ngStyle]
覆盖。一种解决方法是简单地将 white
改回 transparent
,因此:
[ngStyle]="{'background-color' : (col.field=='diff') ? ((getColumnValue(rowData, columns, i) > 0) ?
'#e2fee2' : ((getColumnValue(rowData, columns, i) < 0) ? '#ffa695' : 'transparent') ) : 'transparent'}
这在某种程度上解决了这个问题。
关于html - PrimeNG 表 [rowHovered] 不适用于设置了单元格背景的单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54114526/