Angular ui grid - 针对不可选行的特定类

时间:2015-06-01 07:08:01

标签: angularjs angular-ui-grid

我使用角度ui-grid来显示我的数据。

我启用了在gridOptions中选择行的选项:

enableRowSelection: true,

但是对于特定的行,我通过以下代码禁用选择:

$scope.mygrid.isRowSelectable = function (row) {
    if (row.entity.id == 2) {
        return false;
    } else {
        return true;
    }
};

这是工作,我不能选择id = 2的行,

但是我想为这一行添加类来通知它是不可选择的。

有什么想法吗?

2 个答案:

答案 0 :(得分:1)

突出显示实际行:

您可以编写自己的rowTemplate,并根据类似的实体ID将该类分配给该行,

 var rowTemplate =  '<div>' +
                 '  <div ng-class="{ \'red\': row.entity.company==\'Enersol\' }" 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>';
  $scope.gridOptions = {
    rowTemplate:rowTemplate,
    enableSorting: true,
    columnDefs: [
      { field: 'name'},
      { field: 'company'}
    ]
  };

而不是row.entity.company = \&#39; Enersol \&#39;您可以将其更改为row.entity.id并指定类名。

在这个例子中,红色&#39;给出黄色的背景颜色和红色的前景色。

看看这个plnkr。 http://plnkr.co/edit/vaqBY235Lfz7WLvy0FCc?p=preview

要修改实际的行标题图标:

您可以覆盖选择行标题按钮的模板并添加自定义类css。在控制器中注入templateCache并覆盖这样的模板。

$templateCache.put('ui-grid/selectionRowHeaderButtons',
    "<div class=\"ui-grid-selection-row-header-buttons\" ng-class=\"{'ui-grid-row-selected': row.isSelected , 'ui-grid-icon-cancel':!grid.appScope.isSelectable(row.entity), 'ui-grid-icon-ok':grid.appScope.isSelectable(row.entity)}\" ng-click=\"selectButtonClick(row, $event)\">&nbsp;</div>"
  );

模板使用控制器作用域中的方法来识别行是否可选。

示例plnkr http://plnkr.co/edit/vaqBY235Lfz7WLvy0FCc?p=preview

答案 1 :(得分:0)

在columnDefs中,您可以添加cellClass

function applyClass(grid, row, col, rowRenderIndex, colRenderIndex) {
    if (row.entity.IsMapped) {
        return 'disabledRow';
    }
}

$scope.yourGrid = {
    columnDefs: [
        { cellClass: applyClass }
    ]
};