jstree : Uncaught TypeError: Cannot read property 'children' of undefined

六眼飞鱼酱① 提交于 2019-12-22 07:24:07

问题


In my server I am returning a JSON object in the format of jsTree :

{"id":"value", "text":"value", "parent":"value"} 

I am getting it in my view through an Ajax call. Console.log shows me the details but jsTree gives me the error:

Uncaught TypeError: Cannot read property 'children' of undefined

View:

$.ajax({
    url: "/category",
    dataType: 'json',
    type: 'GET',
    success: function (res) {
        $.each(res, function (i, obj) {
            products.push([obj.id, obj.parent, obj.text]);
            $('#jstree_demo_div').jstree({
                'core': {
                    'data': [{ "id": obj.id, "parent": obj.parent != 0 ? obj.parent : "#", "text": obj.text }]
                }
            });
            console.log(obj.parent != 0 ? obj.parent : "#");
        });

    }
});

回答1:


I'm working with Ajax .

the problem I resolved it by declaring a new object javaScript that contains( id, parent, text)

exemple :

 var objJS = new Object(); 
 objJS .id = ObjectJason.id;
 objJS .parent = ObjectJason.parent!=="0" ?  ObjectJason.parent:"#";
 objJS .text = ObjectJason.text;

I declare an Array where I push all my objects and give it to 'data', like this

 $('#jstree_demo_div').jstree({
                'core': {
                    'data': Array ;
                }
            });

and it's working perfectly! I wish it will help a lot of people



来源:https://stackoverflow.com/questions/25184210/jstree-uncaught-typeerror-cannot-read-property-children-of-undefined

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!