如何在angular-ui-grid中为特定值的行着色? [英] How to color row on specific value in angular-ui-grid?

查看:23
本文介绍了如何在angular-ui-grid中为特定值的行着色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据它在新的 angular-ui-grid 3.0 rc12 中的值为一行着色,但我没能做到.以下代码用于在以前的版本 (ngGrid) 中工作:

I'm trying to color a row depending on it's value in the new angular-ui-grid 3.0 rc12 but I haven't been able to. The following code used to work in the previous version (ngGrid):

$scope.gridOptions =
    data: 'data.sites'
    tabIndex: -1
    enableSorting: true
    noTabInterference: true
    enableColumnResizing: true
    enableCellSelection: true
    columnDefs: [
      {field: 'sv_name', displayName: 'Nombre'}
      {field: 'sv_code', displayName: 'Placa'}
      {field: 'count', displayName: 'Cuenta'}
    ]
    rowTemplate: """<div ng-class="{green: true, blue: row.getProperty('count') === 1}"
                         ng-repeat="col in colContainer.renderedColumns track by col.colDef.name"
                         class="ui-grid-cell"
                         ui-grid-cell></div>"""

和相应的css:

.grid {
  width: 100%;
  height: 250px;
}

.green {
  background-color: #2dff07;
  color: #006400;
}

.blue {
  background-color: #1fe0f0;
  color: #153ff0;
}

这里的问题在于表达式:

The problem here is that the expression:

row.getProperty('count') === 1

不再像在 ngGrid 中那样工作.关于如何在 angular-ui-grid (ui-grid.info)

Doesn't work anymore as it did in ngGrid. Any guesses of how to achive the same in angular-ui-grid (ui-grid.info)

非常感谢!

推荐答案

新的 ui-grid 对 cellClass 有一个特殊的属性:

The new ui-grid has a special property for the cellClass:

  $scope.gridOptions = {
    enableSorting: true,
    data:'myData',
    columnDefs: [
      { field: 'sv_name', displayName: 'Nombre'},
      {field: 'sv_code', displayName: 'Placa'},
      { field: 'count', displayName: 'Cuenta',
        cellClass: function(grid, row, col, rowRenderIndex, colRenderIndex) {
          if (grid.getCellValue(row,col) == 1) {
            return 'blue';
          }
          return 'green';
        }
      }
    ]
  };

看看他的 Plunker

请注意,我将 green 类的颜色设为 red,因为这样可以更好地看到并引起最大的混乱:-)

Note that I made the color for class green in red because it's better to see and to stir maximal confusion:-)

更新:

这是行着色的解决方案.

Here is the solution for row coloring.

像这样写你的 rowTemplate:

Write your rowTemplate like this:

  var rowtpl='<div ng-class="{\'green\':true, \'blue\':row.entity.count==1 }"><div ng-repeat="(colRenderIndex, col) in colContainer.renderedColumns track by col.colDef.name" class="ui-grid-cell" ng-class="{ \'ui-grid-row-header-cell\': col.isRowHeader }" ui-grid-cell></div></div>';

这是分叉的Plunker

请注意,背景颜色会被单元格背景覆盖.自己想办法解决这个问题:-)

Note that background-color is overwritten by cell backgrounds. Find a way around this by yourself:-)

很抱歉最初误读了您的问题.我在这个答案中留下了 cellClass 部分,以防有人需要它.

Sorry for the initial misread of your question. I leave the cellClass part in this answer in case anyone may need it.

这篇关于如何在angular-ui-grid中为特定值的行着色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆