2015-10-06 110 views
1

我一直在试图创建一个简单的页面,并在其上创建一个允许创建新节点的jstree。我实现了创建树,我可以在页面中看到它,但是当我尝试创建一个新节点时,由于创建节点的指令,我只是得到一个“false”。在Jstree中创建新节点

我在网上看过一些例子,但是找不到问题。

有人可以帮助我吗?

这里整个代码

<!DOCTYPE html> 
<html> 
    <head> 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> 
     <title></title> 
     <meta charset="utf-8" /> 
     <meta name="viewport" content="width=device-width, initial-cale=1.0"> 
     <link rel="stylesheet" href="content/style.css" /> 
     <script src="script/jquery-1.11.3.min.js" type="text/javascript"></script> 
     <script src="script/jstree.min.js" type="text/javascript"></script> 
    </head> 
    <body> 
     <div id="jstree-div"></div> 
    </body> 

    <script type="text/javascript"> 
      $('#jstree-div').jstree({ 
       'core': { 
        'data': [ 
         'Simple root node', 
         { 
          'text': 'Root node 2', 
          'state': { 
           'opened': true, 
           'selected': true 
          }, 
          'children': [ 
           { 'text': 'Child 1' }, 
           'Child 2' 
          ] 
         } 
        ] 
       }, 
       'plugins': ['contextmenu'], 
       'contextmenu': { 
        'items': function($node) { 
         var tree = $("#jstree-div").jstree(true); 
         return { 
          "Create": { 
           'label': 'Crear', 
           "action": function (data) { 
            var ref = $.jstree.reference(data.reference); 
            sel = ref.get_selected(); 
            if (!sel.length) { return false; } 
            sel = sel[0]; 
            sel = ref.create_node(sel, { "text": "New node" }, 'last'); 
            if (sel) { 
             ref.edit(sel); 
            } 
           } 
          } 
         } 
        } 
       } 
      }); 
    </script> 
</html> 

回答

1

的问题是,你有没有允许修改,在你的配置结构,增加check_callback选项:

'core': { 
    'check_callback' : true, 
    'data': [ 
0

你的方法和引用似乎比我的,什么我的节点创建嵌入AJAX调用和各少许不同。不过,我已经下调什么对我的作品是什么,我认为应适用于您的设置:

contextmenu : { 
    items : function (node) { 
     var tmp = $.jstree.defaults.contextmenu.items(); 
     tmp.create.action = function (data) { 
      var inst = $.jstree.reference(data.reference), 
      obj = inst.get_node(data.reference); 
      inst.create_node(obj, {type: "item", text: "some text"}, "last", function (new_node) { 
       new_node.state = { 
        is_draggable: true, 
        opened: true, 
        disabled: false, 
        loaded: true 
       }; 
      }); 
     } 
     return tmp; 
    } 
}