现在,我想根据状态来动态更改行背景,如下所示:
S-白色; W-蓝色; E-红色; F-绿色;
我的html代码:
<div class="mat-elevation-z8">
<table mat-table [dataSource]="this.dataSource" multiTemplateDataRows class="mat-elevation-z8" >
<!--- Note that these columns can be defined in any order.
The actual rendered columns are set as a property on the row definition" -->
<ng-container matColumnDef="expandedDetail">
<td mat-cell *matCellDef="let element" [attr.colspan]="displayedColumns.length">
<div class="example-element-detail" [@detailExpand]="element == expandedElement ? 'expanded' : 'collapsed'" *ngIf="log_message">
<div class="example-element-description">
<p *ngIf="this.log_message">Log Message:</p>
<p *ngIf="!this.log_message">N/A</p>
<pre *ngIf="this.log_message"> {{ this.log_message }} </pre>
</div>
</div>
</td>
</ng-container>
<ng-container matColumnDef="UPD_DATE">
<th mat-header-cell *matHeaderCellDef> UPD_DATE </th>
<td mat-cell *matCellDef="let data"> {{data.LAST_UPD_DATETIME}} </td>
</ng-container>
<ng-container matColumnDef="S">
<th mat-header-cell *matHeaderCellDef> S </th>
<td mat-cell *matCellDef="let data"> {{data.STATUS}} </td>
</ng-container>
<ng-container matColumnDef="PROC">
<th mat-header-cell *matHeaderCellDef> PROC </th>
<td mat-cell *matCellDef="let data"> {{data.PROC_ID}} </td>
</ng-container>
<ng-container matColumnDef="AGENT">
<th mat-header-cell *matHeaderCellDef> AGENT </th>
<td mat-cell *matCellDef="let data"> {{data.AGENT_ID}} </td>
</ng-container>
<ng-container matColumnDef="SUBMIT_DATE">
<th mat-header-cell *matHeaderCellDef> SUBMIT_DATE </th>
<td mat-cell *matCellDef="let data"> {{data.SUBMIT_DATE}} </td>
</ng-container>
<ng-container matColumnDef="END_DATE">
<th mat-header-cell *matHeaderCellDef> END_DATE </th>
<td mat-cell *matCellDef="let data"> {{data.END_DATE}} </td>
</ng-container>
<ng-container matColumnDef="START_DATE">
<th mat-header-cell *matHeaderCellDef> START_DATE </th>
<td mat-cell *matCellDef="let data"> {{data.START_DATE}} </td>
</ng-container>
<ng-container matColumnDef="USER_SUBMIT">
<th mat-header-cell *matHeaderCellDef> USER_SUBMIT </th>
<td mat-cell *matCellDef="let data"> {{data.USER_SUBMIT}} </td>
</ng-container>
<ng-container matColumnDef="THREAD_NUM">
<th mat-header-cell *matHeaderCellDef> THREAD_NUM </th>
<td mat-cell *matCellDef="let data"> {{data.THREAD_NUM}} </td>
</ng-container>
<ng-container matColumnDef="INSTANCE_ID">
<th mat-header-cell *matHeaderCellDef> INSTANCE_ID </th>
<td mat-cell *matCellDef="let data"> {{data.INSTANCE_ID}} </td>
</ng-container>
<ng-container matColumnDef="START">
<th mat-header-cell *matHeaderCellDef> START </th>
<td mat-cell *matCellDef="let row"><button class="btn btn-success" (click)="onStart(row)" [disabled]="this.started">Start</button></td>
</ng-container>
<ng-container matColumnDef="STOP">
<th mat-header-cell *matHeaderCellDef> STOP </th>
<td mat-cell *matCellDef="let row"><button class="btn btn-danger" (click)="onStop()">Stop</button></td>
</ng-container>
<ng-container matColumnDef="LOG">
<th mat-header-cell *matHeaderCellDef> LOG </th>
<td mat-cell *matCellDef="let row"><button class="btn btn-info" (click)="onLog()">Log</button></td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="displayedColumns; sticky: true"></tr>
<tr mat-row *matRowDef="let element; columns: displayedColumns;" class="example-element-row"
[class.example-expanded-row]="expandedElement === element" (click)="log_message = onElementExpand(element); expandedElement = element;"></tr>
<tr mat-row *matRowDef="let row; columns: ['expandedDetail']" class="example-detail-row"></tr>
</table>
<mat-toolbar>
<mat-toolbar-row>
<button class="btn btn-primary" (click)="getIpeProcessInstances()" [disabled]="false">{{ 'button.Refresh' | translate }}</button>
<span class="example-spacer"></span>
<mat-paginator [pageSizeOptions]="[5, 10, 20]" showFirstLastButtons></mat-paginator>
</mat-toolbar-row>
</mat-toolbar>
还有我的CSS:
table {
width: 100%;
}
tr.example-detail-row {
height: 0;
}
tr.example-element-row:not(.example-expanded-row):hover {
background: #f5f5f5;
}
tr.example-element-row:not(.example-expanded-row):active {
background: #efefef;
}
.example-element-row td {
border-bottom-width: 0;
}
.example-element-detail {
overflow: hidden;
display: flex;
}
.example-element-diagram {
min-width: 80px;
border: 2px solid black;
padding: 8px;
font-weight: lighter;
margin: 8px 0;
height: 104px;
}
.example-element-symbol {
font-weight: bold;
font-size: 40px;
line-height: normal;
}
.example-element-description {
padding: 16px;
}
.example-element-description-attribution {
opacity: 0.5;
}
.mat-elevation-z8 {
width: 100%;
}
.mat-row:hover {
background-color: whitesmoke;
}
.example-icon {
padding: 0 14px;
}
.example-spacer {
flex: 1 1 auto;
}
.mat-paginator
{
background-color: whitesmoke;
}
那么我如何定义每种情况的颜色? 有没有可以动态定义的选项? 我可以为每行使用一个CSS样式吗? 谢谢!
答案 0 :(得分:2)
您还可以使用[ngClass]
来实现此目的:
<td [ngClass]="{
'is-white': data.STATUS === 'S',
'is-blue': data.STATUS === 'W',
'is-red': data.STATUS === 'E',
'is-green': data.STATUS === 'F'
}">...</td>
然后在您的CSS中:
td.is-white {
background: ...;
}
等等!希望能帮助您了解可以使用其他解决方案来实现这一目标。
编辑
使用时,只需在第二个[ngClass]
中使用<tr>
<tr mat-row *matRowDef="let element; columns: displayedColumns;" class="example-element-row"
[class.example-expanded-row]="expandedElement === element"
[ngClass]="{
'is-white': element.STATUS === 'S',
'is-blue': element.STATUS === 'W',
'is-red': element.STATUS === 'E',
'is-green': element.STATUS === 'F'
}" (click)="log_message = onElementExpand(element); expandedElement = element;"></tr>
答案 1 :(得分:1)
您可以通过在{p>上使用[ngStyle]
来实现
<td mat-cell *matCellDef="let data" [ngStyle]="{'background-color': data.STATUS == 's' ? 'white': data.STATUS == 'w' ? 'blue' : data.STATUS == 'e' ? 'Red' : data.STATUS == 'f' ? 'green' : ''}"> {{data.STATUS}} </td>
我的答案并不完美,因为在这种情况下,您需要在每个[ngStyle]
上重复td
答案 2 :(得分:1)
添加具有状态值的属性,然后为该属性编写CSS
例如:HTML
<tr [attr.row_status]="data.STATUS" /></tr>
因此,在呈现视图时,您的HTML将如下所示:
<tr row_status="S"></tr>
现在为选择器添加CSS:
[row_status="S"] {
background: #fff
}
让我知道您是否仍然面临该问题。并可能下次通过代码段共享代码。
干杯:)