Reputation: 944
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 .
Upvotes: 25
Views: 54280
Reputation: 944
I used the grid api deselectAll
function. It worked!
this.gridOptions.api.deselectAll();
Upvotes: 46
Reputation: 4074
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}
//...
>
Upvotes: 10
Reputation: 101
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);
});
Upvotes: 2
Reputation: 562
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
.
Upvotes: 10
Reputation: 51
set gridOptions.rowDeselection to true with rowSelection as multiple will deselect a selected by click when holding control key.
Upvotes: 3
Reputation: 1263
You could try the deselectAll() method in the GridApi. Though, it doesn't appear that AgGrid has an option to deselect specific rows.
Upvotes: 3