2017-08-10 62 views
0

我在我的MongoDB- database中有以下文檔。此文檔代表我的用戶界面中的文件夾子文件夾通過superFolderId引用父文件夾。Java - 在後端存儲樹結構以在前端查看它

我將在我的angularJS UI中查看此樹。我現在的問題是如何將此樹結構存儲在後端列表中,以便在AngularJS Frontend中顯示此結構。

我會很高興在這個方向的任何提示。

{ 
"_id" : ObjectId("598c43aa76eba30a8ab373ac"), 
"name" : "Folder1", 
"created" : ISODate("2017-08-10T11:29:41Z"), 
"projectId" : "598c425d76eba30a8ab373a6", 
"superFolderId" : "", 
} 
{ 
"_id" : ObjectId("598c43b176eba30a8ab373ad"), 
"name" : "Folder1_1", 
"created" : ISODate("2017-08-10T11:29:49Z"), 
"projectId" : "598c425d76eba30a8ab373a6", 
"superFolderId" : "598c43aa76eba30a8ab373ac", 
} 
{ 
"_id" : ObjectId("598c43f676eba30a8ab373ae"), 
"name" : "Folder1_1_1", 
"created" : ISODate("2017-08-10T11:30:58Z"), 
"projectId" : "598c425d76eba30a8ab373a6", 
"superFolderId" : "598c43b176eba30a8ab373ad", 
} 

回答

0

您不需要在服務器端存儲樹。足夠的(關於你的例子)_id記錄是一個superFolderId其他記錄。

一旦你對客戶端的記錄列表,你可以很容易地將其轉換爲樹:

function _makeTree(options) { 
    var children, e, id, o, pid, path, path_obj, temp, _i, _len, _ref; 
    id = options.id || "id"; 
    pid = options.parent_id || "parent_id"; 
    children = options.children || "children"; 
    path = "path"; 
    path_obj = "path_obj"; 

    temp = {}; 
    o = []; 
    _ref = options.q; 
    for (_i = 0, _len = _ref.length; _i < _len; _i++) { 
     e = _ref[_i]; 
     e[children] = []; 
     e[path_obj] = []; 
     temp[e[id]] = e; 

     if (temp[e[pid]] != null) { 

      e[path] = temp[e[pid]].path + '/' + e.name; // generates read-only String path to node 

      e[path_obj] = _.clone(temp[e[pid]].path_obj); 

      e[path_obj].push({name: e.name, id: e.id, org_id: e.org_id}); 

      temp[e[pid]][children].push(e); 
     } else { 
      e[path] = e.name; 
      e[path_obj].push({name: e.name, id: e.id, org_id: e.org_id}); 
      o.push(e); 
     } 
    } 
    return o; 
}; 

**我把它從我的項目,使一些多餘的東西,你可以刪除

Demo in fiddle


之後,你可以渲染TR ee像我貼HERE或只是谷歌它︰

+0

太棒了!非常感謝!!!你幫了我很多! – quma