Checkbox select_node.jstree event not working

3,646 views
Skip to first unread message

Thomas

unread,
Jun 5, 2010, 5:48:51 AM6/5/10
to jsTree
Hello!

Trying to get the select_node event to fire when checking a checkbox,
but nothing happens. Have read some discuttions but nothing helps for
me. Is something missing?



$("#tree").jstree({
"xml_data" : {
"data" : '................. xml here ...........',
"xsl" : "flat"
},
"plugins" : [ "themes", "xml_data", "ui", "checkbox" ]
});


$("#tree").bind("select_node.jstree", function(event, data) {
alert(data.inst.get_selected());
});

vakata

unread,
Jun 5, 2010, 6:40:57 AM6/5/10
to jsTree
Try binding to "change_state" for checkboxes.

Cheers,
Ivan

Thomas

unread,
Jun 6, 2010, 5:26:06 AM6/6/10
to jsTree
Thanks!

Is it possible to check if select_node is triggered by the check_node
method or by mouse click?

I want to save the tree after each change in the three, but now it
saves for each check_node call when I render the initial tree.

vakata

unread,
Jun 7, 2010, 8:36:12 AM6/7/10
to jsTree
Well, the change_state event cannot distinguish that ... the
select_node event can, but that won't work for you.
So you can listen for the "reselect" event, and start saving after
that has fired.

Cheers,
Ivan

Thomas

unread,
Jun 18, 2010, 1:10:01 PM6/18/10
to jsTree
Does not seem to work.

What would be the correct way to do what I want?

Now i use function that checks how long change_state has been idle, so
if it's not called for 0,1 second it saves. But that does not seem
very professional? But it works.

$("#tree").bind("change_state.jstree", function(event, data) {

// aktuellt id
//data.inst.get_selected().attr("id");

var categoriesArray = new Array();

$("#tree li.jstree-checked").each ( function () {
categoriesArray.push( $(this).attr("id") );
} );

$.idle(function() {



$.post("_ajax_updateTree.php",{
'categories[]' : categoriesArray,
'action' : 'categoriesupdate',
'products_id' : pid
});

}, 100);

vakata

unread,
Jun 19, 2010, 3:46:29 PM6/19/10
to jsTree
Well, I was thinking of something like this:

var save = false;
$("#tree")
.bind("reselect.jstree", function(event, data) { save = true; })
.bind("change_state.jstree", function(event, data) {
if(save) {
// do stuff
}
});

I guess it should work.

Cheers,
Ivan

Thomas

unread,
Jul 17, 2010, 5:40:28 AM7/17/10
to jsTree
It does not work, if i try this:

var save = false;
$("#tree")
.bind("reselect.jstree", function(event, data) { save = true; })
.bind("change_state.jstree", function(event, data) {

if(save)
{
alert('save');
}
});


It alert all the time when I init the tree.

On 19 Juni, 21:46, vakata <ivan.bozha...@gmail.com> wrote:
> Well, I was thinking of something like this:
>
> var save = false;
> $("#tree")
>   .bind("reselect.jstree", function(event, data) { save = true; })
>   .bind("change_state.jstree", function(event, data) {
>     if(save) {
>       // do stuff
>     }
>   });
>
> I guess it should work.
>
> Cheers,
> Ivan
>

> > > On 6 Юни, 12:26,Thomas<thomas.anders...@gmail.com> wrote:
>
> > > > Thanks!
>
> > > > Is it possible to check if select_node is triggered by the check_node
> > > > method or by mouse click?
>
> > > > I want to save the tree after each change in the three, but now it
> > > > saves for each check_node call when I render the initial tree.
>
> > > > On 5 Juni, 12:40, vakata <ivan.bozha...@gmail.com> wrote:
>
> > > > > Try binding to "change_state" for checkboxes.
>
> > > > > Cheers,
> > > > > Ivan
>

vakata

unread,
Jul 18, 2010, 12:38:03 AM7/18/10
to jsTree
Well, how many times do you init the tree? If more than one use:

.bind("init.jstree", function () { save = false; })

Otherwise the code should work - it is tested. If you have any more
problems, please paste your config & data here, or even better -
provide a live demo.

Kindest regards,
Ivan

Thomas

unread,
Jul 30, 2010, 3:16:41 PM7/30/10
to jsTree
The problem is when I use for example:

var save = false;
$("#tree")
.bind("init.jstree", function () { save = false; })
.bind("reselect.jstree", function(event, data) { save = true; })
.bind("change_state.jstree", function(event, data) {

if(save)
{
alert('save');
}
});

And then when I init the tree (as I call it) to check some boxes in an
already rendered tree with the following code, it saves for every
check_box. I only want it to save when I click with the mouse.

$("#tree").jstree("uncheck_all"); // first i clear the tree

// make a call to get some box id's to check
$.getJSON("_ajax_getCategoriesIds.php?pid="+pid,
function(data){
$.each(data, function(i,data){
$("#tree").jstree("check_node", '#'+data.catid); // here I
"initilise" the tree. Checking some boxes.
});
});


Hope you understand. I don't have anything checked in the beginning.

Here is my real init code:

$("#tree").jstree({
"xml_data" : {
"data" : '{ include_php file="tree_xml.php" }',
"xsl" : "flat"
},
"plugins" : [ "themes", "xml_data", "ui", "checkbox" ]
});


On 18 Juli, 06:38, vakata <ivan.bozha...@gmail.com> wrote:
> Well, how many times do you init the tree? If more than one use:
>
> .bind("init.jstree", function () { save = false; })
>
> Otherwise the code should work - it is tested. If you have any more
> problems, please paste your config & data here, or even better -
> provide a live demo.
>
> Kindest regards,
> Ivan
>

Thomas

unread,
Aug 1, 2010, 8:43:47 AM8/1/10
to jsTree
Solved it the easy way:

Added another check:

updateTree = true;

Before i run the "check_node" loop: $("#tree").jstree("check_node",
'#'+data.catid);

And then check for it also:

if(save && !updateTree)
{
alert(save);

Alex

unread,
Aug 7, 2010, 11:45:23 AM8/7/10
to jsTree
Is there a reason why select_node isn't triggered? Is it a bug, or
intentional?

> > > > > > > select_nodeeventcan, but that won't work for you.


> > > > > > > So you can listen for the "reselect"event, and start saving after
> > > > > > > that has fired.
>
> > > > > > > Cheers,
> > > > > > > Ivan
>
> > > > > > > On 6 Юни, 12:26,Thomas<thomas.anders...@gmail.com> wrote:
>
> > > > > > > > Thanks!
>
> > > > > > > > Is it possible to check if select_node is triggered by the check_node
> > > > > > > > method or by mouse click?
>

> > > > > > > > I want to save the tree after eachchangein the three, but now it


> > > > > > > > saves for each check_node call when I render the initial tree.
>
> > > > > > > > On 5 Juni, 12:40, vakata <ivan.bozha...@gmail.com> wrote:
>
> > > > > > > > > Try binding to "change_state" for checkboxes.
>
> > > > > > > > > Cheers,
> > > > > > > > > Ivan
>
> > > > > > > > > On 5 Юни, 12:48,Thomas<thomas.anders...@gmail.com> wrote:
>
> > > > > > > > > > Hello!
>

> > > > > > > > > > Trying to get the select_nodeeventto fire when checking a checkbox,

ChrisR

unread,
Aug 7, 2010, 11:55:49 AM8/7/10
to jsTree
Hi,

As a matter of fact, the 'select_node.jstree' event is triggered
everytime you select a node(s) and also if 'initially_select' option
is set (and the selecting node exists), unless you prevent it from
triggering in the 'before.jstree' event.

Best regards,
ChrisRaven

Alex

unread,
Aug 7, 2010, 1:32:00 PM8/7/10
to jsTree
I'm not sure it does?

jstree.bind('select_node.jstree', function(){alert("selected")})

where 'jstree' is the name of my element (and this works with other
binds).


That doesn't do anything? Am I doing it wrong?

ChrisR

unread,
Aug 7, 2010, 3:42:55 PM8/7/10
to jsTree
I use it a lot, and it works fine for me (no matter, if it is
initially select, select by 'select_node' function, or selecting by
mouse). If you don't have any errors that break the script before
binding the event, the only reasons I can say is that you don't have
'ui' plugin on your plugin list, or you don't select any node.
Maybe try to use:
console.log(jstree.jstree('get_selected').length);
Add it to some button and invoke when you think, you have selected
something.
Also maybe try to change that 'jstree' variable to something other
(for testing purposes).

Best regards,
ChrisRaven

Kheang

unread,
Aug 7, 2010, 6:02:24 PM8/7/10
to jsTree
If you guys are talking about checkbox and select_node here is what
i've read previously:

"also disables the select_node, deselect_node and
deselect_all functions."

I just did a test myself, and when you are using checkbox, even if you
have UI plugin enabled, select_node doesn't trigger.
Which seems normal in this case since using checkboxes you would
normally go with check_node, uncheck_node.

I hope this helps!
> > > > > > > > > > > > > >                                         "data" : '................. xml here- Hide quoted text -
>
> - Show quoted text -...
>
> read more »

ChrisR

unread,
Aug 7, 2010, 6:15:29 PM8/7/10
to jsTree
Indeed, if one is using the 'checkbox' plugin, those functions are
disabled. I've thought that Alex's last post was more general, not
related with the 'checkbox' plugin. Sorry, my mistake :)

Best regards,
ChrisRaven
> ...
>
> więcej »

Alex

unread,
Aug 8, 2010, 8:49:05 PM8/8/10
to jsTree
Is that just something that hasn't been implemented yet, or was taken
out for a reason?

I'm curious as to why, because select_node seems like a brilliant
event to trigger, it saves looping through the tree and give you more
granular control.
> > > > > > > > > > > > > Cheers,...
>
> read more »

ChrisR

unread,
Aug 8, 2010, 9:01:48 PM8/8/10
to jsTree
Hi,

According to docs: http://www.jstree.com/documentation/checkbox (just
beneath beginning of the API section) it was made purposely. Probably,
to allow checking nodes instead of selecting them - usually when there
is a tree with checkboxes, its purpose is to set some options, not
modify the tree itself, so 'select_node' looks unnecessary for the
first look. But if someone wants to make uncommon use of the jsTree,
then definitely it would be handy.

Best regards,
ChrisRaven
> ...
>
> więcej »
Reply all
Reply to author
Forward
0 new messages