wolfgang
wolfgang

Reputation: 7819

how to use set_id to rename node_id in jstree?

this is my rename function, none of the options i've tried work, i do know, that set_id is the function to use but how?

 $('#jstree').on('rename_node.jstree', function (node,obj) {

    var node_id = "calculated"// calculate node_id
    // tried the following 3 options ..
   ....

     $('#jstree').jstree(true).set_id(obj,node_id); //not working

     obj.instance.set_id(this,node_id)// not working either
     obj.instance.set_id(obj,node_id)//nope..

So how do i set the node_id in jstree?

Upvotes: 5

Views: 9885

Answers (4)

oerl
oerl

Reputation: 1224

I looked at the API http://www.jstree.com/api/#/?q=rename&f=rename_node.jstree, and I think you have to use obj.node.

$('#jstree').jstree(true).set_id(obj.node,node_id);

obj.text should contain the new name of the node, and obj.old the old name of the node.

EDIT: The link to the API documentation does not match the code example.

Here are the correct links:

  1. To set the ID of a node: https://www.jstree.com/api/#/?f=set_id(obj,%20id)
  2. To rename the node (set the text value): https://www.jstree.com/api/#/?f=rename_node(obj,%20val)

Upvotes: 12

Sallo Szrajbman
Sallo Szrajbman

Reputation: 1

Just set property id on the element you´re using to create the nodes.

<div id="jstree">
<ul>
    @foreach($feelings as $feeling)
        <li id="{{{$feeling->id}}}">{{{$feeling->name}}}
            <ul>
                @foreach($feeling->children as $feeling_children)
                    <li id="{{{$feeling_children->id}}}">{{{$feeling_children->name}}}
                        <ul>
                            @foreach($feeling_children->children as $feeling_children2)
                                <li id="{{{$feeling_children2->id}}}">{{{$feeling_children2->name}}}
                                    <ul>
                                        @foreach($feeling_children2->children as $feeling_children3)
                                        <li id="{{{$feeling_children3->id}}}">{{{$feeling_children3->name}}}
                                            <ul>
                                                @foreach($feeling_children3->children as $feeling_children4)
                                                <li id="{{{$feeling_children4->id}}}"> {{{$feeling_children4->name}}}</li>
                                                @endforeach
                                            </ul>
                                        </li>
                                        @endforeach
                                    </ul>
                                </li>
                            @endforeach
                        </ul>
                    </li>
                @endforeach
            </ul>
        </li>
    @endforeach
</ul>

Upvotes: 0

Pierre de LESPINAY
Pierre de LESPINAY

Reputation: 46228

The first parameter you called node is actually the event object

Example creating a node then updating his autogenerated id

$('#jstree').on('create_node.jstree', function (e, obj) {
    #...
    $(this).jstree(true).set_id(obj.node, 42);
}

Upvotes: 2

Prosto Trader
Prosto Trader

Reputation: 3527

Try the following inside your on.() function:

 $(this).attr("id", node_id);

Upvotes: 0

Related Questions