2012-05-01 21 views
2

からツリーデータを作成します。jqTree - <a href="http://mbraak.github.com/jqTree" rel="nofollow">http://mbraak.github.com/jqTree</a></p> <p>を私はJSONからツリーデータを作成して助けが必要 - 私はjqTreeを使用してツリーを表示しようとしているJSON

私のJSONデータは次のようになります。

{ 
"d" : { 
"results": [ 
    { 
     "Title": "Committee A", 
     "ReportsToId": null, 
     "Rank": 1, 
     "Id": 5 
    }, 
    { 
     "Title": "Committee B", 
     "ReportsToId": 5, 
     "Rank": 2, 
     "Id": 7 
    }, 
    { 
     "Title": "Committee C", 
     "ReportsToId": 7, 
     "Rank": 3, 
     "Id": 13 
    }, 
    { 
     "Title": "Committee D", 
     "ReportsToId": 13, 
     "Rank": 4, 
     "Id": 1 
    }, 
    { 
     "Title": "Committee E", 
     "ReportsToId": 13, 
     "Rank": 4, 
     "Id": 3 
    } 
] 
} 
} 

私はこれで終わるしたい:

var treeData = [ 
    { 
     label: 'Committee A', 
     children: [ 
      { 
       label: 'Committee B', 
       children: [ 
       { 
        label: 'Committee C', 
        children: [ 
         { label: 'Committee D' }, 
         { label: 'Committee E' } 
        ] 
       }] 
      } 
      ] 
    } 
    ]; 

答えて

2

ここhttp://www.jqwidgets.com/populating-jquery-tree-with-json-data/から適応ソリューションです:

var jqTreeData = function (data) { 
     var source = []; 
     var items = []; 
     // build hierarchical source. 
     for (i = 0; i < data.length; i++) { 
      var item = data[i]; 
      var title = item["Title"]; 
      var reportsToId = item["ReportsToId"]; 
      var id = item["Id"]; 

      if (items[reportsToId]) { 
       var item = 
       { 
        label: title 
       }; 

       if (!items[reportsToId].children) { 
        items[reportsToId].children = []; 
       } 

       items[reportsToId].children[items[reportsToId].children.length] = item; 
       items[id] = item; 
      } 
      else { 
       items[id] = 
       { 
        label: title 
       }; 

       source[0] = items[id]; 
      } 
     } 
     return source; 
    } 
+0

あなたがしてください見ることができます私の質問? http://stackoverflow.com/questions/11742463/how-to-format-json-in-rails-controller – byCoder

+0

@ dm80私のコードを助けてくれますか? http://jsfiddle.net/elkk/ts9v17xt/ – user525146

関連する問題

 関連する問題