我正在使用jqtree插件来生成treeview。现在,我想通过标签为每个节点排序树。
这个插件没有排序功能,所以我认为我需要在加载数据之前对它进行排序。
这是我要分类的数据。
[
{
"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": []
}
]
}
]
}
]
}]
非常感谢您的建议。
发布于 2014-11-05 17:35:58
您可以使用遍历树的递归函数和任何排序算法对它们进行排序。
recursiveSortByLabel(arr); //arr = the array with your tree data
function recursiveSortByLabel(obj){
if(obj.length > 0){
for(var i in obj){
if(obj[i].children.length > 0)
recursiveSortByLabel(obj[i].children);
sortAlgorithm(obj); //any sorting algorithm
}
}
}在这个小提琴中,我使用了相当低效率但易于实现的bubblesort算法。它将结果记录到控制台进行检查。
https://stackoverflow.com/questions/26763111
复制相似问题