在我的服务器中,我返回一个 jsTree 格式的 JSON 对象:
{"id":"value", "text":"value", "parent":"value"}
我通过 Ajax 调用在我的 View 中获取它。 Console.log
向我显示详细信息,但 jsTree 给我错误:
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解决了这个问题
例子:
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 ;
}
});
而且效果很好!希望对很多人有帮助
关于javascript - jstree : Uncaught TypeError: Cannot read property 'children' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25184210/