2014-11-05 66 views
0

我正在使用jqtree插件来生成树视图。 现在我想通过标签为每个节点排序树。Jqtree - 排序entrys

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

这是我的数据进行排序。

[ 
{ 
    "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": [] 
         } 
        ] 
       } 
      ] 
     } 
    ] 
} 

]

非常感谢您的任何建议。

回答

1

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

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我做到了与宁效率不高,但很容易实现冒泡排序算法。它将结果记录到控制台进行检查。

+0

谢谢 - 这是我需要的 – 2014-11-07 08:06:39