2016-02-29 143 views
0

我得到我的树通JSON文件和它looksd像获取子节点jsTree

root > parent1 > parent > child1 : child2 : child3 : etc 

我添加事件处理程序和一些儿童和点击我得到一些东西,如表,信息等等。问题是,当我点击child1时,我想从其他json文件中添加一个树分支。我接过例如,从jsTree文档

$('#tree').jstree({ 
'core' : { 
    'data' : { 
    'url' : function (node) { 
     return node.id === '#' ? 
     'ajax_roots.json' : 
     'ajax_children.json'; 
    }, 
    'data' : function (node) { 
     return { 'id' : node.id }; 
    } 
    } 
}); 

,并设置了我的代码...

var $industry = $('#industry'); 
$industry.jstree({ 
    "core" : { 
     "animation" : 0, 
     "check_callback" : true, 
     "themes" : { 
      "dot": true, 
      "stripes" : true, 
      "responsive": false 
     }, 
     'data' : { 
      'url' : function (node) { 
       return node.id === '#' ? 
        'data/industry.json' : 
        'data/SDTM.json'; 
      }, 
      'data' : function (node) { 
       return { 'id' : node.id }; 
      } 
     } 
    }}); 

然后我添加了事件处理上的孩子,我要合并

// ===== Click event on node ===== 
    for(var i = 0; i < data.selected.length; i++) { 
     var node = data.instance.get_node(data.selected[i]).text; 
     if (node == "SDTM 3.1.1"){ 
      //console.log(node); 
      $.jstree._reference($industry) 
       .create_node(
        'select_node.jstree', 'after', 
        { state: 'open', data: 'data/SDTM.json' } 
       );  } 
    } 
}) 
.jstree(); 

并在控制台我得到下一个错误

Uncaught TypeError: $.jstree._reference is not a function 

我做错了什么?

+0

这如何?你解决了吗? –

回答

1

我会做这种方式:

for (var i = 0; i < data.selected.length; i++) { 
    var node = data.instance.get_node(data.selected[i]), 
     nodeText = node.text; 

    if (nodeText == "SDTM 3.1.1") { 
     //console.log(node); 

     // get data for the branch, need to write code here 
     var branchJson = ... data/SDTM.json 
     /* 
     branchJson should contain an array of objects, 
     each object may have keys like below 

     { 
      text  : 'Node text' 
      children : [...], 
      state  : { 'open' } 
     } 

     */ 

     // iterate nodes in received json 
     branchJson.forEach(function(obj) { 
      data.instance.create_node(node, obj, 'after');  
     }) 

    } 
}