Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change angular ui-grid's columns visibility

I want to show/hide columns after the grid is rendered. Before i moved to the new ui-grid I called to toggleVisible() but now it doesn't seem to work. I tried to change the column def visibility(or any other property) like bellow

columnDefs[9].visible = false;

When I set the visibility on the column definition(before render) it does work, but after wards i cannot change it.

like image 525
UshaP Avatar asked Oct 26 '14 10:10

UshaP


3 Answers

Old question, but this works for me in 3.0.0-rc.20. I'm guessing columnDefs needs to be in scope.

$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();
};
like image 52
mmamane Avatar answered Nov 15 '22 16:11

mmamane


Just started working with angular-ui-grid so this might be not the best solution.

Try including the uiGrid api object and then invoking the refersh method on a grid object

...
$scope.gridOptions.onRegisterApi = function(gridApi){
  $scope.gridApi = gridApi;
};
....
columnDefs[9].visible = false;
$scope.gridApi.grid.refresh();
like image 40
Igor Malyk Avatar answered Nov 15 '22 17:11

Igor Malyk


In case anyone was looking for a solution that didn't require you to create a 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;
      }

    }

Just replace the column.field==='_id' part with your own condition. Also don't forget to return the columns or you will not get any columns at all.

like image 33
scaryxited Avatar answered Nov 15 '22 16:11

scaryxited