首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >不同状态的文本颜色角度6

不同状态的文本颜色角度6
EN

Stack Overflow用户
提问于 2019-02-07 12:57:12
回答 3查看 13.4K关注 0票数 6

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

这是我的dashboard.component.ts:

代码语言:javascript
复制
 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上调用表的方式:

代码语言:javascript
复制
<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-表:

代码语言:javascript
复制
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",

我应该怎么做,如果我想给状态一些颜色,我的意思是当状态忙,文本颜色变绿,或空闲变黄,重载变红的情况。

帮帮我,伙计们...谢谢,

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2019-02-07 13:21:13

你可以使用下面的代码

代码语言:javascript
复制
<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

票数 4
EN

Stack Overflow用户

发布于 2019-02-07 13:16:57

在生成行时,您可以使用ngClass并对数据进行一些条件检查,如下所示:

代码语言:javascript
复制
<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,

代码语言:javascript
复制
.busy {
    background-color: green;
}

.idle {
    background-color: yellow;
}

.overload {
    background-color: red;
}
票数 3
EN

Stack Overflow用户

发布于 2019-02-07 13:24:05

代码语言:javascript
复制
<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中为每种类型的状态定义类。

代码语言:javascript
复制
.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,在我这一端,它工作得很好。我附上这张截图仅供参考。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54566536

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档