Ztree异步树加载
JSP代码片段
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <script type="text/javascript" src="${ctx}/thscommon/javascript/jquery-1.7.2.min.js"></script> <script type="text/javascript" src="${ctx}/win/javascript/jquery.ztree.core-3.5.js"></script> <link rel="stylesheet" href="${ctx}/win/css/zTreeStyle/zTreeStyle.css" type="text/css"> <title>Insert title here</title> <script type="text/javascript"> var setting = { /* check : { enable : true, chkStyle : "radio", radioType: "id" }, */ async : { enable : true, url : "${ctx}/common/tree/river!getNodeData.action", autoParam : [ "id" ], dataFilter : filter }, callback : { //beforeClick: beforeClick, onClick : onClick } }; function filter(treeId, parentNode, childNodes) { if (!childNodes) return null; for ( var i = 0, l = childNodes.length; i < l; i++) { childNodes[i].name = childNodes[i].name.replace(/\.n/g, '.'); } return childNodes; } function onClick(event, treeId, treeNode, clickFlag) { var win = art.dialog.open.origin;//来源页面 win.snstSet(treeNode.id, treeNode.name,$("#idx").val()); $.dialog.close(); } $(document).ready(function() { $.fn.zTree.init($("#tree"), setting); }); //--> </script> </head> <body> <div class="zTreeDemoBackground left"> <ul id="tree" class="ztree"></ul> </div> <input type="hidden" id="idx" name="idx" value="${idx}"> </body> </html>
action处理如下
public void getNodeData() { List<Map<String, Object>> nodedata = service.getNodes(this.getId()); List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); Map<String, Object> tmp = null; for (Map<String, Object> map : nodedata) { tmp = new HashMap<String, Object>(); tmp.put("id", map.get("id")); tmp.put("name", map.get("name")); tmp.put("isParent", Integer.valueOf(map.get("cnt").toString()) > 0 ? true : false); list.add(tmp); } try { PrintWriter out; JSONObject jo = new JSONObject(); response.setCharacterEncoding("gbk"); response.setContentType("text/html; charset=GBK"); out = response.getWriter(); System.out.println(JSONArray.fromObject(list).toString()); out.print(JSONArray.fromObject(list).toString()); out.close(); } catch (IOException e) { e.printStackTrace(); } }
Service处理如下
public List<Map<String, Object>> getNodes(String id) { return dao.getNodes(id); }
Dao处理如下
public List<Map<String, Object>> getNodes(String id) { List<String> params = new ArrayList<String>(); StringBuffer querySQL = new StringBuffer(); querySQL.append("SELECT tt.wsystemcode id,tt.wsystemname name"); querySQL.append(",(SELECT COUNT (1) cnt FROM datacenter_v3.tcode.t_cod_wsystem t WHERE t.parentcode = tt.wsystemcode) cnt"); querySQL.append(" FROM datacenter_v3.tcode.t_cod_wsystem tt"); querySQL.append(" WHERE 1=1"); if(Tool.isNull(id)){ querySQL.append(" and tt.parentcode is null"); }else{ querySQL.append(" and tt.parentcode = ?"); params.add(id); } querySQL.append(" GROUP BY tt.wsystemcode,tt.wsystemname"); querySQL.append(" ORDER BY tt.wsystemcode"); return search(querySQL.toString(), params.toArray()); }