'AG-Grid not reordering datasource rows
I have an ag-grid that updates based on a datasource. It works fine except when rows are reordered.
I have a getRowNodeId setup with a uniqueId for each row.
data source comes in as
datasource: 76370da8-eeb1-44f9-b107-f0060a7de6d9 - 1 : A2
datasource: ef682a41-212f-46fe-a2f3-f9086af24b9a - 2 : B3
datasource: 4627d5a5-7297-4147-b21f-ec3ee464743c - 3 : C3
datasource: 4c453ab1-eb2d-4a0c-b328-ed51047050be - 4 : E1
datasource: 10eff5aa-20cd-4967-95db-aaf5d7a8dd81 - 5 : E1
datasource: ebd4b39b-8003-4a9b-8983-7e3a6bd3fc58 - 6 : A2
the grid row data is:
griddata: 76370da8-eeb1-44f9-b107-f0060a7de6d9 - 1 : A2
griddata: ef682a41-212f-46fe-a2f3-f9086af24b9a - 2 : B3
griddata: 4627d5a5-7297-4147-b21f-ec3ee464743c - 3 : C3
griddata: 4c453ab1-eb2d-4a0c-b328-ed51047050be - 4 : E1
griddata: ebd4b39b-8003-4a9b-8983-7e3a6bd3fc58 - 6 : A2
griddata: 10eff5aa-20cd-4967-95db-aaf5d7a8dd81 - 5 : E1
griddata: a9c11821-e6a3-48d0-a6b3-d38617ed16d2 - 7 :
applytransaction of removing all grid rows and adding the datasource back will updated the grid but I dont want an operation so heavy. I just want a simple reordering of these rows. Refreshcells does not work, nor do I want redrawRows for the same reason as the applyTransaction.
Any help or ideas would be greatly appreciated.
Solution 1:[1]
I just wanted to answer how I solved this. It may not be the best way, but it works for me. Hopefully it can help someone else.
After my data came back in the correct or and my grid didnt refresh that order, I called
gridApi.onSortChanged();
This fired a callback event on the grid postSort().
My Code looks like the following which did the sort I was looking for:
postSort: (rowNodes: RowNode[]) => {
function move(toIndex: number, fromIndex: number) {
rowNodes.splice(toIndex, 0, rowNodes.splice(fromIndex, 1)[0]);
}
const orderedUniqueIds = this.getCorrectOrderedIds()
var nextInsertPos = 0;
for (var i = 0; i < orderedUniqueIds.length; i++) {
let currentUniqueId = orderedUniqueIds[i];
let correctNodePosistion = findIndex(rowNodes, (n: RowNode) => {
return n.data.uniqueId === currentUniqueId;
})
if (correctNodePosistion > -1) {
move(correctNodePosistion, i)
nextInsertPos++;
}
}
console.log('post sort fired');
}`
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Jambione |
