javascript - 仅使表格中突出显示的单元格值

标签 javascript html css angularjs

我正在使用 angularjs,在更新表中的某个值时,我想添加一个 CSS 类,仅添加到该特定单元格。

  • 如何只在该单元格中添加高亮类?而不是整行......使用 Angular Directive(指令)

预期行为:由于 Value2 已更新,因此应仅突出显示该单元格。

我的 plunker:http://plnkr.co/edit/mfglNOpluccUKLTlLdja?p=preview

我的 table :

<table border="1" style="width: 320px;" class="table table-striped table-hover table-responsive table-bordered">
  <thead style="font-weight: bold;">
    <tr>
      <th ng-repeat="column in columnsTest" ng-if="column.checked" ng-bind="column.id"></th>
    </tr>
  </thead>
  <tbody>
    <tr ng-repeat="row in rows">

      <td ng-repeat="column in columnsTest" ng-if="column.checked">
        <span class="glyphicon glyphicon-circle-arrow-right" ng-show="column.id === 'Value1'"></span>
        <span data-highlighter="row.Updated">{{row[column.id]}}</span>
      </td>
    </tr>
  </tbody>
</table>

CSS:

.highlight {
background-color: lightgreen;
transition: background 200ms;
}

Angular Directive(指令):

app.directive('highlighter', ['$timeout', function ($timeout) {
return {
    restrict: 'A',
    scope: {
        model: '=highlighter'
    },
    link: function (scope, element) {
        scope.$watch('model', function(updated) {
                if (updated) {


                element.addClass('highlight');

                // auto remove after some delay
                $timeout(function () {
                    element.removeClass('highlight');
                }, 1000);
                }
        });
    }
};

最佳答案

这部分是问题所在:

model: '=highlighter'

因为

<span data-highlighter="row.Updated">

这将为 model 提供 row.Updated 的值,该值将在所有三列上发生变化,因为它是在其父级上定义的。

改成

<span data-highlighter="row[column.id]">

然后就可以了。


编辑:删除了我之前的回答。

关于javascript - 仅使表格中突出显示的单元格值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36645284/

相关文章:

javascript - 什么是 `vsc-initialized` ?

css - 2 种不同的 Bootstrap 轮播共存

javascript - 在 Pentaho CDE 中选择组件

javascript - 带有指令的 AngularJS 动态表单字段 ID 不起作用

javascript - 使用多个 Not Acceptable prop值时,为什么我的React propTypes的Jest测试会中断?

php - 我可以在 foreach 循环中过滤 $_GET 数组吗

javascript - 理论上是否可以创建一个 'window' 以使用 HTML 和 JavaScript 在 div 后面查看?

iphone - 如何在我的 &lt;input&gt; 上禁用 iPhone 的自动填充栏?

php - 通过 PHP session 自定义 CSS 属性?

css - 布局下推 div