How can I get the selected node on the loaded.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 event data arg object contains a function called get_selected() but couldn't get anything from it.
My purpose is to redirect the client to the current selected node (by 'url' attribute).
Thanks in advance
How can I get the selected node on the loaded.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 event data arg object contains a function called get_selected() but couldn't get anything from it.
My purpose is to redirect the client to the current selected node (by 'url' attribute).
Thanks in advance
Share Improve this question asked Nov 7, 2011 at 13:34 Yair NevetYair Nevet 13k17 gold badges69 silver badges109 bronze badges 2- 2 You don't want to use the 'select_node.jstree' event to do it ? – Guillaume Cisco Commented Nov 7, 2011 at 13:41
- Hi, do you have any reference or code example? – Yair Nevet Commented Nov 7, 2011 at 13:48
2 Answers
Reset to default 2Seems according to the documentation of the demo here :
http://www.jstree./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 carefully the documentation as :
one is used, this is because if
refresh
is called those events are triggered
// 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 make your redirection correctly if you're not using the UI plugin, and write : window.location = $(this).attr('href');
you can select current node by :
$('#' + data.node.id)
Code bees:
$('#Tree').bind('loaded.jstree', function(event, data){
console.log($('#' + data.node.id)); //This is current node, see on console
}).jstree();