2015-02-10 18 views
0

我想創建一個TreeView數組以供在Angular UI樹中使用。我想從物體的陣列這樣創建結構:在Angular中創建對象的treeview數組

0:對象 體:空 categoryDe​​scription:空 categoryTopic:空 描述:空 faqBodyId:0 faqCategoryId:0 faqGroupId:1 groupDescription:空 組名: 「Generelt」 groupTopic: 「Generelt」 themeCode: 「GEN」 話題:空

1:對象的身體:空categoryDe​​scription: 「測試AF駕駛員學校」 categoryTopic:「郵件「description:null faqBodyId:0 faqCategoryId:2 faqGroupId:1 group描述:null組名:null組合主題:null主題:null : 「測試」 faqBodyId:3 faqCategoryId:2 faqGroupId:0 groupDescription:空組名:空groupTopic:空themeCode:空話題: 「Test123」

等等

這是三個層次。組,類別和身體 節點並不總是有一個孩子!

我想它在這個結構三個層次:

[ 
    { 
    "id": 1, 
    "title": "1. dragon-breath", 
    "items": [] 
    }, 
    { 
    "id": 2, 
    "title": "2. moiré-vision", 
    "items": [ 
     { 
     "id": 21, 
     "title": "2.1. tofu-animation", 
     "items": [ 
      { 
      "id": 211, 
      "title": "2.1.1. spooky-giraffe", 
      "items": [] 
      }, 
      { 
      "id": 212, 
      "title": "2.1.2. bubble-burst", 
      "items": [] 
      } 
     ] 
     }, 
     { 
     "id": 22, 
     "title": "2.2. barehand-atomsplitting", 
     "items": [] 
     } 
    ] 
    }, 
    { 
    "id": 3, 
    "title": "3. unicorn-zapper", 
    "items": [] 
    }, 
    { 
    "id": 4, 
    "title": "4. romantic-transclusion", 
    "items": [] 
    } 
] 

Structure

如何與角碼做了什麼?

+0

那些看起來像標準JavaScript JSON陣列給我,但你的示例數據需要在這裏格式化,這很難理解這應該代表什麼。 – Claies 2015-02-10 14:13:53

+0

這是JSON格式..對不起,我沒有提到這一點。這裏的編輯把它全部拉到一起..這有什麼竅門嗎? – 2015-02-10 14:53:05

+0

根據您鏈接的文檔,Angular JS UI樹本身支持JSON數據。你有沒有嘗試你的數據,並有一些意外的結果? – Claies 2015-02-10 22:36:33

回答

0

我解決了這個問題。 輸入數組進行排序:

function buildtree(arr) { 
     var group = []; 

     angular.forEach(arr, function (value, index) { 
      if (value.faqGroupId > 0 && value.faqCategoryId == 0 && value.faqBodyId == 0) { 
       var category = []; 
       var faqgroup = { 
        "id": value.faqGroupId, 
        "title": value.groupTopic, 
        "description": value.groupDescription, 
        "name": value.groupName, 
        "items": category 
       } 
       angular.forEach(arr, function (valuecat, index) { 
        if (value.faqGroupId == valuecat.faqGroupId && valuecat.faqCategoryId > 0 && valuecat.faqBodyId == 0) { 
         var body = []; 
         var faqcat = { 
          "id": valuecat.faqCategoryId, 
          "title": valuecat.categoryTopic, 
          "description": valuecat.categoryDescription, 
          "items": body 
         } 
         angular.forEach(arr, function (valuebod, index) { 
          if (valuecat.faqGroupId == valuebod.faqGroupId && valuecat.faqCategoryId == valuebod.faqCategoryId && valuebod.faqBodyId > 0) { 
           var faqbody = { 
            "id": valuebod.faqBodyId, 
            "title": valuebod.topic, 
            "description": valuebod.description, 
            "body": valuebod.body, 
           } 
           body.push(faqbody); 
          } 
         }) 
         category.push(faqcat); 
        } 
       }) 
       group.push(faqgroup); 
      } 
     }); 

     return group; 
    } 
0

陣列列表:

平面對象數組列表

var dataList= [ 
    {'id':1 ,'parentid' : 0, 'label'="label 1"}, 
    {'id':4 ,'parentid' : 2 ,'label'="label 2"}, 
    {'id':3 ,'parentid' : 1, 'label'="label 3"}, 
    {'id':5 ,'parentid' : 0, 'label'="label 4"}, 
    {'id':6 ,'parentid' : 0, 'label'="label 5"}, 
    {'id':2 ,'parentid' : 1, 'label'="label 6"}, 
    {'id':7 ,'parentid' : 4, 'label'="label 7"}, 
    {'id':8 ,'parentid' : 1, 'label'="label 8"} 
    ]; 

隱蔽扁平物體陣列列表以樹形目錄列表

function flatListToTreeViewData(dataList) { 
    var tree = [], 
     mappedArr = {}, 
     arrElem, 
     mappedElem; 

    for (var i = 0, len = dataList.length; i < len; i++) { 
     arrElem = dataList[i]; 
     mappedArr[arrElem.id] = arrElem; 
     mappedArr[arrElem.id]['children'] = []; 
    } 

    for (var id in mappedArr) { 
     if (mappedArr.hasOwnProperty(id)) { 
      mappedElem = mappedArr[id]; 

      array of children. 
      if (mappedElem.parentID) { 
       mappedArr[mappedElem['parentID']]['children'].push(mappedElem); 
      }else { 
       tree.push(mappedElem); 
      } 
     } 
    } 
    return tree; 
}