Sunday 15 January 2012

javascript - Jqtree - ordering entrys -



javascript - Jqtree - ordering entrys -

i using jqtree plugin generate treeview. want sort tree every node via label.

the plugin comes without sort function think need sort before load info it.

this info sort.

[ { "label": "a label", "id": "201", "children": [ { "label": "c label", "id": "40773", "children": [ { "label": "f label", "id": "222460", "children": [] }, { "label": "c label", "id": "222469", "children": [] }, { "label": "x label", "id": "27512", "children": [ { "label": "f label", "id": "143546", "children": [] }, { "label": "d label", "id": "141341", "children": [ { "label": "g label", "id": "222456", "children": [] }, { "label": "l label", "id": "222457", "children": [] }, { "label": "x label", "id": "222443", "children": [ { "label": "z label", "id": "222447", "children": [] }, { "label": "a label", "id": "222446", "children": [] } ] }, { "label": "l label", "id": "222455", "children": [] } ] }, { "label": "a label", "id": "143547", "children": [ { "label": "b label", "id": "222458", "children": [] } ] }, { "label": "r label", "id": "143548", "children": [] } ] } ] } ] }

]

many suggestions.

you can utilize recursive function iterates through tree , sort of sorting algorithm sort them.

recursivesortbylabel(arr); //arr = array tree info function recursivesortbylabel(obj){ if(obj.length > 0){ for(var in obj){ if(obj[i].children.length > 0) recursivesortbylabel(obj[i].children); sortalgorithm(obj); //any sorting algorithm } } }

in fiddle did rather unefficient easy implement bubblesort algorithm. logs result console inspectation.

javascript sorting jqtree

No comments:

Post a Comment