jsTree JSON issue [closed]

房东的猫 提交于 2019-12-04 21:31:34

I got the solution and is working fine now. Thanks you all for showing interest.

var new_avatar = new Array();
new_avatar = data.attributeSets;

var dirn = {};
var final_child = {};
var nodeChildren = {};

//Collect children for each node
for (var info in new_avatar) {

final_child = "[";
for (var detailInfo in new_avatar[info].attributes) {
    ckey = detailInfo; // Key
    cval = new_avatar[info].attributes[detailInfo]; // Key => Value
    final_child += '{ "data": "' + ckey + '",';
    final_child += ' "attr": { "rel":"directory" },';
    final_child += ' "children": [ "' + cval + '" ] },';
}
final_child = final_child.substring(0, final_child.length - 1);     //removing last comma so it would be a valid JSON
final_child += " ] ";   //final close to this JSON
nodeChildren[info] = final_child;
}

    // Attached collected nodes to respective parents
    dirn = "[";
    for (var info in new_avatar) {
        dirn += '{ "data": "' + new_avatar[info].name + '" ,';
        dirn += ' "attr": { "rel":"directory" }, ';
        dirn += ' "children": ' + nodeChildren[info] + " }, ";     //putting final_child in so it will buld level 2 + level 3 (level 3 child is just value declared in final_child children above)
    }
    dirn = dirn.substring(0, dirn.length - 2);      
    dirn += " ] ";  

    dirn = $.parseJSON(dirn);   

$("#tree2").jstree({
    plugins: ['themes', 'json_data', "ui", "dnd"],
    json_data: {

     "data": dirn,   

     },
    themes: {
     theme: 'default'
    },
    checkbox: {
     real_checkboxes: true,
     two_state: true
    },
    "types": {
     "types": {
         "disabled": {
             "check_node": false,
             "uncheck_node": false
         },
         "directory": {
             "check_node": false,
             "uncheck_node": false
         }
     }
    }
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!