I am trying to persist the order of Nodes in the tree using react-sortable-tree (https://github.com/frontend-collective/react-sortable-tree), for Example
- Label 1
- Label 2
- Label 3
Changing the order of Label 3 to Label 2 in the following way and also passing the treeIndex,
- Label 1
- Label 3
- Label 2
Upon setting the updated state and reloading the page tree structure does not follow treeIndex order.
I have tried the following using the utils of react sortable tree (https://github.com/frontend-collective/react-sortable-tree/blob/master/src/utils/tree-data-utils.js)
I have tried converting my current tree to flat structure using getFlatDataFromTree() and then making the treeIndex changes that are required and then converting the flat data back to tree structure using the same util library with getTreeFromFlatData() but the sequence with respect to treeIndex is not considered.
I already have tree data stored in treeData,
here labelNodes contain the path of each node of the tree. So I am looping over them and assigning the correct order.
Code:
flatTree.forEach(treeNode => {
labelNodes.forEach(element => {
if (treeNode.node.id === element.id) {
treeNode.path = element.path;
treeNode.treeIndex = element.treeIndex;
}
return true;
});
});
const getParentKey = node => {
if (node.parentNode) {
return node.parentNode.id;
}
return 0;
};
const sortedFlatTree = flatTree.sort((a, b) => a.treeIndex - b.treeIndex);
const treeConversion = getTreeFromFlatData({
flatData: sortedFlatTree,
getParentKey,
});
treeConversion does not maintain the sorted order.