PKU
PKU

Reputation: 315

Select jsTree node without triggering select_node event

I have a jsTree which when the user clicks on a node, it triggers the select_node event. However, I also have a list containing some entries with the id of the nodes in the jsTree. On selecting an entry in this list, I need to only select the node from the tree but without firing the select_node event. As per the documentation, select_node api has 3 parameters

https://www.jstree.com/api/#/?q=(&f=select_node(obj [, supress_event, prevent_open])

obj: mixed an array can be used to select multiple nodes
supress_event: Boolean if set to true the changed.jstree event won't be triggered
prevent_open: Boolean if set to true parents of the selected node won't be opened

However, it does not talk about suppressing the select_node event. Any suggestions?

Upvotes: 2

Views: 4110

Answers (3)

Sriram Sridharan
Sriram Sridharan

Reputation: 740

You may use the following; it works for me

$('#TreeDiv').jstree("select_node", actualId, true);

According to the jsTree official documentation, the select_node method accepts a Boolean value as the second argument which indicates whether the corresponding event should be suppressed. See this page.

Upvotes: 2

Tobberoth
Tobberoth

Reputation: 9527

I don't know if there's a better way to pull it off, but here's how I did it:

Instead of using the select_node function, I wrote a function which takes the ID of the node and then opens it, sets it as selected, and then recursively open the parent nodes.

$('#TreeDiv').jstree(true).open_node(actualId);
$('#TreeDiv').jstree(true).get_node(actualId).state.selected = true;
var parent = $('#TreeDiv').jstree(true).get_parent(actualId);
while (parent.length > 0) {
    $('#TreeDiv').jstree(true).open_node(parent);
    parent = $('#TreeDiv').jstree(true).get_parent(parent);
}

Upvotes: 2

karim
karim

Reputation: 311

I see two options here:

  1. Temporarily disable your select_node event handler while you manually select a node from code.
  2. Use the changed event instead of the select_node event and use the supress flag.

Upvotes: 4

Related Questions