Selecting the First/Root Node of a Tree 0rc

8,412 views
Skip to first unread message

jcb

unread,
Jun 4, 2010, 11:11:27 AM6/4/10
to jsTree
I found initially_select and that works fine. But in my case the ID
of the first/root node can change.

A user can destroy and completely recreate the tree. I load nodes via
xml from my database and track parent ID along with the ID.

So initially the first node might have an ID of 1 but after the user
deletes and creates a few trees it might have an ID of 8.

Is there a way to select the first/root node after a refresh?

ChrisR

unread,
Jun 4, 2010, 2:05:35 PM6/4/10
to jsTree
Hi,

Maybe you should use something like this:

$('#tree_id').jstree('select_node', 'ul > li:first');

Of course, if you're using some LI-nodes inside UL-nodes in another
place on your website, then the jQuery selector should be more
complicated to select first li inside ul, which is in your tree.

Cheers,
ChrisRaven

jcb

unread,
Jun 4, 2010, 2:42:20 PM6/4/10
to jsTree
Thanks Chris, that works fine when I put it in a clickable anchor.

I'm hoping to have it select the first node after the tree loads.
Like
right after a refresh. I tried:

.bind("loaded.jstree", function (event, data) {
$('#ntree').jstree('select_node', 'ul > li:first');
})

but it didn't work.

jcb

unread,
Jun 4, 2010, 3:07:16 PM6/4/10
to jsTree
Mistyped, this is what I tried.

.bind("loaded.jstree", function (event, data) {
data.inst.select_node('ul > li:first');
})

Ivan Bozhanov

unread,
Jun 4, 2010, 3:48:36 PM6/4/10
to jstree
Try binding to "reselect.jstree" instead :) Sorry about that :)

Cheers,
Ivan

> --
> 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.
>
>

jcb

unread,
Jun 4, 2010, 4:28:04 PM6/4/10
to jsTree
Worked like a charm!

Thanks

carlos...@gmail.com

unread,
May 6, 2019, 5:41:47 AM5/6/19
to jsTree
9 years later, and this worked perfectly! (maintaining and old jstree version)
Reply all
Reply to author
Forward
0 new messages