gpt4 book ai didi

javascript - Jstree - 排序条目

转载 作者:行者123 更新时间:2023-12-03 11:30:16 33 4
gpt4 key购买 nike

我正在使用 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": []
}
]
}
]
}
]
}

]

非常感谢您的建议。

最佳答案

您可以使用迭代树的递归函数和任何类型的排序算法来对它们进行排序。

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
}
}
}

在此fiddle我用相当低效但易于实现的冒泡排序算法来做到这一点。它将结果记录到控制台以供检查。

关于javascript - Jstree - 排序条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26763111/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com