CSS td条件性背景色设置

问题描述 投票:0回答:1

假设我有以下角表

  <table>
    <tr *ngFor="let company of investTable; let i = index">
      <td>
       {{company.name}}
      </td>
      <td>
       {{company.price}}
      </td>
      <td>
       {{company.profit}}
      </td>
    </tr>
  </table>

我怎样才能使表格单元格根据单元格值的不同而有不同的背景色呢? 比如说,如果公司利润是正值,就把它变成绿色。

css angular html-table conditional-statements background-color
1个回答
2
投票

在css

td.highlight {
  background-color: green;
}

在html中:-

<table>
    <tr *ngFor="let company of investTable; let i = index">
      <td>
       {{company.price}}
      </td>
      <td [ngClass]="{'highlight': company.profit > 0}">
       {{company.profit}}
      </td>
    </tr>
  </table>

1
投票

你可以在你的html中加入这个。

<td [style.positive]="company.profit > 0">

在你的css中加入这个。

td.positive{
  background-color: green;
}

1
投票

看看 ngStylengClass. 最简单的使用方法是直接绑定到 style 财产。

<table>
  <tr *ngFor="let company of investTable; let i = index">
    <td>
      {{company.name}}
    </td>
    <td>
      {{company.price}}
    </td>
    <td [style.background-color]="company?.profit > 5 ? 'green' : 'red'">
      {{company.profit}}
    </td>
  </tr>
</table>
© www.soinside.com 2019 - 2024. All rights reserved.