How to specify a href attribute when using json data?

2,692 views
Skip to first unread message

Kristaps

unread,
Oct 11, 2010, 4:52:14 AM10/11/10
to jsTree
Can someone show me example how to change href attribute when jstree
is populated from json data.

vakata

unread,
Oct 12, 2010, 4:14:01 PM10/12/10
to jsTree
Hi,

Instead of "data" : "some title" use:

"data" : { "title" : "some title", "attr" : { "href" : "value" } }

Kindest regards,
Ivan

Kristaps

unread,
Oct 12, 2010, 4:45:08 PM10/12/10
to jsTree
Thanks for your help vakata! Got it working now. Just one final, i
guess question. When i click on a link nothing happens, just a class
"jstree-clicked" set to that link. How can I overwrite default
functionality and force it to follow the link specified by href
attribute ?

vakata

unread,
Oct 12, 2010, 5:21:42 PM10/12/10
to jsTree
Just remove the UI plugin from your config (the plugins array) - then
all links will work fine.

If you want to do something more - keep the UI plugin and bind to the
"select_node.jstree" event:

$("container").bind("select_node.jstree", function (e, data) {
// data.rstl.obj is the object that was selected.
document.location.href = data.rslt.obj.children("a").attr("href");
});

Kindest regards,
Ivan

Kristaps

unread,
Oct 13, 2010, 4:16:52 AM10/13/10
to jsTree
Now i have following setup:
$("#sidebar").jstree({
"json_data" : {
"data" : <?=$node_tree_json?>
},
"ui" : {
"selected_parent_close" : "select_parent",
"initially_select" : [ "node_<?=$this->node->id?>" ]
},
"plugins" : [ "themes", "json_data", "ui" ]
}).bind("select_node.jstree", function (e, data) {
document.location.href =
data.rslt.obj.children("a").attr("href");
});
And the page is reloading all the time, i belive it is happening
because in settings to initally select a node...

vakata

unread,
Oct 15, 2010, 3:44:18 AM10/15/10
to jsTree
Yes, remove the initially select option, or simply process all the
selections after the first one:

$("#sidebar").jstree({ /*....*/ })
.bind("reselect.jstree", function (e, data) {
data.inst.get_container().bind("select_node.jstree", function (e,
data) {
document.location //...
})
})

Kindest regards,
Ivan

Reply all
Reply to author
Forward
0 new messages