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?
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;
}
you are looking for getRenderedNodes
gridOptions.api.getRenderedNodes()
Note: this will only retrieve the rendered rows. If you use row virtualization, meaning only the rows in the viewport will be in the DOM, only those will be returned. As per the functions docs:
Retrieve rendered nodes. Due to virtualisation this will contain only the current visible rows and those in the buffer.
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)
I think this is the simplest answer.
let {rowsToDisplay} = params.api.getModel();
Update 2023-05-27: As comments on this answer have pointed out, the answer I gave originally is incorrect, especially for the latest version of AG Grid which doesn't even have gridOptionsWrapper
.
Looping through api.forEachNode()
and building a data array manually seems to be the best option.
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()
.
**`Try This:`**
All the selected nodes can b edisplayed here
const rowsToDisplay = api?.getRenderedNodes()?.map(node => node?.data)
For SSR
const { selectAll = false, toggledNodes = [] } = params.api?.getServerSideSelectionState() || {};
It gives a list of all rows, even if you filter by any columns.
getAllRows() {
let allRows = [];
this.gridApi.forEachNodeAfterFilter((rowNode) => allRows.push(rowNode));
return allRows;
}
Incase you are just looking for data then try
this.gridOptions.rowData
Below code gives you all rows whether they rendered or not without looping:
var allRows = gridApi.rowModel.rowsToDisplay;
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);
© 2022 - 2025 — McMap. All rights reserved.