Preventing users from selecting parent nodes

1,996 views
Skip to first unread message

ernest

unread,
Jun 10, 2010, 10:15:25 AM6/10/10
to jsTree
I have created a tree in which the user is only able to select one
node (using "ui : { select_limit : 1 },") but I want him only to be
able to select the 'leaves' i.e. the lowest level nodes of the tree.
If he selects a 'parent' node, I would like that node to toggle open
or closed. Does anybody have any ideas?

Regards
Ernest

vakata

unread,
Jun 10, 2010, 10:35:51 AM6/10/10
to jsTree
Hi,

Just use the before callback to prevent the select on non leaf nodes.

$("#tree").bind("before.jstree", function (e, data) {
if(data.func === "select_node" && !data.inst.is_leaf(data.args[0]))
{
data.inst.toggle_node(data.args[0]);
e.stopImmediatePropagation();
return false;
}
});

Note that you need the latest commit to use is_leaf, if you have the
regular download use:
!data.args[0].closest("li").hasClass("jstree-leaf")

Also make sure you set the UI config option selected_parent_close to
'false' or apply some logic to make sure you do not get in a loop.

Cheers,
Ivan

ernest

unread,
Jun 10, 2010, 11:12:53 AM6/10/10
to jsTree
Thanks Ivan.
I'm still having a bit of trouble. I've used your suggested code but
now none of the nodes can be selected! Could it be that the
"before.jstree" event interferes with the "select_node" event?

Ernest

ernest

unread,
Jun 10, 2010, 11:44:15 AM6/10/10
to jsTree
I'm using Firebug to help me catch errors in my script. Firebug says
"data.inst.is_leaf is not a function" and if I use the other line of
code i get: "data.args[0].closest is not a function".

Ivan Bozhanov

unread,
Jun 10, 2010, 4:45:10 PM6/10/10
to jstree
Sorry about that - use $(data.args[0]).closest

> --
> You received this message because you are subscribed to the Google Groups "jsTree" group.
> To post to this group, send email to jst...@googlegroups.com.
> To unsubscribe from this group, send email to jstree+un...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/jstree?hl=en.
>
>

ernest

unread,
Jun 11, 2010, 3:09:35 AM6/11/10
to jsTree
Thanks. It's working perfectly.
Cheers,
Ernest

On Jun 10, 10:45 pm, Ivan Bozhanov <ivan.bozha...@gmail.com> wrote:
> Sorry about that - use $(data.args[0]).closest
>
> > You received this message because you are subscribed to the Google Groups "jsTree" group.> To post to this group, send email tojs...@googlegroups.com.> To unsubscribe from this group, send email tojstree+u...@googlegroups.com.

Ovidiu C.

unread,
Aug 1, 2014, 7:30:33 AM8/1/14
to jst...@googlegroups.com
Hi. Is this still achievable in the latest version? There doesn't seem to be any 'before.jstree' event anymore.

Ivan Bozhanov

unread,
Aug 1, 2014, 10:03:13 AM8/1/14
to jst...@googlegroups.com
There is conditionalselect (an example plugin) in "src/misc.js"

Best regards,
Ivan

Ovidiu C.

unread,
Aug 1, 2014, 10:48:13 AM8/1/14
to jst...@googlegroups.com
Excellent. Thanks!
Reply all
Reply to author
Forward
0 new messages