$("#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"));
});
In this above code I have created a jstree and upon click of a button with id #createIf_c I am adding a node with title "if" but as I want some more data to be associated with this node , I have added more attributes to it while creating the node. Next when I am trying to access this associated data, here "value" then I am getting the alert 'undefined'. So is there a different way for associating data with a node? or a different way of accessing the associated data of a node is jstree?..please help....
you can put your extra data in the JSON node.data this is not documented
Plz refer to the Author's answer.
You could edit info by $('#tree').jstree(true).get_node("some_node_id")
, and post the extra data as json by $('#tree').jstree(true).get_json("some_node_id")
.
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;
The simplest way to do this is just like adding an attribute to an html element i.e.,
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With