I have a grid with a default sorted on a column and I'm having problems getting the next/previous rows by adding or substracting from the currently selected row ID.
Here's the column with the default sort
{
headerName: "Created",
field: "createdOn",
cellRenderer: (params) => {
return WebModule.Utils.dateFormat(params.value);
},
sort: "desc",
width: 125
},
And here's my logic to get previous/next row
class ResultModal {
constructor(params) {
this.params = params
let rowIndex = params.rowIndex;
this.previousRow = params.api.getRowNode(rowIndex - 1);
this.nextRow = params.api.getRowNode(rowIndex + 1);
this.result = params.data;
}
}
I pass the whole ag-grid params object to a modal so I can navigate the grid records from buttons in the modal.
The issue is that if I run the above logic with the 2nd row selected, params.rowIndex is 1, I get nextRow with (1+1) but the actual rowIndex of nextRow will be something like 2245 (I have lots of data in the grid).
So I end up selecting a row burried deep down in the grid instead of the actual 3rd row displayed.
Do I need to use something else than getRowNode when the grid is sorted/filtered ?
I ended up using api.getDisplayedRowAtIndex()
let node = params.api.getSelectedNodes()[0];
let rowIndex = node.rowIndex;
this.previousRow = params.api.getDisplayedRowAtIndex(rowIndex - 1);
this.nextRow = params.api.getDisplayedRowAtIndex(rowIndex + 1);
This really gets the previous and next row regardless of the sort/filter options.
One possible solution might be to use
gridOptions.api.forEachNodeAfterFilterAndSort
which iterates over the rows as they are displayed. Note that the callback passes the RowNode itself and the row-index in the grid.
function forEachNodeAfterFilterAndSort(
callback: (rowNode: RowNode, index: number) => void
): void;
https://www.ag-grid.com/javascript-data-grid/grid-api/#reference-rowNodes