How to initialize jsTree using JSON

781 views
Skip to first unread message

Nico

unread,
Mar 3, 2014, 12:25:12 PM3/3/14
to jst...@googlegroups.com
I try to initialize the nodes in jstree with json data that I recovered from controller, but nothing happens

Here's my html code

<div class="span10">
    <div id="jstree" class="span4">
    </div>
</div>

Here's my js code

$('#jstree').jstree({
    'core' : {
                // so that create works
                "check_callback" : true,
             },
     "json_data": { "data" : eval(getJson()) },
    'plugins' : ["contextmenu","dnd","json_data"]
});


function getJson(){
    var json;
     $.ajax({
      url: 'createJSON',
      type: 'POST',
      async:true,         
      success: function(data) {
          json = data;
      },
      error: function(e) {
        console.log("error");
      }
    });
    return json;
}

From controller i get this json data

{"id":"ipo","parent":"#","text":"ipo","children":"[]"}

So why
eval(getJson()) doesn't remplace with json data ?

Ivan Bozhanov

unread,
Mar 4, 2014, 3:24:09 AM3/4/14
to jst...@googlegroups.com
Why are you using 1.0 code with the 3.0 format? I suspect you are using 3.0, right?
There is no json_data property in 3.0, you need to configure like so:
$('#jstree').jstree({
    'core' : {
                "check_callback" : true,
                "data" : eval(getJson())
    },
    'plugins' : ["contextmenu","dnd"]
});

Nico

unread,
Mar 4, 2014, 4:01:39 AM3/4/14
to jst...@googlegroups.com
You're right, i'm using 3.0. I mixed the version.
I get your correction but still it doesn't work. I've debugged the result on mozzila with Firebug.
Indeed it doesn't add json data, though it gets well from controller (server).
Do i need refresh jstree ?

Ivan Bozhanov

unread,
Mar 4, 2014, 4:39:47 AM3/4/14
to jst...@googlegroups.com
use only:
data: getJson()

no need to eval from what I see.
Also - remove the children property it is wrong anyway (a string is not a valid value), and warp everything in an array:
[{"id":"ipo","parent":"#","text":"ipo"}]

Best regards,
Ivan
Reply all
Reply to author
Forward
0 new messages