$("#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”的节点,但由于我想要更多的数据与这个节点相关联,我添加了更多创建节点时的属性。接下来,当我尝试访问此关联数据时,此处为“值”,然后我收到警报“未定义”。那么将数据与节点关联有不同的方法吗?或者访问节点的相关数据的另一种方式是jstree?..请帮助....
答案 0 :(得分:16)
您可以将您的额外数据放入JSON node.data 这未记录
答案 1 :(得分:13)
Plz参考作者的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;
答案 2 :(得分:4)
执行此操作的最简单方法就像向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
答案 3 :(得分:2)
将数据与节点关联的正确方法如下:
如果要添加更多数据,即属性,则在“attr”属性
下提及所有属性(名称,值)“ attr ”:{attributeName1:“attributeValue1”,attributeName2:“attributeValue2”......}
$("#createIf_c").click(function () {
$("#ifTree").jstree("create",null,"inside",
{ "data" : "testNodeName",
"attr": { title:"if",value:"expression"} }, function() {}, true);
});
答案 4 :(得分:1)
使用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);
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
答案 5 :(得分:0)
关联HTML定义中的数据:
如果要使用树的HTML定义关联数据,请使用:
<li id="treesiteadmin-serverStatus" data-ic='{"form":"site.serverstatus"}' data-jstree='{"icon":"glyphicons glyphicons-server"}'>Stato del server</li>
&#34;数据&#34;当前所选节点的属性为:
{"jstree":{"icon":"glyphicons glyphicons-server"},"ic":{"form":"site.serverstatus"}}
答案 6 :(得分:0)
div id="tree_1" class="tree-demo">
<ul>
<li data-jstree='{ "opened" : false }' id="c67"> Sıcak İçecekler
<ul>
<li data-jstree='{ "type" : "file", "selected" : false }' id="500">Çay</li>
<li data-jstree='{ "type" : "file", "selected" : false }' id="501">Fincan Çay</li>
</ul>
</li>
<li data-jstree='{ "opened" : true }' id="c68"> Soğuk İçecekler
<ul>
<li data-jstree='{ "type" : "file", "selected" : true }' id="514">Ayran</li>
<li data-jstree='{ "type" : "file", "selected" : true }' id="515">Kola</li>
<li data-jstree='{ "type" : "file", "selected" : true }' id="516">Meyveli Gazoz</li>
</ul>
</li>
</ul>
</div>
如果您想通过<ul>
和<li>
元素来解决它,此html非常适合自定义ID。