JsTree树不出来,从chrome console 看到
Uncaught TypeError: Cannot read property 'children' of undefined
问题出自 黄色部分
---------------------------------------Code---------------------------------------------
var url = "${pageContext.request.contextPath}/tree/queryRoomTree.html";
$("#jstree_demo")
.jstree({
"core" : {
"animation" : 0,
"check_callback" : true,
'force_text' : true,
"themes" : { "stripes" : true },
"check_callback" : true,
'data' : function (obj, callback) {
var jsonstr="[]";
var jsonarray = eval('('+jsonstr+')');
$.ajax({
type: "POST",
url:url,
dataType:"json",
async: false,
success:function(result) {
var arrays= result;
for(var i=0 ; i
"id":arrays[i].id,
"parent":arrays[i].pid=="root"?"#":arrays[i].pid,
"text":arrays[i].name
}
jsonarray.push(arr);
}
}
});
callback.call(this, jsonarray);
}
},
"types" : {
"#" : { "max_children" : 1, "max_depth" : 4, "valid_children" : ["root"] },
"root" : { "icon" : "dist/images/tree_icon.png", "valid_children" : ["default"] },
"default" : { "valid_children" : ["default","file"] },
"file" : { "icon" : "glyphicon glyphicon-file", "valid_children" : [] }
},
"plugins" : ["checkbox", "contextmenu", "dnd", "search", "state", "types", "wholerow" ]
});
----------------------------------------------------------------------------------Data Json from backend--------------------------------------------------------------------------------------------------------
[
{"id":"-1","name":"鍏ㄩ儴绌鸿皟","pid":"","icon":null,"iconSkin":"root","open":false,"isParent":true,"checked":false},
{"id":"A1","name":"A妤 ","pid":"-1","icon":null,"iconSkin":"A","open":false,"isParent":true,"checked":false},
{"id":"A2","name":"B妤 ","pid":"-1","icon":null,"iconSkin":"A","open":false,"isParent":true,"checked":false},
{"id":"A3","name":"C妤 ","pid":"-1","icon":null,"iconSkin":"A","open":false,"isParent":true,"checked":false},
{"id":"A4","name":"D妤 ","pid":"-1","icon":null,"iconSkin":"A","open":false,"isParent":true,"checked":false},
因为后台返回的数据 根节点的"pid":""(空),而黄色代码部分却是
"parent":arrays[i].pid=="root"?"#":arrays[i].pid,
Code改为
"parent":arrays[i].pid==""?"#":arrays[i].pid,
//区别是绿色部分,也就是真正的根节点pid是““,赋值为#