How to associate a data to a node in jstree?

北慕城南 提交于 2019-11-30 04:54:23
mohammed barqawi

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

Proper way to associate a data to node is as follows:

If you are adding more data i.e. attribute then mentioned all attributes (name, value) under "attr" property

"attr":{ attributeName1:"attributeValue1", attributeName2:"attributeValue2"...... }

 $("#createIf_c").click(function () { 
 $("#ifTree").jstree("create",null,"inside",
   { "data" : "testNodeName", 
      "attr": { title:"if",value:"expression"} },  function() {}, true);
});

Using jstree v3, you can associate attributes using the plugin like so:-

// 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);

The expected format of the node parameter (from the source comments):

// 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
//}

and the expected format of the create_node parameters:

// 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

To associate data from HTML definition:

If you want to associate data using HTML definition of the tree, use:

<li id="treesiteadmin-serverStatus" data-ic='{"form":"site.serverstatus"}' data-jstree='{"icon":"glyphicons glyphicons-server"}'>Stato del server</li>

The "data" property of currently selected node will be:

{"jstree":{"icon":"glyphicons glyphicons-server"},"ic":{"form":"site.serverstatus"}}

See result - "data" property of selected node

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>

this html is very suitable for custom ids if you want to solve it by <ul> and <li> elements.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!