【发布时间】:2015-04-07 18:19:24
【问题描述】:
我正在使用带有 Json 的 jstree。我的问题是我可以将 Json 直接提供到 jsTree 数据中,但是当我将它作为变量传递时,它会将整个 Json 字符串打印为一个节点的标题。
下面是nodes.json
{
"id": "ajson1",
"parent": "#",
"text": "Simple root node"
}, {
"id": "ajson2",
"parent": "#",
"text": "Root node 2"
}, {
"id": "ajson3",
"parent": "ajson2",
"text": "Child 1"
}, {
"id": "ajson4",
"parent": "ajson2",
"text": "Child 2"
},
下面的代码有效
request = $.getJSON("nodes.json");
var data;
request.complete(function() {
data = request.responseText;
console.log(data);
$.jstree.defaults.core.themes.responsive = true;
$('#tree').jstree({
plugins: ["checkbox", "sort", "types", "wholerow", "search"],
"types": {
"file": {
"icon": "jstree-file"
}
},
'core': {
'data': [{
"id": "ajson1",
"parent": "#",
"text": "Simple root node"
}, {
"id": "ajson2",
"parent": "#",
"text": "Root node 2"
}, {
"id": "ajson3",
"parent": "ajson2",
"text": "Child 1"
}, {
"id": "ajson4",
"parent": "ajson2",
"text": "Child 2"
}, ]
}
});
});
但是下面这个不行
request = $.getJSON("nodes.json");
var data;
request.complete(function() {
data = request.responseText;
console.log(data);
$.jstree.defaults.core.themes.responsive = true;
$('#tree').jstree({
plugins: ["checkbox", "sort", "types", "wholerow", "search"],
"types": {
"file": {
"icon": "jstree-file"
}
},
'core': {
'data': [data]
}
});
});
如果您想查看代码执行,我将其托管在我的域中。 http://www.jordanmclemore.com/projects/jstree/test/visual/current.html
【问题讨论】:
标签: javascript jquery json jstree