Matthew Herbst
Matthew Herbst

Reputation: 31983

How to batch select rows in ag-Grid

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?

Upvotes: 2

Views: 1015

Answers (2)

MinteLiu.l
MinteLiu.l

Reputation: 59

you can use the param suppressFinishActions to prevent the selectionChanged event.

/**
 * Select (or deselect) the node.
 * @param newValue -`true` for selection, `false` for deselection.
 * @param clearSelection - If selecting, then passing `true` will select the node exclusively (i.e. NOT do multi select). If doing deselection, `clearSelection` has no impact.
 * @param suppressFinishActions - Pass `true` to prevent the `selectionChanged` from being fired. Note that the `rowSelected` event will still be fired.
*/
public setSelected(newValue: boolean, clearSelection: boolean = false, suppressFinishActions: boolean = false) {
    this.setSelectedParams({
        newValue,
        clearSelection,
        suppressFinishActions,
        rangeSelect: false
    });
}

Upvotes: 1

un.spike
un.spike

Reputation: 5113

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...
    }
}

Upvotes: 0

Related Questions