Replacing an entire tree based with contents from a REST call

829 views
Skip to first unread message

Steve

unread,
Sep 26, 2010, 10:20:14 AM9/26/10
to jsTree
I'm building a very simple jstree with a checkbox plugin. The HTML
page on which this tree appears also has a simple HTML select box.
When a selection is made in that box, I need to make a web-service
call to a server and replace the jstree contents with new contents
from the web service. And I'm having some trouble getting it to work.

I've got the select binding working:
$("#topicSelector").change(function() {
getChartHeadings($("#topicSelector").val());
});
And the web service call is also working:
function getChartHeadings(fgid) {
$.get("getchartheadings.adp?fgid="+fgid, function(opts){
$("#topictree").jstree("create_node","#topictree","inside",opts);
$("#topictree").jstree("refresh");
});
}
But the tree is not being replaced. Is "create_node" the correct way
to reload a tree? The web service is returning an HTML list (ul and li
tags). I can post that data if it helps.

Thanks, Steve

vakata

unread,
Sep 28, 2010, 3:42:08 PM9/28/10
to jsTree
I believe it would be best to simply recreate the tree using the new
HTML:

$.get("getchartheadings.adp?fgid="+fgid, function(opts){
$("#topictree").jstree({ "html_data" : { "data" : opts }, /* rest of
your config */ });
});

Create node only creates ONE new node (it won't parse children) and it
also expects the data in a specific JSON format.

Kindest regards,
Ivan

Steve Carton

unread,
Sep 28, 2010, 7:58:11 PM9/28/10
to jst...@googlegroups.com
Thanks Ivan - that's how I'm handling it - I was hoping not to have to
reinitialize everything. But it works fine.

Steve

On 9/28/2010 3:42 PM, vakata wrote:
> I believe it would be best to simply recreate the tree using the new
> HTML:
>
> $.get("getchartheadings.adp?fgid="+fgid, function(opts){
> $("#topictree").jstree({ "html_data" : { "data" : opts }, /* rest of
> your config */ });
> });
>
> Create node only creates ONE new node (it won't parse children) and it
> also expects the data in a specific JSON format.
>
> Kindest regards,
> Ivan
>
>

vakata

unread,
Sep 29, 2010, 4:00:04 AM9/29/10
to jsTree
Sorry about that ...
Actually you do have another option - make jstree handle all the data
getting using ajax (html_data config).

All you have to do is call refresh when needed.
$("#topicSelector").change(function() {
$("#topictree").jstree("refresh");
});
$("#topictree").jstree({
html_data : {
ajax : {
url : "getchartheadings.adp"
data : function () { return { "fgid" : $
("#topicSelector").val() }; }
}
// rest of config


Sorry for not thinking of this yesterday :)

Kindest regards,
Ivan

On 29 Септ, 02:58, Steve Carton <st...@chesbay.net> wrote:
> Thanks Ivan - that's how I'm handling it - I was hoping not to have to
> reinitialize everything. But it works fine.
>
> Steve
>
> On 9/28/2010 3:42 PM, vakata wrote:
>
> > I believe it would be best to simply recreate the tree using the new
> > HTML:
>
> > $.get("getchartheadings.adp?fgid="+fgid, function(opts){
> >    $("#topictree").jstree({ "html_data" : { "data" : opts }, /* rest of
> > your config */ });
> > });
>
> > Create node only creates ONE new node (it won't parse children) and it
> > also expects the data in a specific JSON format.
>
> > Kindest regards,
> > Ivan
>
> > On 26 , 17:20, Steve<st...@chesbay.net>  wrote:
Reply all
Reply to author
Forward
0 new messages