FirstRedPepper
FirstRedPepper

Reputation: 452

Ag-Grid - Access All Data - including calculated columns

Trying to access all data rendered in Ag-Grid - including the value of calculated columns which use value getters. I have only been able to access the data provided to Ag-Grid, but not any calculated columns (see code below).

  var items = [];
  var cnt = gridOptions.api.getDisplayedRowCount();
  for (var i = 0; i < cnt; i++) {
    var rowNode = gridOptions.api.getDisplayedRowAtIndex(i);
    items.push(rowNode.data);
  }

Any suggestions would be greatly appreciated. I'm trying to save the output of the calculated columns to a database.

Upvotes: 2

Views: 1796

Answers (2)

This is how I did it without having to directly to data within the valueGetter function. It's not super clean but it gets the calculated value as expected. I'm on version 24.1.0.

const columnKeys = this.gridColumnApi.getColumnState().map(c => c.colId);
const rows = [];

this.gridApi.forEachNode(rowNode => {
  const row = [];
  for (const key of columnKeys) {
    // getValue will get the calculated value at key in the rowNode
    row.push(this.gridApi.getValue(key, rowNode));
  }

  rows.push(row);
});

console.log(rows);

Upvotes: 2

FirstRedPepper
FirstRedPepper

Reputation: 452

Thought of a workaround - write to the data within the valueGetter function. This way the data stores the calculated values and can then be exported using the built-in api functions (such as using forEachNode).

valueGetter: function (params){

  //your normal value getter logic here
  var output = params.data['column1'] + params.data['column2']

  //write to the data
  var col = params.colDef.field;
  params.data[col] = output;

  return output;

}

Upvotes: 2

Related Questions