【问题标题】:How to dynamically change color of row in a table when it is load加载时如何动态更改表中行的颜色
【发布时间】:2020-06-02 12:21:01
【问题描述】:

我正在使用 Angular 1.6。 我有一张桌子:

<table>
  <tr>
    <th>QTY</th>
    <th>PRICE</th>
    <th>TIME</th>
  </tr>
  <tr>
    <td>1</td>
    <td>20.00</td>
    <td>08:08</td>
  </tr>
  <tr>
    <td>2</td>
    <td>30.00</td>
    <td>08:08</td>
  </tr>
</table>

我需要在加载表格时,在当前时间减去交易时间小于 5 秒时将行背景颜色动态更改为黄色。

5 秒后,背景变回白色。

任何帮助或提示将不胜感激!

【问题讨论】:

标签: javascript angularjs dom-events


【解决方案1】:

你可以在这里使用 ng-style 指令 ng-style 指令允许您有条件地在 HTML 元素上设置 CSS 样式。

你可以在你的表中使用它,如下所示

假设您在“transaction_time”中有交易交易时间,在“current_time”中有当前时间。

<table>
  <tr>
    <th>QTY</th>
    <th>PRICE</th>
    <th>TIME</th>
  </tr>
  <tr ng-repeat="trade in stockMarket track by $index" ng-style="trade.transaction_time - current_time <=5 && { 'background-color': 'yellow' } || trade.transaction_time-current_time>10 && { 'background-color': 'white'}" >
    <td>{{$index}}</td>
    <td>{{trade.amount}}</td>
    <td>{{trade.transaction_time}}</td>
  </tr>
</table>

希望对你有帮助。

【讨论】:

  • 我试图把它放在一个javascript函数中,但它没有被调用?
  • ng-style不能分配javascript函数吗?
  • 是的,可以使用ng风格的函数,看看下面的例子
  • `
    • {{listEntry}}
    angular.module('myApp', []) .controller('MainController', function($scope, $timeout) { $scope.listEntries = ["stock1","stock2"]; $scope.styleFunction = function(index){ return true; } }) `
猜你喜欢
相关资源
最近更新 更多
热门标签