更改角度ui-grid的列可见性

Ush*_*haP 18 angular-ui-grid

我想在渲染网格后显示/隐藏列.在我搬到新的ui-grid之前,我打电话给toggleVisible()但是现在它似乎没有用.我试图改变列def可见性(或任何其他属性),如波纹管

columnDefs[9].visible = false;
Run Code Online (Sandbox Code Playgroud)

当我在列定义上设置可见性(在渲染之前)它确实有效,但在病房之后我无法改变它.

小智 17

老问题,但这适用于3.0.0-rc.20.我猜测columnDefs需要在范围内.

$scope.columnDefs = [ 
    { name: 'one' },
    { name: 'two', visible: false }
];

$scope.grid = {
    data: 'data',
    columnDefs: $scope.columnDefs
};

$scope.grid.onRegisterApi = function(gridApi){
    $scope.gridApi = gridApi;
};    

$scope.showColumnTwo = function() {
    $scope.columnDefs[1].visible = true;
    $scope.gridApi.grid.refresh();
};
Run Code Online (Sandbox Code Playgroud)


Igo*_*lyk 11

刚开始使用,angular-ui-grid所以这可能不是最好的解决方案.

尝试包含uiGrid api对象,然后refershgrid对象上调用该方法

...
$scope.gridOptions.onRegisterApi = function(gridApi){
  $scope.gridApi = gridApi;
};
....
columnDefs[9].visible = false;
$scope.gridApi.grid.refresh();
Run Code Online (Sandbox Code Playgroud)

  • 尝试这样的东西刷新3.0` $ scope.gridApi.core.notifyDataChange(uiGridConstants.dataChange.COLUMN);`[API Doc](http://ui-grid.info/docs/#/api/ui.grid的.class:网格#methods_notifydatachange) (3认同)

小智 8

如果有人正在寻找一个不需要你创建columndDef的解决方案.

 s.gridOptions = {
    data: 'myData',
    onRegisterApi: function(gridApi) {
      gridApi.core.registerColumnsProcessor(hideIdColumn);
      s.gridApi = gridApi;

      function hideIdColumn(columns){
          columns.forEach(function(column){
            if(column.field==='_id'){
              column.visible=false;
            }
          });
          return columns;
      }

    }
Run Code Online (Sandbox Code Playgroud)

只需用您自己的条件替换column.field ==='_ id'部分.另外,不要忘记返回列,否则根本不会有任何列.