首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Jqtree -订购入口

Jqtree -订购入口
EN

Stack Overflow用户
提问于 2014-11-05 17:13:09
回答 1查看 322关注 0票数 0

我正在使用jqtree插件来生成treeview。现在,我想通过标签为每个节点排序树。

这个插件没有排序功能,所以我认为我需要在加载数据之前对它进行排序。

这是我要分类的数据。

代码语言:javascript
复制
[
{
    "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": []
                        }
                    ]
                }
            ]
        }
    ]
}

]

非常感谢您的建议。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-11-05 17:35:58

您可以使用遍历树的递归函数和任何排序算法对它们进行排序。

代码语言:javascript
复制
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算法。它将结果记录到控制台进行检查。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26763111

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档