2
votes

Is there a way to batch select rows in ag-Grid?

Specifically, when I first load data into my grid, I also get from the server a list of rows that should be initially selected. Right now, the only way I know of to select them is to:

this.state.gridApi.forEachNode((node) => {
  if (isInitiallySelected(node.data) {
    node.setSelected(true);
  }
});

However, this fires an oneSelectionChange event for each iteration. This causes some problems in my UI, since I show feedback (a toast) when rows are selected/de-selected.

Is there a way to tell the grid on initial load which rows should be selected?

If not, is there a way via the API to batch-select rows?

If not, is there a way to conditionally silence the onSelectionChange event when doing a node.setSelected() call?

1
I would decouple the showToast logic and in onRowSelected(), I would fire a show toast only when count of initial rows = count of selected rows, after you are done individually selecting them - Pratik Bhat

1 Answers

0
votes

ag-grid doesn't provide optional events emitting. But you can create a hack for internal handling

private initSelectionDone:boolean;

this.initSelectionDone = false;
this.state.gridApi.forEachNode((node) => {
  if (isInitiallySelected(node.data) {
    node.setSelected(true);
  }
});
this.initSelectionDone = true;

onSelectionChange(...){
    if(this.initSelectionDone){
        ...your logic here...
    }
}