UI GRID:columnDefs 中的 Change cell Filter 没有很好地更新

UIGRID: Changing cellFilter in columnDefs is not well updated

我需要动态更改列的小数精度。 我尝试了不同的方法,但没有一种有效。

我在 Plunker 中做了一个快速示例,其中有一个用于交换数据的按钮。它们有不同的数据和不同的 columnDefs:一个有 3 个小数点,另一个有 4 个小数点。 您可以在那里看到第一行保持第一种格式。

我在 gridApi 中使用 notifyDataChange,但不适用于此类更改:

$scope.gridApi.core.notifyDataChange( this.uiGridConstants.dataChange.ALL );

这对我来说是个问题,因为在我的例子中,我想保留相同的数据但动态更改列的小数精度。

有人知道如何 refresh/reload 新的 columnDefs 配置吗? 我真的很感激!

遗憾的是,似乎 none refresh/update 网格的常用方法将适用于此用例。

我设法通过删除所有行然后在 $timeout 之后重新创建它们来使其工作。

我更新了你的 plunker here

主要编辑如下:

 $scope.swapData = function() { 
    $scope.gridOpts.data = []{
    $timeout(function() {
      if (!$scope.isData2) { 
        $scope.gridOpts.data = data2;
        $scope.gridOpts.columnDefs = columnDefs2;
      }
      else {
        $scope.gridOpts.data = data1;
        $scope.gridOpts.columnDefs = columnDefs1;
      }
      $scope.isData2 = !$scope.isData2;  
    });
  };

ui-grid github 仓库中似乎有一个 answer

来自贡献者:

The cellFilter is joined in with the cell template when the cell template is fetched/used. It doesn't get updated live.

可能的解决方案:

colDef.cellFilter = 'date:col.filterFormat'

uiGridApi.core.registerColumnsProcessor(function(columns, rows){
  angular.forEach(columns, function(col){
    var filterFormat = col.colDef.filterFormat; 
    if ( filterFormat ){ col.filterFormat = filterFormat; }
});
return rows;
}, 40);