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

10

43

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?

Fuqua answered 5/6, 2018 at 9:55 Comment(0)
M
51

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;
}
Minnie answered 6/6, 2018 at 10:54 Comment(6)
I am doing the same for now but, was wondering if ag-grip provides any inbuilt function for the same. Thanks for the advice though.Fuqua
I have tried to find one so I'm at least 90% sure there is no such thing. The closest one I found is if you do api.getModel() that returns a InMemoryRowModel that has the property rowsToDisplay.Literally
OP already tried 'api.forEachNode()' (as mentioned in original question) and is expecting an inbuilt function/property which can return rows/count in gridBulldoze
From the video link below, the array always keep updated and rendered to the DOM. Do not need to retrieve row data. youtu.be/_V5qFr62uhYUnhappy
How do you do this in React Hook (without using "this")?Unhappy
I don't think the above code will work if you have row groups.Imposition
C
17

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.

Cullet answered 17/5, 2022 at 21:21 Comment(4)
not sure if there's a better answer out there, but this is the best answer on this thread by a country mile. thx.Affinity
This is excellent and the best answer to the question above. Thanks for sharing your knowlegde with us!Onions
This doesn't work if you have a tree-grid displaying the same node at multiple locations of the tree.Lilybel
gridOptions.api.getRenderedNodes().map(x=>x.data)Borkowski
E
7

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)

Emasculate answered 3/7, 2019 at 19:37 Comment(0)
H
7

I think this is the simplest answer.

let {rowsToDisplay} = params.api.getModel();

Holds answered 8/3, 2021 at 4:12 Comment(0)
W
4

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().

Wooton answered 30/1, 2020 at 17:56 Comment(2)
In this case, you will get an initial value of rowData, but not an updated, for example, if some row was removed.Bigler
The GridOptionsWrapper API was removed for v29 in scope of the private issue AG-7603 Delete GridOptionsWrapperDoodlebug
A
1
  **`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() || {};
Agincourt answered 20/4, 2023 at 10:19 Comment(1)
You can improve your answer by explaining whyFell
P
0

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;
}
Pourpoint answered 14/9, 2022 at 9:51 Comment(0)
A
0

Incase you are just looking for data then try

this.gridOptions.rowData
Armenia answered 6/10, 2023 at 11:9 Comment(0)
N
0

Below code gives you all rows whether they rendered or not without looping:

var allRows = gridApi.rowModel.rowsToDisplay;
Nugent answered 22/3, 2024 at 8:17 Comment(0)
R
-1

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);
Raffin answered 16/5, 2019 at 16:1 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.