2010-08-13 68 views
13

我正在尝试编写一些动态地将节点添加到jstree的代码。我按照文档http://www.jstree.com/documentation/crrm,但不能得到一个简单的例子工作 - 节点child2被添加,但它被添加到节点'root.id'而不是'child1.id'指定。 ..任何提示将不胜感激。代码如下以编程方式将子节点添加到jstree

<html> 
<head> 
<script type="text/javascript" src="http://static.jstree.com/v.1.0rc2/jquery.js"></script> 
<script type="text/javascript" src="http://static.jstree.com/v.1.0rc2/jquery.jstree.js"></script> 

<script type="text/javascript"> 
$(document).ready(function() { 
    $(function() { 
     $("#tree").jstree({ 
      "json_data" : { 
       "data" : [ 
        { 
         "data" : "parent", 
         "attr" : { "id" : "root.id" }, 
         "children" : [ { "data" : "child1", 
             "attr" : { "id" : "child1.id" }, 
             "children" : [ ] } 
            ] 
        }, 
       ] 
      }, 
      "plugins" : [ "themes", "json_data", "crrm" ] 
     }); 
    }); 
    $("#add").click(function() { 
     $("#tree").jstree("create", $("#child1.id"), "inside", { "data" : "child2" }, 
          function() { alert("added"); }, true); 
    }); 
}); 
</script> 
</head> 

<body> 

<div id="tree" name="tree"></div> 

<input type="button" id="add" value="add" /> 
</body> 
</html> 
+0

对我来说,“创建”操作不起作用......我sp明确的“create_node”,它的工作! – 2011-04-14 14:53:09

+0

对于它的价值,我无法让'创造'工作。虽然'create_node'确实起作用,我认为参数必须不同。如果他们的文档没有那么无用,那肯定会很好。你知道我在哪里可以找到'create_node'方法的文档吗? – Brad 2011-07-13 00:54:12

+0

http://www.jstree.com/documentation 转至核心文档超链接 – Ashwin 2012-04-20 11:34:14

回答

13

当使用ID的时期,你需要逃避他们,像这样:

$("#tree").jstree("create", $("#child1\\.id"), "inside", { "data" : "child2" }, 
          function() { alert("added"); }, true); 

这是因为它如何使用jQuery选择。它位于这里的jsTree常见问题解答中提到: http://www.jstree.com/faq/

+0

Ah-ha - 谢谢! – user419766 2010-08-16 16:51:52

+2

链接到答案中的常见问题已无效。 – Pang 2015-10-13 03:07:41

3

首先初始化jstree(在我的情况我使用AJAX),把check_callback为核心的obj和核心OBJ这样后调用插件:

jQuery('#jstree_demo_div').jstree({ 
    'core' : { 
      'data' : { 
      'url' : 'data/mapas.php', 

      }, 
      "check_callback" : function(e,data){ 
       console.log(data) 
      } 
     }, 
     "plugins" : [ "contextmenu" ] }) 

第二个使用这一行,并把$('#j1_1')作为父对象,数据在JSON中,'last'作为位置或'first',函数回调函数(在我的情况下是函数tales()),内部参数集合在true

jQuery("#jstree_demo_div").jstree(true).create_node($('#j1_1'), {text: "New node", id: true} , "last",tales(), true); 
相关问题