EasyUI Forum

General Category => EasyUI for jQuery => Topic started by: devnull on January 03, 2015, 05:38:31 PM



Title: tree resort by target node [Solved]
Post by: devnull on January 03, 2015, 05:38:31 PM
How can I resort ONLY the children of a particular tree node ?

I found this: http://www.jeasyui.com/forum/index.php?topic=2948.0 - but that reloads the entire tree which I don't want to do.

After adding a node to a tree, I want to resort it's siblings.




Title: Re: tree resort by target node
Post by: stworthy on January 04, 2015, 12:17:32 AM
The function below shows how to sort the tree nodes.
Code:
function sortNodes(tree, parentNode){
var t = $(tree);
var data = t.tree('getData', parentNode.target);
var nodes = $.extend(true, [], data.children||[]);
$.map(nodes, function(node){
t.tree('remove', node.target);
});
// sort the nodes
nodes.sort(function(n1,n2){
if (n1.text == n2.text){
return 0;
} else if (n1.text > n2.text){
return 1;
} else {
return -1;
}
});
// append the sorted nodes
t.tree('append', {
parent: parentNode.target,
data: nodes
});
}

Call the 'sortNodes' function to sort the children nodes of the selected node.
Code:
var t = $('#tt');	// the tree object
var selected = t.tree('getSelected');
if (selected){
sortNodes(t, selected)
}