Is there any grid api to deselect ag-grid selected rows programatically? I'm trying to perform some operation on the selected row, basically an async operation , after which I need to deselect this row from the grid .
6 Answers
Used grip api deselectAll function . It worked !
this.gridOptions.api.deselectAll();

- 794
- 2
- 9
- 17
-
this works fine but due to this table is continuously refreshing – TAHA SULTAN TEMURI Dec 16 '19 at 11:13
-
1Can we deselect any specific row in aggrid? – Rajat Apr 23 '20 at 07:04
-
It is better to assign the API object into its own object (and run this function from it), upon "gridReady" event, but basically this code works fine for its purpose. Thanks – TheCuBeMan Aug 09 '20 at 15:16
For anyone that finds this in the future:
Allow manual row deselection by setting gridOptions.rowDeselection = true
as Victor said.
Programatically deselect all rows using gridOptions.api.deselectAll()
as OP discovered.
To programatically deselect a single row, use rowNode.setSelected(false)
.
rowNode.setSelected(isSelected, clearSelection)
can be used to select rows as well, and will deselect all rows other than the subject rowNode if clearSelection
is true
.

- 526
- 5
- 12
-
1
-
A `rowNode` is the datatype returned by the functions that get rows. Examples of these functions called from the gridApi: https://www.ag-grid.com/angular-grid/grid-api/#reference-rowNodes Read more about row nodes here: https://www.ag-grid.com/angular-grid/row-object/ – naffarn Apr 14 '21 at 06:38
I believe it is weird but setting rowDeselection
to true
didn't work for me. What I wanted was simple: Being able to deselect a row when it was selected already. So I checked the Row Selection section of AG Grid's documentation and I find this:
rowMultiSelectWithClick: ... Clicking a selected row in this mode will deselect the row.
Huh! Yeah that sounds like what I need! But I don't want multiple selection...! I want single selection ONLY. So I thought maybe setting rowSelection
to single
will fix it and the selection will be single and deselectable. And... yes it works! The reason I was in doubt initially when doing this is using "rowMultiSelectWithClick" together with "single rowSelection" sounds contradictory, but it works anyways and this is the thing that matters really! :)
So e.g., if you're using it in React (quite similar in Angular or Vanilla JavaScript), just add:
<AgGridReact
rowSelection="single"
rowMultiSelectWithClick={true}
//...
>

- 3,163
- 2
- 28
- 37
set gridOptions.rowDeselection to true with rowSelection as multiple will deselect a selected by click when holding control key.

- 51
- 3
You could try the deselectAll() method in the GridApi. Though, it doesn't appear that AgGrid has an option to deselect specific rows.

- 1,137
- 2
- 11
- 25
-
1Yes, Though I was having single select ag grid instance , So this worked. But If you want to deslect some specific rows you can use api.forEachNodeAfterFilter( function(node) { // select the node node.setSelected(false); }); – Riya Sep 06 '18 at 08:55
-
Nice~ I have faved your question. because I use AgGrid myself and might stumble upon this issue myself eventually :) – ak.leimrey Sep 06 '18 at 09:27
To deselect a specific row/node use api.getSelectedNodes() instead of getSelectedRows(). Then for each node use node.data for the row info you need and then node.setSelected(false) to deselect when done.
let selected = gridOptions.api.getSelectedNodes();
_.each(selected, function(node) {
let row = node.data;
//stuff
node.setSelected(false);
});

- 83
- 9