JsTree - Get selected node in load.jstree file

How can I get the selected node in the load.jstree event?

What should I do in the event handler:

$('#Tree').bind('loaded.jstree', function(event, data){//TODO: How to get the selected node here?}).jstree(); 

By the way, I found out that the arg object of the data object contains the get_selected () function, but could not get anything from it.

My goal is to redirect the client to the currently selected node (by the attribute 'url').

Thank you in advance

+4
source share
2 answers

It seems according to the documentation of the demo:

http://www.jstree.com/demo

You can do:

 .one("reselect.jstree", function (event, data) { }); 

or

 .bind("select_node.jstree", function (event, data) { // `data.rslt.obj` is the jquery extended node that was clicked alert(data.rslt.obj.attr("id")); }) 

Read the documentation carefully as:

one is used, this is because if refresh is called, these events fire

 // 1) if using the UI plugin bind to select_node .bind("select_node.jstree", function (event, data) { // `data.rslt.obj` is the jquery extended node that was clicked alert(data.rslt.obj.attr("id")); }) // 2) if not using the UI plugin - the Anchor tags work as expected // so if the anchor has a HREF attirbute - the page will be changed // you can actually prevent the default, etc (normal jquery usage) .delegate("a", "click", function (event, data) { event.preventDefault(); }) 

For the last event, delegate instead of writing event.preventDefault(); you can do your redirection correctly if you are not using the user interface plugin and write: window.location = $(this).attr('href');

+2
source

you can select the current node:

 $('#' + data.node.id) 

The code becomes:

 $('#Tree').bind('loaded.jstree', function(event, data){ console.log($('#' + data.node.id)); //This is current node, see on console }).jstree(); 
0
source

Source: https://habr.com/ru/post/1379998/


All Articles