How to add custom column dynamically in ag-grid

Shivek Parmar picture Shivek Parmar · Apr 13, 2016 · Viewed 11.1k times · Source

I'm stacked to add Custom columns dynamically in ag-grid. I have tried following approach.

additionalCol = { field:'SOME RANDOM', headerName: 'SOME RANDOM' };
$scope.gridOptions.columnApi.addValueColumn(additionalCol);

I have also tried

$scope.columns.push(additionalCol);
$scope.gridOptions.api.refreshView();

but didn't get success. I even tried to add columns that exist in Data but failed. Below is Image for what I am trying. enter image description here

As shown in image "% dev" is Custom column which I need to add dynamically with dynamically calculated values.

Answer

Alexander Zbinden picture Alexander Zbinden · Apr 20, 2016

Try this:

var columnDefs = $scope.gridOptions.columnDefs;
columnDefs.push({ field:'SOME RANDOM', headerName: 'SOME RANDOM'});
$scope.gridOptions.api.setColumnDefs(columnDefs);