19

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 .

Riya
  • 794
  • 2
  • 9
  • 17

6 Answers6

38

Used grip api deselectAll function . It worked !

this.gridOptions.api.deselectAll();
Riya
  • 794
  • 2
  • 9
  • 17
8

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.

naffarn
  • 526
  • 5
  • 12
  • 1
    What is rowNode, where do you get it? – cs_pupil Apr 12 '21 at 22:25
  • 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
6

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}
    //...
>
aderchox
  • 3,163
  • 2
  • 28
  • 37
3

set gridOptions.rowDeselection to true with rowSelection as multiple will deselect a selected by click when holding control key.

Victor
  • 51
  • 3
2

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

ak.leimrey
  • 1,137
  • 2
  • 11
  • 25
  • 1
    Yes, 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
2

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);
});
Loom
  • 83
  • 9