最近要做树,用dtree+ajax来实现,结果半天也弄不出来。碰到两个问题:
1、dtree+ajax不刷新
直接用例子程序来做一个静态的树,节点都是自己写的,但是放在getJson里面不能刷新,+号点击没有反应,找了半天,终于找到问题了:
doucument.write(b);
要写成
$("#treeDiv").html(tree.toString());
下面给一个整体的实例。
http://www.cnblogs.com/kenkofox/archive/2011/03/28/1997638.html
(!!!在IE,refreshTree的
getJSON不刷新的问题,参考:http://www.cnblogs.com/kenkofox/archive/2011/04/02/2004101.html)
本来我想使用jsTree或者treeView这种jquery插件的,这些插件虽然功能很强大,但无奈,太花俏了,需要学习的配置很多。
而且对于我的应用来说,并不需要花俏的功能,例如拖拽,双击重命名,右键菜单等。
耗了2天在学习jsTree和treeView,都发现不太适合,索性用最原始的dtree,效果也不错,而且所有代码简单,能够完全掌握在自己控制之下。
废话不说了。在这里分享一下,我用ajax加载dtree的做法,后台servlet提供json格式的tree数据。这个做法应该可以用到做一个系统的导航栏上。
全部代码:ajax,servlet动态加载dtree.rar
HTML:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF8">
<link rel="StyleSheet" href="./dtree/dtree.css" type="text/css" />
<script type="text/javascript" src="./dtree/dtree.js"></script>
<script type="text/javascript" src="./js/jquery-1.5.1.min.js"></script>
<script>
dtreePath = './dtree/'; //我在dtree.js中加了这个变量,便于调整img路径
//需要设置为dtree目录位置,底下有img目录
var tree; //tree必须为全局变量
$(document).ready(function(){
refreshTree();
});
/**
* 点击菜单的操作,在后台Servlet返回的json数据中设置了url为javascript:showFolder(dir)
*/
function showFolder(dir) {
alert(dir);
}
function refreshTree() {
//生成新的树,ajax方式获取最新tree,每个json item表示一个节点
$.getJSON('OnlineFileManagerServlet',function(data){
tree = new dTree('tree'); //参数tree,表示生成的html代码中id的标记,不影响功能
tree.add(0,-1,'网络文件夹'); //树根
//遍历JSON中的每个entry
$.each(data,function(entryIndex,entry){
tree.add(entry['id'], entry['pid'], entry['name'], entry['url']);
});
$("#treeDiv").html(tree.toString());
});
}
</script>
</head>
<body>
<div class="dtree">
<p><a href="javascript: tree.openAll();">open all</a> | <a href="javascript: tree.closeAll();">close all</a></p>
<div id="treeDiv">
</div>
</div>
</body>
</html>
Servlet:(关于json,参考:http://www.cnblogs.com/kenkofox/archive/2011/03/25/1995436.html)
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
try {
//创建json数据
JSONArray tree = new JSONArray();
JSONObject node1 = new JSONObject();
node1.put("id", "1");
node1.put("pid", "0");
node1.put("name", "金属产品检验报告");
node1.put("url", "javascript:showFolder('" + "abc" + "')");
JSONObject node2 = new JSONObject();
node2.put("id", "2");
node2.put("pid", "0");
node2.put("name", "塑料产品检验报告");
node2.put("url", "javascript:showFolder('" + "abc" + "')");
JSONObject node3 = new JSONObject();
node3.put("id", "3");
node3.put("pid", "1");
node3.put("name", "阳江海关检验报告");
node3.put("url", "javascript:showFolder('" + "abc" + "')");
JSONObject node4 = new JSONObject();
node4.put("id", "4");
node4.put("pid", "3");
node4.put("name", "检验报告abc");
node4.put("url", "javascript:showFolder('" + "abc" + "')");
JSONObject node5 = new JSONObject();
node5.put("id", "5");
node5.put("pid", "2");
node5.put("name", "检验报告2");
node5.put("url", "javascript:showFolder('" + "abc" + "')");
tree.put(node1);
tree.put(node2);
tree.put(node3);
tree.put(node4);
tree.put(node5);
out.write(tree.toString());
System.out.println(tree.toString());
} catch (JSONException ex) {
Logger.getLogger(OnlineFileManagerServlet.class.getName()).log(Level.SEVERE, null, ex);
} finally {
out.close();
}
}
分享到:
相关推荐
$.getJSON异步请求和同步请求 同时执行多个$.getJSON() 数据混乱的问题的解决在执行之前加$.ajaxSettings.async = fals
NULL 博文链接:https://ch-dj.iteye.com/blog/1745077
jQuery的json问题。json定义标准。Ie6中getjson()与其他浏览器的兼容性的解决方案
这是一个前端后端通过JQuery的getJSON函数交互json数据的例子。 前端网页是front.html,后端处理的模块是back.jsp 把back.jsp、front.html及jquery-1.8.3.min.js部署在Tomcat的webapps的ROOT下,通过在浏览器输入...
利用jQuery中的getJson和ashx,实现ashx页面数据回传给getJson,该实例属于代码为C#,只是简单的读取返回数据,通俗易懂,适合入门级。
前端用jquery的getJson方法从PHP文件获取json数据
Java 遍历文件夹文件,读取Json文本字符串,存入数据库。
C# url post get json
详细解读Jquery各Ajax函数:$.get(),$.post(),$.ajax(),$.getJSON()
ajax格式的getJSON
$(function(){ $.getJSON(url,function(data){ alert (data.Name); }) }); 服务器返回字符串: {“Name”:”loogn”,”Age”:23} 2,不同域名下 js: 代码如下: var url=”...
JQuery getJSON() 调用Servlet简单例子
GetJson是从REST服务接收JSON数据的最简单HTTP库
GetJsonData 仅需要一行代码就获取Json数据 1.不带请求参数 GetJson getJson=new GetJson(); String jsonData=getJson.Get("https://c.iwakeup.cn/cp/posts", "GET", null); 2.带请求参数 支持GET POST PUT ...
GetJson GetJson is the simplest HTTP library to Receive JSON Data from REST Service. It Gives data as a String which can be used to pass the data between activities which can then be converted to ...
遇到一个getJSON的问题,回调函数一直无法执行,检查所有的都没有问题,但就是alert不出来传回的数据,有类似情况的朋友可以参考下本
jquery的ajax和getJson跨域.docxjquery的ajax和getJson跨域.docx
lotus domnio java代理getjson
介绍了JQuery 获取json数据$.getJSON方法的实例代码,有需要的朋友可以参考一下
原因:getJson在IE下IE下默认会使用浏览器缓存,所以导致数据不显示 解决:让其不要使用缓存 这里有更多的解决方法 http://stackoverflow.com/questions/264216/getjson-returning-cached-data-in-ie8