Gábor Csikós
Gábor Csikós

Reputation: 2917

How to get the number of filtered rows in ag-Grid?

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

Answers (5)

KARTHIKEYAN.A
KARTHIKEYAN.A

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

Aqeel Bahoo
Aqeel Bahoo

Reputation: 453

onFilterChanged(event) { 
 console.log(gridOptions.api.getDisplayedRowCount());
}

In console the count should be the filtered rows.

Upvotes: 3

Andrew
Andrew

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

LittleXenomorph
LittleXenomorph

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

Jarod Moser
Jarod Moser

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

Related Questions