Start Building Professional
Web Apps Today


 
Categories Question details Back To List
Question  posted by Scott Brown on Jan 18, 2009 23:54
open dhtmlx forum
dhtmlxTree sorting question

I have a sizeable tree (600+ nodes) that I am loading and setting "tree.enableSmartXMLParsing(1);" and I have an onClick node event to sort child nodes of the opened node however I would like to know how I can sort only those nodes that have children then in the same branch sort the leafs ?

Parent Node
[+] elephant
[+] lion
[+] tiger
[+] zebra
|-- bear
|-- giraffe

So the branch nodes get sorted separatly from the leafs ?

Make sense ? Hope you can help ?

Cheers
S
Answer posted by Support on Jan 19, 2009 01:57
You can use custom sorting function 

tree.setCustomSortFunction(function(idA,idB){

var childA=tree.hasChildren(idA);
var childB=tree.hasChildren(idB);
//separate items with child and childless
if (childA && !childB) return 1;
if (!childA && childB) return -1;

//sorting inside groups
return (tree.getItemText(idA)>tree.getItemText(idB)?1:-1);

}
Answer posted by Scott on Jan 21, 2009 00:34
Hi there !
Perfect solution and great responsiveness ! Congratulations on a great product and keep up the good work !

Only thing I've done is switch states on returns

From...
if (childA && !childB) return 1;
if (!childA && childB) return -1;

To...
if (!childA && childB) return 1;
if (childA && !childB) return -1;

This put the folders first in the ordering and orders both branches and leafs...