开发者

How do you load jsTree using a variable for data

开发者 https://www.devze.com 2023-03-18 12:24 出处:网络
I have an ajax call to a wcf module that returns a menu tree in this format.(truncated) { \"data\" : [

I have an ajax call to a wcf module that returns a menu tree in this format. (truncated)

    { "data" : [
          {
          "data" : "Home", 
          "attr" : {"webpageid" : "1", "url"     : "/Intranet/index.html", "appcode" : "Intranet Home", "parent"  : "0", "enabled" : "1", "visible" : "1", "target"  : "_self     ", "order"   : "0", "title"   : "Home", "itmname" : "index", "submenuclass" : "", "htmlid" : "homenav", "opennewtab" : "true", "externalsite" : "0"},
          "children" :[
                        {
   开发者_StackOverflow社区                     "data" : "Site Administration", 
                        "attr" : {"webpageid" : "64", "url"     : "", "appcode" : "Mgmt/S0001", "parent"  : "1", "enabled" : "1", "visible" : "1", "target"  : "_self     ", "order"   : "1", "title"   : "Site Administration", "itmname" : "SiteAdmin", "submenuclass" : "", "htmlid" : "", "opennewtab" : "false", "externalsite" : "0"},
                        "children" :[
                        {
                        "data" : "Add Web Page", 
                        "attr" : {"webpageid" : "65", "url"     : "/Intranet/admin/mgmt/addwebpage.html", "appcode" : "Mgmt/S0002", "parent"  : "64", "enabled" : "1", "visible" : "1", "target"  : "_self     ", "order"   : "1", "title"   : "Add Web Page", "itmname" : "AddPage", "submenuclass" : "", "htmlid" : "", "opennewtab" : "false", "externalsite" : "0"} 
                        }
                      ]
          }
        ]

}, ... more data ... ]}

here is the call back function from my ajax call:

function fSucc(data) {
    $(function () {
        $('#webpagetree').jstree({
            "json_data": (function () { return data; })(),
            "ui": { "select_limit": 1 },
            "plugins": ["themes", "json_data", "ui", "themeroller", "dnd", "crrm"]
        }).bind("select_node.jstree", function (e, data) {
            alert(jQuery.data(data.rslt.obj[0], "jstree").id)
        });
    });
}

but it doesn't create or load an instance of the jsTree.

instead I get the error "Neither data nor ajax settings supplied." I've also tried putting the data into a global variable, then returning that.

Thanks for any help


Wouldn't you just want

$('#webpagetree').jstree({
    "json_data": data,
...

in your fSucc function?


Adding an eval() around the data will work.

"json_data": { "data" : eval(data) },


It works well by passing the variable containing the json in the function eval() :

$('#webpagetree').jstree({
    'core': {
        'data': eval(myJson)
    }
});
0

精彩评论

暂无评论...
验证码 换一张
取 消