2017-07-04 197 views
1

如何在JStree中创建新节点时添加属性?在JSTree中创建新节点时添加属性

当btnadd点击那个时候我正在添加新的节点。 节点创建成功,但现在我想添加属性。

我的HTML

<div id="jstree"> 
</div> 

<button id="btnadd">create node</button> 

<div id="CurentNodeName"></div> 
<div id="CurentNodeId"></div>  

脚本

<script> 
    var CurrentNode; 
    $(function() { 
     var data = [ 
      { "id": "ajson1", "parent": "#", "text": "Simple root node" }, 
      { "id": "ajson2", "parent": "#", "text": "Root node 2" }, 
      { "id": "ajson3", "parent": "ajson2", "text": "Child 1" }, 
      { "id": "ajson4", "parent": "ajson2", "text": "Child 2" }, 
     ]; 
     $("#jstree").jstree({ 
      "core": { 
       // so that create works 
       "check_callback": true, 

       "data": data 
      } 
     }).on('create_node.jstree', function (e, data) { 
      console.log('saved'); 
     }); 

     // listen for event 
     $('#jstree').on('changed.jstree', function (e, data) { 
      var i, j, r = [], s = []; 
      for (i = 0, j = data.selected.length; i < j; i++) { 
       r.push(data.instance.get_node(data.selected[i]).text); 
       CurrentNode = data.selected[0]; 
       console.log(data.selected[0]); 

      } 

      $('#CurentNodeName').html('Selected: ' + r.join(', ')); 
      $('#CurentNodeId').html('Selected Id: ' + CurrentNode); 

     }).jstree(); 

     // create the New Node when Button Click 
     $("#btnadd").on("click", function() { 

      $('#jstree').jstree().create_node(CurrentNode, { "id": "ajson5" + CurrentNode, "text": "newly added" }, "last", function() {     
      }); 
    }); 

</script> 

我也尝试 “ATTR” 是:{title: “如果”,值: “表达”}}但是这不能帮助我。

+0

尝试'数据[I] .attr = {标题:“如果”,值:“表达”}' – manuzi1

回答

0

试试这种方法。它的工作对我来说

$('#jstree').jstree().create_node(CurrentNode, { 
    li_attr: { "path": "path" },"text": "name" }, 
    "last", function() { }, true); 
+0

CurrentNode是我当前的节点,所以请借此 – 2017-07-05 05:16:54

+0

确定感谢名单的照顾..他为我工作,我正在寻找一整天 –