js代码:
var curMenu = null, zTree_Menu = null; var setting = { async:{ autoParam:["menuid"],//异步加载时自动提交父节点参数 contentType:"application/json",//提交参数的数据类型 enable:true,//是否开启异步加载模式 type:"post",//提交方式 url:"LoadServlet",//服务端加载类 }, view: { showLine: true, selectedMulti: false, dblClickExpand: false }, data: { simpleData: { enable: true, idKey:"menuid", pIdKey:"parentid" } }, callback:{ asyncSuccess: zTreeOnAsyncSuccess,//异步加载成功的fun asyncError: zTreeOnAsyncError, //加载错误的fun beforeClick:beforeClick //捕获单击节点之前的事件回调函数 } }; function zTreeOnAsyncError(event, treeId, treeNode){ alert("异步加载失败!"); } function zTreeOnAsyncSuccess(event, treeId, treeNode, msg){ } function beforeClick(treeId, node) { if (node.isParent) { if (node.level === 0) { var pNode = curMenu; while (pNode && pNode.level !==0) { pNode = pNode.getParentNode(); alert(pNode); } if (pNode !== node) { var a = $("#" + pNode.tId + "_a"); a.removeClass("cur"); zTree_Menu.expandNode(pNode, false); } a = $("#" + node.tId + "_a"); a.addClass("cur"); var isOpen = false; for (var i=0,l=node.children.length; i<l; i++) { if(node.children[i].open) { isOpen = true; break; } } if (isOpen) { zTree_Menu.expandNode(node, true); curMenu = node; } else { zTree_Menu.expandNode(node.children[0].isParent?node.children[0]:node, true); curMenu = node.children[0]; } } else { zTree_Menu.expandNode(node); } } return !node.isParent; } $(document).ready(function(){ $.fn.zTree.init($("#treeDemo"), setting); zTree_Menu = $.fn.zTree.getZTreeObj("treeDemo"); });
后台代码(LoadMenuDaoImpl):
conn=BaseDao.getConn();
String sql="select * from tbl_menu where parentid=?";
ps=conn.prepareStatement(sql);
ps.setString(1, menuid);
rs=ps.executeQuery();
menu.append("[");
while(rs.next()){
menu.append("{menuid:'"+rs.getString("menuid")+"',parentid:'"+rs.getString("parentid")+"',name:'"+rs.getString("menuname")+"',isParent:true},");
}
menu.append("]");
LoadMenuServlet代码:
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
String menuid=request.getParameter("menuid");
System.out.println(menuid);
if(menuid==null||menuid.equals("")){
menuid="0";
}
LoadMenuDao lmd=new LoadMenuDaoImpl();
String menus=lmd.loadMenu(menuid);
out.print(menus);
out.flush();
out.close();
显示结果:
目前的问题是,菜单的根目录已经加载上来,但是,当我点击父节点的时候,如:数据展示篇,它并没有再次向后台读取数据!没有实现异步!
求解决!