Javascript 以编程方式将子节点添加到jstree
我试图编写一些代码,动态地将节点添加到jstree中。我已经在上跟踪了文档,但无法获得一个简单的示例——正在添加节点child2,但正在将其添加到节点“root.id”,而不是指定的“child1.id”。。。任何提示都将不胜感激。代码如下Javascript 以编程方式将子节点添加到jstree,javascript,jstree,Javascript,Jstree,我试图编写一些代码,动态地将节点添加到jstree中。我已经在上跟踪了文档,但无法获得一个简单的示例——正在添加节点child2,但正在将其添加到节点“root.id”,而不是指定的“child1.id”。。。任何提示都将不胜感激。代码如下 <html> <head> <script type="text/javascript" src="http://static.jstree.com/v.1.0rc2/jquery.js"></script>
<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>
$(文档).ready(函数(){
$(函数(){
$(“#树”).jstree({
“json_数据”:{
“数据”:[
{
“数据”:“父项”,
“attr”:{“id”:“root.id”},
“children”:[{“data”:“child1”,
“attr”:{“id”:“child1.id”},
“儿童”:[]}
]
},
]
},
“插件”:[“主题”、“json_数据”、“crrm”]
});
});
$(“#添加”)。单击(函数(){
$(“#tree”).jstree(“创建”、$(“#child1.id”)、“内部”、{“数据”:“child2”},
函数(){alert(“added”);},true);
});
});
在ID中使用句点时,您需要这样转义:
$("#tree").jstree("create", $("#child1\\.id"), "inside", { "data" : "child2" },
function() { alert("added"); }, true);
这是因为它如何使用jQuery选择器。这里的jsTree常见问题解答中提到了这一点:
首先初始化jstree(在我的例子中,我使用ajax),将check_回调放入core obj,并在core 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 );
对我来说,“创建”操作不起作用。。。我指定了“创建_节点”,它成功了!不管它值多少钱,我也无法让
create
工作。虽然create\u node
确实有效,但我认为参数必须不同。如果他们的文档不是那么没用,那就太好了。你知道我在哪里可以找到关于create\u节点
方法的文档吗?转到核心文档超链接到常见问题解答的答案已失效。