How to associate a data to a node in jstree?
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