您的位置:首页 > Web前端 > JavaScript

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是““,赋值为#
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐