$("#ifTree").jstree({
"plugins" : ["themes","html_data","ui","crrm"],
"themes" : {
"theme" : "apple",
"dots" : true,
"icons" : false
}
});
$("#createIf_c").click(function () {
$("#ifTree").jstree("create",null,"inside", { "data" :{ title:"if",value:"expression"} },
function() {}, true);
});
$("#display").click(function(){
var a = $.jstree._focused().get_selected();
alert(a.attr("value"));
});
在上面的代码中,我创建了一个jstree,在单击id为#createIf_c的按钮时,我添加了一个标题为"if“的节点,但由于我想要更多的数据与此节点相关联,所以在创建节点时,我向其添加了更多的属性。下一步,当我试图访问这个相关数据时,这里是"value“,然后我就会得到一个”undefined“的警告。那么,有没有不同的方法将数据与节点相关联呢?或者,访问节点相关数据的另一种方式是jstree?..please help...
发布于 2012-05-29 21:50:42
实现这一点的最简单方法就像向html元素添加属性一样,
var node = $.jstree._focused().get_selected(); //get the selected node or which ever you want the data to be associated with
node.attr("expression","xyz"); //add an attribute (name,value) here, name-expression and value-xyz
发布于 2014-10-27 17:54:18
请参考作者的answer。
你可以通过$('#tree').jstree(true).get_node("some_node_id")
编辑信息,并通过$('#tree').jstree(true).get_json("some_node_id")
以json格式发布额外的数据。
You can add anything you want to the data object. Like:
{ "id" : "some_node_id" "text" : "some node", ... "data" : { "foo" : "bar", "example_array" : ["1",2,true], "obj" : {"asdf":"asdf1"} } ...
And later on you can retrieve it like so:
$('#tree').jstree(true).get_node("some_node_id").data.obj.asdf; // this would equal "asdf1"
$('#tree').jstree(true).get_node("some_node_id").data.example_array; // this would be an array: ["1",2,true]
Setting other values is just as simple - you are working with a normal object:
$('#tree').jstree(true).get_node("some_node_id").data.obj.asdf = "some other value";
$('#tree').jstree(true).get_node("some_node_id").data.example_array = "I do not want this an array anymore";
$('#tree').jstree(true).get_node("some_node_id").data.obj.new_property = 1024;
发布于 2018-08-06 03:38:42
使用jstree v3,您可以使用插件关联属性,如下所示:
// create instance
var inst = $("#tree-id").jstree();
// create node definition
var node = {
id: "new_node_id",
text: "New Node",
li_attr: { "data-files": "false" },
a_attr: { "data-url": "some_url" }
};
// create node
var newNodeId = inst.create_node("#", node);
节点参数的预期格式(来自源注释):
// Expected format of the node (there are no required fields)
//{
// id: "string" // will be autogenerated if omitted
// text: "string" // node text
// icon: "string" // string for custom
// state: {
// opened: boolean // is the node open
// disabled: boolean // is the node disabled
// selected: boolean // is the node selected
// },
// children: [] // array of strings or objects
// li_attr: { } // attributes for the generated LI node
// a_attr: { } // attributes for the generated A node
//}
以及create_node
参数的预期格式:
// create_node(par, node, pos, callback, is_loaded)
// par (object) - the parent node (to create a root node use "#" (string) or `null`)
// node (object) - the data for new node (valid JSON object, or a simple string with the name)
// pos (object) - index to insert the node, "first" and "last" are supported, default is "last"
// callback (function) - a function to be called once the node is created
// is_loaded (boolean) - internal argument indicating if the parent node was succesfully loaded
// returns (string) - the ID of the newly create node
https://stackoverflow.com/questions/10030279
复制相似问题