How to associate a data to a node in jstree?

后端 未结 7 1946
温柔的废话
温柔的废话 2020-12-29 07:12
$(\"#ifTree\").jstree({
            \"plugins\" : [\"themes\",\"html_data\",\"ui\",\"crrm\"], 
            \"themes\" : {
                    \"theme\" : \"apple\",
         


        
7条回答
  •  不知归路
    2020-12-29 08:12

    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
    

提交回复
热议问题