Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ag-grid Row selection

While selecting the rows of an ag-grid with the help of cell selection using : this.gridOptions.rowMultiSelectWithClick = true ; Is it possible to access the last selected row only for computation and keep the previously selected rows state intact.

like image 702
nikom Avatar asked Mar 06 '23 04:03

nikom


2 Answers

Adding to what Paritosh mentioned, you could listen to rowSelected event and maintain an array of nodes.

<ag-grid-angular
   [rowSelection]="rowSelection"
   (rowSelected)="onRowSelected($event)"
   [rowMultiSelectWithClick]="true"
   (selectionChanged)="onSelectionChanged($event)"></ag-grid-angular>
selectedNodes: RowNode[];     

onRowSelected(event) {
   if(event.node.selected) {
      this.selectedNodes.push(event.node);
   }
}

getLastItem() {
   return this.selectedNodes[this.selectedNodes.length - 1];
} 

You can call getLastItem() method any time according to your requirement and get the lastly selected node.

like image 72
Senal Avatar answered Mar 18 '23 21:03

Senal


Sure, you have to use onRowSelected event for that, in which you'll get the required data as event argument.

onRowSelected(event) {
  console.log("row " + event.node.data + " selected = " + event.node.selected);
}

The data: event.node.data
Selected or not: event.node.selected

<ag-grid-angular
  .....
  [rowSelection]="rowSelection"
  (rowSelected)="onRowSelected($event)"
  [rowMultiSelectWithClick]="true"
  (selectionChanged)="onSelectionChanged($event)"
  ></ag-grid-angular>

Here is the live example: Plunk - ag-grid rowMultiSelectWithClick


Update

You can use gridApi to then get the last selected node. This will even work when you are deselecting a row. It would give us the last row which was selected before deselecting the latest row.

getLastSelectedNode(){
  let rows = this.gridApi.getSelectedRows();
  if(rows.length > 0)
    console.log(rows[rows.length - 1]);
  else
    console.log('No rows selected');
}

Updated Plunk

like image 35
Paritosh Avatar answered Mar 18 '23 20:03

Paritosh