Search code examples
angularag-gridag-grid-angular

AG-Grid custom filter (Angular) - how to get row data inside OnNewRowsLoaded()


Currently working on a custom filter for AG-Grid, and it's mostly working, save for when new rows are loaded in. I want to be able to see which rows are loaded, in order to update the data inside of the custom filter. I've tried to use the IFilterParams.rowModel.forEachNode(), but to no avail. Here's the jist of what I'm seeing:

export class MyCustomFilter implements IFilterAngularComp {
  // (...)

  agInit(params: IFilterParams): void {
    this.params = params;
  }

  onNewRowsLoaded() {
    console.log("new_rows_loaded_start");

    this.params.rowModel.forEachNode((n) => {
      console.log("test");
    });

    console.log("new_rows_loaded_end");
  }

  // (...)

}

Here's what I'm seeing in my console output, after some new rows are loaded:

new_rows_loaded_start
new_rows_loaded_end

Notice how there aren't any "test" entries there. If I do a params.rowModel.forEachNode() anywhere else (outside of the onNewRowsLoaded() method), it prints out the appropriate number of "test" entries. I've been relying on the forEachNode() to get the state of the grid inside my filter, so my issues are the following:

  1. Is it expected that the rowModel.forEachNode() wouldn't work properly inside of the onNewRowsLoaded() method?
  2. If I can't rely on the rowModel.forEachNode() here to determine the newly added rows, would there be another way of doing this?

Solution

  • The Fix:

    It turns out that if we want to iterate over the existing rows inside of the onNewRowsLoaded() method, we should NOT use this.params.rowModel.forEachNode()

    Instead, we should use:

    this.params.api.forEachLeafNode()
    

    The Explanation:

    This is because params.rowModel.forEachNode() depends on existing group / filter data, so calling it inside a filter method causes some problems which results in the method failing silently. Thankfully, params.api.forEachLeafNode() doesn't depend on groups or filters, so we're good to use it in this case.