3

When rows are selected by the user, I save which rows are selected in some state. When the grid is rendered, I want those rows to still be selected. I've tried in onModelChanged calling setSelected on all the selected rows. However, this is not performant when lots of rows are selected. Also, there is a visible moment before the rows are selected, which is not ideal.

Is there any way I can pre-select rows before the grid is rendered?

GreyBeardedGeek
  • 29,460
  • 2
  • 47
  • 67
Zeke Hernandez
  • 1,226
  • 11
  • 14

4 Answers4

2

I managed to select a row by using firstDataRendered event.

gridAPI.addEventListener(
  "firstDataRendered",
  ({ api }) => {
    // Has to wait until the next tick
    setTimeout(() => {
      api.getDisplayedRowAtIndex(0).selectThisNode(true);
    }, 0);
  },
);

sad comrade
  • 1,341
  • 19
  • 21
1

Yes you can preselect rows like below example.

onGridReady(params) {
  this.gridOptions.api.forEachNode(node=> node.rowIndex === 1 ? node.setSelected (true) : node.setSelected(false));
}

You can make condition based on your state.

Anuj Gupta
  • 11
  • 3
  • thanks for this, but I'm am nearly positive that the rows have not been loaded into the grid in `onGridReady` so `forEachNode` is called on an empty list. I'll have to double check, but I'm pretty sure I tried this. Thanks again. – Zeke Hernandez Mar 22 '20 at 19:37
  • Yes but it can be achieve by making rowData check in ag grid component means load component once rowData ready. I tried this and it works for me. – Anuj Gupta Mar 23 '20 at 06:43
0

Is there any way I can pre-select rows before the grid is rendered?

I assume that you are looking for configuration like editable for columns (its configurable), columns exist after gridReady event, but rows - only after firstRenderer event.

On top of that there are no properties for rows and as far as I know (and also double-checked on doc) there is no settings for that.

I mean you can configure lots of things, but the selection is out of it.

And on their examples, they are using forEach for that.

un.spike
  • 4,857
  • 2
  • 18
  • 38
0

You can use firstDataRendered. Then loop on each node to set the data as selected. Hope this helps you!

firstDataRendered