Reputation: 2917
I have an ag-Grid with filtering option.
How to get the number of filtered rows without using forEachNodeAfterFilter
callback?
I want to enable a button only if there is a filtered row, and i don't want to perform a foreach loop in the background every time.
Upvotes: 13
Views: 18503
Reputation: 20078
We can able to access api using gridRef by declaring part of AgGridReact and then
gridRef.current.gridOptions.api.getDisplayedRowCount()
<AgGridReact
ref={gridRef}
columnDefs={columnDefs}
rowData={rowData}
headerHeight={35}
rowHeight={35}
defaultColDef={defaultColDef}
onGridReady={onGridReady}
onFilterChanged={()=> setRow(gridRef.current.gridOptions.api.getDisplayedRowCount())}
></AgGridReact>
Upvotes: 0
Reputation: 453
onFilterChanged(event) {
console.log(gridOptions.api.getDisplayedRowCount());
}
In console the count should be the filtered rows.
Upvotes: 3
Reputation: 1526
Use onFilterChanged()
to access the filtered rows, or the filtered + selected rows. The event passed to onFilterChanged()
can be used like so (example in Typescript React)
(If you need number of filtered just use ev.api.rowModel.rowsToDisplay.length
)
onFilterChanged = ev => {
if (ev?.api?.rowModel?.rowsToDisplay) {
this.setState({ selectedRows: ev?.api?.rowModel?.rowsToDisplay.filter(node => node.isSelected()) });
}
};
Upvotes: 0
Reputation: 325
What you are probably looking for is:
gridOptions.api.getDisplayedRowCount()
The actual number of rows after the filtering event is trivially the number of displayed rows.
Upvotes: 17
Reputation: 7338
gridOptions.api.getModel().rootNode.childrenAfterFilter.length
You should check out what else is available to you under rootNode
, there are a few arrays that might be useful to you
Upvotes: 8