单元格 class 更改推迟到下一次编辑或滚动

Cell class change is postponed till next edit or scroll

我正在尝试用蓝色标记 UI Grid 单元格中的已编辑。为此,在 afterCellEdit 事件中,我对行实体进行了一些更改。 cellClass 函数检查这些标记和 returns 对应的 class 名称。还使用调用 $scope.$apply(在文档中指定)。

以下示例已简化,因为没有检查已编辑的列。在真实的应用程序中,我有更复杂的代码来标记特定的单元格。但是这个例子足以说明问题:

  1. Select 单元格在 Company 列(但不在最后一行)。
  2. 更改那里的值。
  3. 输入
    值发生变化,Edited 列变为真,选择移至下一行,但已编辑单元格中的文本仍为黑色
  4. 再次按 Enter
    在第 2 步单元格中编辑的文本变为蓝色。

如何在第 3 步而不是第 4 步强制更改颜色?

http://plnkr.co/edit/SEEf4DPhA3CB1mx7R4I8?p=preview

var app = angular.module('app', ['ngAnimate', 'ngTouch', 'ui.grid', 'ui.grid.selection', 'ui.grid.edit', 'ui.grid.cellNav']);

app.controller('MainCtrl', ['$scope', '$http', function ($scope, $http) {
  $scope.gridOptions = {
    enableCellEditOnFocus: true,
    
    columnDefs: [{
      field: 'edited',
      enableCellEdit: false
    },{
      field: 'company',
      cellClass: function(grid, row, col, rowRenderIndex, colRenderIndex) {
        return row.entity.edited ? 'blue' : '';
      }
    }],
    
    onRegisterApi: function (gridApi) {
      gridApi.edit.on.afterCellEdit($scope, function (rowEntity, colDef, newValue, oldValue) {
        if (newValue !== oldValue) {
          rowEntity.edited = true;
          $scope.$apply();
        }
      });
    }
  };

  $http.get('https://cdn.rawgit.com/angular-ui/ui-grid.info/gh-pages/data/100.json')
    .success(function(data) {
      $scope.gridOptions.data = data;
    });
}]);
.grid {
  width: 500px;
  height: 200px;
}

.blue {
  color: blue;
}
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.3/angular.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.3/angular-touch.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.3/angular-animate.js"></script>
<script src="http://ui-grid.info/docs/grunt-scripts/csv.js"></script>
<script src="http://ui-grid.info/docs/grunt-scripts/pdfmake.js"></script>
<script src="http://ui-grid.info/docs/grunt-scripts/vfs_fonts.js"></script>
<script src="http://ui-grid.info/release/ui-grid.js"></script>
<link rel="stylesheet" href="http://ui-grid.info/release/ui-grid.css" type="text/css">
<link rel="stylesheet" href="main.css" type="text/css">

<div ng-app="app" ng-controller="MainCtrl">
  <div id="grid1" ui-grid="gridOptions" ui-grid-selection ui-grid-edit ui-grid-cellNav class="grid"></div>

PS: Same question in Russian.

您可以使用 notifyDataChange 让 uigrid 知道值已更改。

只需在您的控制器中注入 uiGridConstants 并将 $scope.apply() 替换为 gridApi.core.notifyDataChange(uiGridConstants.dataChange.COLUMN);

var app = angular.module('app', ['ngAnimate', 'ngTouch', 'ui.grid', 'ui.grid.selection', 'ui.grid.edit', 'ui.grid.cellNav']);

    app.controller('MainCtrl', ['$scope', '$http','uiGridConstants', function ($scope, $http,uiGridConstants) {
    $scope.gridOptions = {
    enableCellEditOnFocus: true,

    columnDefs: [{
      field: 'edited',
      enableCellEdit: false
    },{
      field: 'company',
      cellClass: function(grid, row, col, rowRenderIndex, colRenderIndex) {
        return row.entity.edited ? 'blue' : '';
      }
    }],

    onRegisterApi: function (gridApi) {
      gridApi.edit.on.afterCellEdit($scope, function (rowEntity, colDef, newValue, oldValue) {
        if (newValue !== oldValue) {
          rowEntity.edited = true;
          gridApi.core.notifyDataChange(uiGridConstants.dataChange.COLUMN);
        }
      });
    }
};

$http.get('https://cdn.rawgit.com/angular-ui/ui-grid.info/gh-pages/data/100.json')
    .success(function(data) {
      $scope.gridOptions.data = data;
    });
}]);

一个工作的 plunker : http://plnkr.co/edit/GrseKTm6EF2CdWiNhXM7?p=preview