How can I get all the rows of ag-grid?

断了今生、忘了曾经 提交于 2020-06-25 08:57:50

问题


ag-grid provides a way to get selected rows using api.getSelectedRows() function. And provides a way to set all rows using api.setRowData([]) function. I'm looking for something getAllRows() to return all the rows in the grid. I know there is a way to loop through all the rows using api.forEachNode() etc. Is there any way I can get the entire array of rows?


回答1:


I don't think there is such a method but you can make your own:

getAllRows() {
  let rowData = [];
  this.gridApi.forEachNode(node => rowData.push(node.data));
  return rowData;
}



回答2:


Ag-grid doesn't provide any method to do that. You can check it here Accessing Data Ag-grid and here Grid Api.

I guest the reason is because you can do it through a loop, as you mentioned before.

let items: Array<rows> = [];
this.gridApi.forEachNode(function(node) { 
    items.push(node.data);
});

Or if the source of your ag-grid is linked through angular there is no need to loop over the grid (assuming that the data grid has not pending changes)




回答3:


I was wondering the same thing and found it annoying that you cannot simply get all rows. In my case I needed to flip a flag on all rows, where the indicator is set outside the component containing the grid, so I did the following. This is in TypeScript for a React app.

  gridApi!.selectAll();
  const rows = gridApi!.getSelectedRows();

  rows.forEach(row => {
    row.coverageAction = this.props.isIndicatorChecked;
  });

  gridApi!.setRowData(rows);



回答4:


This is how I get the rowData from the grid API:

api.getModel().gridOptionsWrapper.gridOptions.rowData

Lots of methods include gridOptionsWrapper in their return object, so you don't necessarily have to use getModel().



来源:https://stackoverflow.com/questions/50697232/how-can-i-get-all-the-rows-of-ag-grid

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!