JsTree树不出来 报Uncaught TypeError: Cannot read property 'children' of undefined

来源:互联网 发布:网络显卡联机 编辑:程序博客网 时间:2024/06/06 00:27

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是““,赋值为#

阅读全文
0 0