JsTree树不出来 报Uncaught TypeError: Cannot read property 'children' of undefined
2018-02-27 09:54
1266 查看
1、 问题
JsTree树不出来,从chrome console 看到Uncaught TypeError: Cannot read property 'children' of undefined
2、code 和后台json数据
问题出自 黄色部分---------------------------------------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<arrays.length; i++){
var arr = {
"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},
3. 解决办法
因为后台返回的数据 根节点的"pid":""(空),而黄色代码部分却是 "parent":arrays[i].pid=="root"?"#":arrays[i].pid,Code改为 "parent":arrays[i].pid==""?"#":arrays[i].pid,
//区别是绿色部分,也就是真正的根节点pid是““,赋值为#
相关文章推荐
- JsTree树不出来 报Uncaught TypeError: Cannot read property 'children' of undefined
- tankgame2.js:37 Uncaught TypeError: Cannot read property 'direct' of undefined(…)
- Uncaught TypeError: Cannot read property 'length' of undefined
- 浏览器报Uncaught TypeError: Cannot read property 'trigger' of undefined解决方案
- extjs4:Uncaught TypeError: Cannot read property 'processed' of undefined
- Uncaught TypeError: Cannot read property 'msie' of undefined
- uploadify的JS组件报错:Uncaught TypeError: Cannot read property 'queueData' of undefined
- Datatables 之Uncaught TypeError: Cannot read property 'mData' of undefined
- background.js Uncaught TypeError: Cannot read property 'xxx' of undefined
- jQuery mobile报错 Uncaught TypeError: Cannot read property 'concat' of undefined
- aes.js:77 Uncaught TypeError: Cannot read property 'extend' of undefined(…)
- Uncaught TypeError: Cannot read property 'msie' of undefined
- Uncaught TypeError: Cannot read property 'length' of undefined
- Uncaught TypeError: Cannot read property 'decimalSeparator' of undefined
- Uncaught TypeError: Cannot read property 'length' of undefined
- 关于Uncaught TypeError: Cannot read property 'toLowerCase' of undefined的问题
- 使用form的方法(例如:reset,clear等)报错Uncaught TypeError: Cannot read property 'tip' of undefined(…)的原因
- bug宝典之bootstrap switch Uncaught TypeError: Cannot read property 'apply' of undefined
- util.js:68 Uncaught TypeError: Cannot read property 'fn' of undefined
- dataTables Uncaught TypeError: Cannot read property 'style' of undefined