首页 > 解决方案 > Ag-Grid - 访问所有数据 - 包括计算列

问题描述

尝试访问在 Ag-Grid 中呈现的所有数据 - 包括使用value getters的计算列的值。我只能访问提供给 Ag-Grid 的数据,但不能访问任何计算列(参见下面的代码)。

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

任何建议将不胜感激。我正在尝试将计算列的输出保存到数据库中。

标签: javascriptag-grid

解决方案


我就是这样做的,而不必直接访问 valueGetter 函数中的数据。它不是超级干净,但它得到了预期的计算值。我的版本是 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);

推荐阅读