AG-网格-访问所有数据-包括计算列

2022-04-10 00:00:00 javascript 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);
  }

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


解决方案

我是这样做的,而不必直接访问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);

相关文章