2011-10-27 27 views
3

我有一個使用json數據格式的jsTree。 加載根節點集很好。如何將json變量作爲子節點添加使用「jstree」jquery插件 - no ajax

我的問題是如何將子節點附加到被單擊的父節點。

任何幫助將不勝感激。

謝謝!

$("#tree-cat1") 
    .bind("open_node.jstree", function (event, data) { 
     console.log(data.rslt.obj.attr("id")); 
     //eval(loadChild()); 
     //at this point i need to append the result of loadChild() to the tree under the relevant node 
    }) 
    .jstree({ 
     "json_data": { 
      "data": eval(loadRoot()) 
     }, 
     "themes": {"theme": "classic","dots": true,"icons": true}, 
     "plugins": ["themes", "json_data", "ui"] 
    }) 

function loadRoot() { 
    return "[{'data':'A node','state':'closed','attr':{'id':'A'}}]"; 
} 
function loadChild() { 
    return "[{'data':'A1 node','attr':{'id':'A1'}}]"; 
} 
+0

做你想做的每一個節點將被打開時添加的孩子嗎?爲什麼你不使用'兒童'屬性追加樹初始化 – Irishka

+0

每次節點擴展時我運行oracle查詢來獲取數據(全部在JavaScript中)。數據返回到一個js變量。然後,我必須在樹中的相關點處填充返回數據的樹(根據代碼示例) – lance

回答

2

看到文檔的位置:jsTree docu

編輯

這裏是代碼,您需要更改網址到你的目的地,嘗試

HTML:

<div id="tree-cat1"></div> 

js:

$("#tree-cat1").jstree({ 
    "plugins": ["themes", "json_data", "ui"], 
    "themes": {"theme": "classic","dots": true,"icons": true}, 
    "json_data": { 
      //root elements 
     "data": [{"data":'A node',"state":'closed',"attr":{"id":'A'}}], 
     "ajax": { 
      "type": 'POST', 
      "data": {"action": 'getChildren'}, 
      "url": function (node) { 
       var nodeId = node.attr('id'); //id="A" 

       return 'yuorPathTo/GetChildrenScript/' + nodeId; 
      }, 
      "success": function (new_data) { 
       //where new_data = node children 
       //e.g.: [{'data':'A1 node','attr':{'id':'A1'}}, {'data':'A2 node','attr':{'id':'A2'}}] 
       return new_data; 
      } 
     } 
    } 
}); 

舊零件
類似的東西,將填充開節點有孩子,如果尚未:

... 
    "json_data": { 
      //root elements 
     "data": [{"data":'A node',"state":'closed',"attr":{"id":'group_A'}}], 
     "ajax": { 
      "type": 'POST', 
      "data": {"action": act.GET_GROUPREPORTS}, 
      "url": function (node) { 
       var nid = node.attr('id'); //id="group_A" 
       nid = nid.substr(nid.lastIndexOf('_')+1); 

       return module.getDBdata_path + nid; 
      }, 
      "success": function (data) { 
       var rid, new_data = data; 

       if (typeof data[0] === 'undefined') { 
        new_data = []; 
        for (rid in data) { 
         if(data.hasOwnProperty(rid)) { 
          new_data.push({"data": data[rid], 
           "attr": {"id": 'rprefix_'+rid, 
             "title": ' ', 
             "rel": 'report', 
             "href": module.repView_path+rid 
           } 
          }); 
         } 
        } 
       } 
       return new_data; 
      } 
     } 
    }, ... 
+0

感謝Irishka,但我仍然無法使其工作。我嘗試使用ajax對象的建議,但在某處我失去了一些東西。如果您有時間可以使用我的示例作爲解決方案的基礎?如果我們得到這個工作,我會欠你很多時間! – lance

+0

查看更改,我已編輯答案 – Irishka

相關問題