【问题标题】:jstree : Uncaught TypeError: Cannot read property 'children' of undefinedjstree:未捕获的类型错误:无法读取未定义的属性“孩子”
【发布时间】:2014-09-30 19:04:39
【问题描述】:

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

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

我通过 Ajax 调用将其显示在我的视野中。 Console.log 向我展示了详细信息,但 jsTree 给了我错误:

未捕获的类型错误:无法读取未定义的属性 'children'

查看:

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

    }
});

【问题讨论】:

  • 您的页面上是否有 ID 为 jstree_demo_div 的元素?用它添加 html 标记。

标签: javascript jquery ajax jstree jstree-search


【解决方案1】:

我正在使用 Ajax。

我通过声明一个包含(id、父级、文本)的新对象 javaScript 解决了这个问题

示例:

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

我声明了一个数组,我在其中推送所有对象并将其提供给“数据”,就像这样

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

它运行良好!我希望它能帮助很多人

【讨论】:

  • 那么您的示例中的 Array 是什么?是objJS吗?
  • 背后的逻辑是什么?
猜你喜欢
  • 2012-11-22
  • 1970-01-01
  • 1970-01-01
  • 2021-12-22
  • 2015-01-06
  • 2017-07-26
  • 1970-01-01
  • 2019-02-26
相关资源
最近更新 更多