我在angular上有一个视图,就像这样:

这是我的dashboard.component.ts:
export class DashboardComponent implements OnInit {
tablePresetColumns;
tablePresetData;
ngOnInit() {
this.tablePresetColumns = [{id: 1,content: 'Username'},{id: 2,content: 'Status'}];
this.tablePresetData = [[{id: 1,content: 'john.doe@random.com'},{id: 2,content: 'Busy'}],[{id: 1,content: 'test2@random.com'},{id: 2,content: 'overload'}]];
}
} 这是我在dashboard.component上调用表的方式:
<div eds-tile class="xl-4" style="height: 700px">
<eds-tile-title>User on Shift</eds-tile-title>
<eds-table [columns]="tablePresetColumns" [data]="tablePresetData" [modes]="['compact', 'dashed']"></eds-table>
</div>eds-表:
selector: 'eds-table',
template: "<table class=\"table\" [ngClass]=\"modes\">\n <thead *ngIf=\"columns\">\n <tr>\n <th *ngFor=\"let col of columns\">\n {{col.content}}\n </th>\n </tr>\n </thead>\n <tbody>\n <tr *ngFor=\"let row of data\">\n <td *ngFor=\"let cell of row\">\n {{cell.content}}\n </td>\n </tr>\n </tbody>\n</table>\n",我应该怎么做,如果我想给状态一些颜色,我的意思是当状态忙,文本颜色变绿,或空闲变黄,重载变红的情况。
帮帮我,伙计们...谢谢,
发布于 2019-02-07 13:21:13
你可以使用下面的代码
<td *ngFor="let cell of row"
[ngStyle]="{'color': (cell.content === 'Busy') ? 'green' : (cell.content === 'overload') ? 'red' : (cell.content === 'idle') ? 'yellow' : ''}">{{cell.content}}
</td>条件应该在cell.content上,而不是在row.content上
发布于 2019-02-07 13:16:57
在生成行时,您可以使用ngClass并对数据进行一些条件检查,如下所示:
<table class="table\" [ngClass]="modes\">
<thead *ngIf="columns\">
<tr>
<th *ngFor="let col of columns"> {{col.content}} </th>
</tr>
</thead>
<tbody>
<tr *ngFor="let row of data">
<td [ngClass]="{
'busy' : cell.content == 'Busy',
'idle' : cell.content == 'Idle'
'overload' : cell.content == 'Overload'
}" *ngFor="let cell of row"> {{cell.content}} </td>
</tr>
</tbody>
</table>并且您还应该具有用于上述as的css,
.busy {
background-color: green;
}
.idle {
background-color: yellow;
}
.overload {
background-color: red;
}发布于 2019-02-07 13:24:05
<table class="table" [ngClass]="modes">
<thead *ngIf="columns">
<tr>
<th *ngFor="let col of columns"> {{col.content}} </th>
</tr>
</thead>
<tbody>
<tr *ngFor="let row of data">
<td [ngClass]="cell.content.toLowerCase()" *ngFor="let cell of row"> {{cell.content}} </td>
</tr>
</tbody>
</table>在css中为每种类型的状态定义类。
.busy {
color: red;
/* other css property you want to add */
}
.idle {
color: red;
/* other css property you want to add */
}
.overload {
color: red;
/* other css property you want to add */
}这就是stackblitz,在我这一端,它工作得很好。我附上这张截图仅供参考。

https://stackoverflow.com/questions/54566536
复制相似问题