13

我正在尝试编写一些将节点动态添加到 jstree 的代码。我已经关注了http://www.jstree.com/documentation/crrm上的文档,但无法获得一个简单的示例——正在添加节点 child2,但它正在添加到节点“root. id' 而不是指定的 'child1.id' ...任何提示将不胜感激。代码如下

<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>
4

2 回答 2

14

在 ID 中使用句点时,您需要像这样转义它们:

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

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

于 2010-08-14T05:58:09.647 回答
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()),内部参数设置在真的

jQuery("#jstree_demo_div").jstree(true).create_node( $('#j1_1'), {text: "New node", id: true} , "last",tales(), true );
于 2014-03-21T20:17:39.393 回答