Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular Grid ag-grid columnDefs Dynamically change

Tags:

I have a problem about columnDefs change dynamically. Here is my gridOptions:

$scope.gridOptions = {   columnDefs: [],   enableFilter: true,   rowData: null,   rowSelection: 'multiple',   rowDeselection: true }; 

And when I retrieve data from server:

$scope.customColumns = [];  $http.post('/Home/GetProducts', { tableName: 'TABLE_PRODUCT' }).success(function (data) {     angular.forEach(data.Columns, function (c) {         $scope.customColumns.push(             {                 headerName: c.Name,                 field: c.Value,                 width: c.Width             }         );     });      $scope.gridOptions.columnDefs = $scope.customColumns;      $scope.gridOptions.rowData = data.Products;     $scope.gridOptions.api.onNewRows(); }).error(function () {  }); 

Note: here c is column object which comes from server.

When dynamically generating columns and assigning it to $scope.gridOptions.columnDefs there is blank grid but $scope.customColumns array is filled with right generated column objects. Please help me is this bug or I am doing something wrong?

like image 540
Vaso Beruashvili Avatar asked Jul 31 '15 10:07

Vaso Beruashvili


People also ask

How do you set ag-grid dynamically height?

You can use ag-grid feature AutoHeight = true in the column Configuration. or if you want to calculate the height dynamically you should use getRowHeight() callback and create DOM elements like div and span , and add your text in it, then the div will give the OffsetHeight for it then you wont see any cropping of data/ ...

How do you update column data in ag-grid?

The easiest way to update data inside the grid is to replace the data you gave it with a fresh set of data. This is done by either updating the rowData bound property (if using a framework) or calling api. setRowData(newData) . See Updating Row Data for more details.


1 Answers

In ag-grid the columns in gridOptions are used once at grid initialisation. If you change the columns after initialisation, you must tell the grid. This is done by calling gridOptions.api.setColumnDefs()

Details of this api method are provided in the ag-grid documentation here.

like image 66
Niall Crosby Avatar answered Sep 29 '22 19:09

Niall Crosby