jstree:未捕获的TypeError:无法读取undefined的属性'children' [英] jstree : Uncaught TypeError: Cannot read property 'children' of undefined

查看:607
本文介绍了jstree:未捕获的TypeError:无法读取undefined的属性'children'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的服务器中,我以jsTree的格式返回一个JSON对象:

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

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

我收到它在我看来通过Ajax调用。 Console.log 向我显示详细信息,但jsTree给了我错误:

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:无法读取未定义的属性'children'

Uncaught TypeError: Cannot read property 'children' of undefined

查看:

$.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 : "#");
        });

    }
});


推荐答案

我正在使用Ajax。

问题我通过声明一个包含(id,parent,text)的新对象javaScript解决了这个问题

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

例如:

 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

这篇关于jstree:未捕获的TypeError:无法读取undefined的属性'children'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆