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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With