用 JSP 中的 JSON 填充 JSTree
Populate JSTree with JSON from JSP
我正在尝试使用从JSP文件中检索的JSON字符串填充JSTree。
这是我的尝试:
function initDirs() {
var req = new XMLHttpRequest();
req.overrideMimeType("application/json");
req.open("GET", "svntojson.jsp?expandedNodePath=/root", true);
req.onreadystatechange = function receive() {
if (req.readyState == 4) {
createTree(req.responseText.trim());
}
};
req.send();
}
initDirs();
function createTree(jsonData) {
console.log(jsonData);
$('#treeview').jstree({
'core' : {
'data' : jsonData
}
});
}
不幸的是,jstree是空的。我记录了返回的 json,这对我来说看起来不错:
{ "id" : "root", "parent" : "#", "text" : "root" },
{"id":"branches","parent":"root","text":"branches"},
{"id":"README.txt","parent":"root","text":"README.txt"},
{"id":"svn.ico","parent":"root","text":"svn.ico"},
{"id":"Desktop.ini","parent":"root","text":"Desktop.ini"},
{"id":"vgt","parent":"root","text":"vgt"},
{"id":"trunk","parent":"root","text":"trunk"},
{"id":"format","parent":"root","text":"format"}
如果手动设置返回的 json,它可以工作:
function createTree(jsonData) {
console.log(jsonData);
$('#treeview').jstree({
'core' : {
'data' : [
{ "id" : "root", "parent" : "#", "text" : "root" },
{"id":"branches","parent":"root","text":"branches"},
{"id":"README.txt","parent":"root","text":"README.txt"},
{"id":"svn.ico","parent":"root","text":"svn.ico"},
{"id":"Desktop.ini","parent":"root","text":"Desktop.ini"},
{"id":"vgt","parent":"root","text":"vgt"},
{"id":"trunk","parent":"root","text":"trunk"},
{"id":"format","parent":"root","text":"format"}
]
}
});
}
任何人都可以帮助我在树视图中显示我返回的 json?
编辑:这是我的最终解决方案:
function initDirs() {
var req = new XMLHttpRequest();
var path = "root/";
req.overrideMimeType("application/json");
req.open("GET", "svntojson.jsp?expandedNodePath="+path, true);
req.onreadystatechange = function receive() {
if (req.readyState == 4) {
var jsonData = JSON.parse(req.responseText.trim());
$('#treeview').jstree(true).settings.core.data = jsonData;
$('#treeview').jstree(true).refresh();
}
};
req.send();
}
initDirs();
req.responseText.trim()
返回一个字符串,所以你需要把这个JSON转换成一个Javascript对象。
尝试以下操作:
if (req.readyState == 4) {
var respData = JSON.parse(req.responseText.trim());
createTree(respData);
}
相关文章:
- 如何设置html元素填充的动画
- 如何使用jquery在填充自动完成的值后使文本框只读
- 如何通过ajax刷新JSF填充的javascript变量
- 在jstree中,如何将指定的节点集中到大型树上
- 用与线条相同的颜色填充多折线图上的点
- 用每小时的差值填充数组/列表-从下拉列表中给定两个时间值
- 带有填充属性的SVG矩形显示在包含元素的上方插入框阴影
- 用我的json数据填充JQuery DataTable
- 通过链接重定向不;我不在jstree中工作
- 使用 jQuery 检查所有值是否为空或已填充
- 为什么不't我的变量在我的控制器中填充后在我的视图中呈现
- 将节点数据分配给另一个变量jstree
- 将Json作为变量加载到jsTree中
- 如何让typeahead在我的搜索栏中填充自定义参数
- 如何使用JSON字符串中的jQuery填充下拉框
- "堆叠外空间”;同时通过AJAX调用填充jsTree
- 用 JSP 中的 JSON 填充 JSTree
- jsTree-使用AJAX/C#Web方法动态填充树
- 用模型信息填充JSTree中的AJAX url
- 用JavaScript构建一个数组来填充jsTree