2015-10-06 123 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; 
    } 
}