Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add custom column dynamically in ag-grid

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.

like image 653
Shivek Parmar Avatar asked Apr 13 '16 10:04

Shivek Parmar


People also ask

How to dynamically add columndefs to a grid in R?

So to achieve your goal (dynamically addemove columnDefs) you need to use setColumnDefs (colDefs) method setColumnDefs (colDefs) Call to set new column definitions into the grid. The grid will redraw all the column headers, and then redraw all of the rows.

How do I update column definitions in AG-grid?

Updating existing column definitions To update existing column definitions, we first call the ag-Grid API method getColumnDefs () to get a reference to the grid's current columns. We then map over the columns, changing any desired properties before updating our columnDefs bound property.

How do I create AG-grid columns in react?

In the example above you'll see that we're creating ag-Grid Columns by mapping over a state variable columns and returning a agGridColumn React component for each column definition, spreading props while doing so: To add or remove columns, we simply have to call the setColumns setState method, passing in a new set of column definitions.

Why do I need column headers in AG grid?

Most use-cases for AG Grid will have predefined column headers because this allows taking advantage of the grid configuration available e.g. in-cell editing etc. The AG Grid API offers you full configuration of the Column Definitions:


1 Answers

Try this:

var columnDefs = $scope.gridOptions.columnDefs;
columnDefs.push({ field:'SOME RANDOM', headerName: 'SOME RANDOM'});
$scope.gridOptions.api.setColumnDefs(columnDefs);
like image 112
Alexander Zbinden Avatar answered Sep 19 '22 03:09

Alexander Zbinden