I have successfully created a dynamically rendered tree, all i just want now is to get the tree nodes sorted alphabetically on init. Cannot find much details in the documentation on how to do that. Can anyone help ?
@gomedh could you not just sort your nodes array alphabetically before passing it to the tree component as an input?
At that point you have all the control over your tree nodes and child nodes - so you can sort them in whichever way works for you.
this.AllNodes = tempNode.sort(function (name1, name2) {
if (name1.name < name2.name) {
return -1;
} else if (name1.name > name2.name) {
return 1;
} else {
return 0;
}
});
//you can use any of node property for sorting like name, id
I have successfully created a MatNestedTree. I want to use pagination for the same. I didn't get anything regarding pagination on material site. Can anyone help ?
That would be very useful for trees using nested set model ^^
Closing this issue due to inactivity. Please open a new issue if the problem still exists.
this.AllNodes = tempNode.sort(function (name1, name2) {
if (name1.name < name2.name) {
return -1;
} else if (name1.name > name2.name) {
return 1;
} else {
return 0;
}
});//you can use any of node property for sorting like name, id
This sort works for one level only. To sort the whole tree on several levels, considering that the sort is based on the property code, I did like this:
createTree(data) {
let tree = [];
// Here build up the tree, which is at that moment unsorted
// Parse every node of the unsorted tree
this.parseNodes(tree);
return tree;
}
parseNodes(obj) {
for (const elmt of obj) {
// If the element of the array has a property _children_, we sort the childrens, then parse them
if (elmt.children !== undefined) {
elmt.children = this.sortTree(elmt.children);
this.parseNodes(elmt.children);
}
}
}
sortTree(tree) {
return tree.sort(function (name1, name2) {
if (name1.code < name2.code) {
return -1;
} else if (name1.code > name2.code) {
return 1;
} else {
return 0;
}
});
}