ztree學習之異步加載節點

ztreedemo.jsp:

Html代碼  收藏代碼
  1. <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>  
  2. <%  
  3. String path = request.getContextPath();  
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  5. %>  
  6.   
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  8. <html>  
  9.   <head>  
  10.     <base href="<%=basePath%>">  
  11.       
  12.     <title>My JSP 'ztreedemo.jsp' starting page</title>  
  13.       
  14.     <meta http-equiv="pragma" content="no-cache">  
  15.     <meta http-equiv="cache-control" content="no-cache">  
  16.     <meta http-equiv="expires" content="0">      
  17.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  18.     <meta http-equiv="description" content="This is my page">  
  19.     <link rel="stylesheet" href="<%=basePath%>/ztree/css/demo.css" type="text/css">  
  20.     <link rel="stylesheet" href="<%=basePath%>/ztree/css/zTreeStyle/zTreeStyle.css" type="text/css">  
  21.     <script type="text/javascript" src="<%=basePath%>/ztree/js/jquery-1.4.4.min.js"></script>  
  22.     <script type="text/javascript" src="<%=basePath%>/ztree/js/jquery.ztree.core-3.5.js"></script>  
  23.     <script type="text/javascript" src="<%=basePath%>/js/test/ztreedemo.js"></script>  
  24.   </head>  
  25.     
  26.   <body>  
  27.     <ul id="treeDemo" class="ztree"></ul>  
  28.   </body>  
  29. </html>  

 

ztreedemo.js:

Js代碼  收藏代碼
  1. $(document).ready(function(){  
  2.     initMyZtree();  
  3. });  
  4.   
  5. var zNodes="";  
  6. var setting = {  
  7.     view: {  
  8.         selectedMulti: false,  
  9.         fontCss: setFontCss  
  10.     },  
  11.     async: {  
  12.         enable: true,  
  13.         url:"getZtreeData",  
  14.         autoParam:["id"]  
  15.     },  
  16.     callback: {  
  17.         beforeClick: beforeClickZtree  
  18.     }  
  19. };  
  20.   
  21. function initMyZtree(){  
  22.     $.ajax({                 
  23.         type: "POST",                 
  24.         dataType: "json",                 
  25.         url: 'getZtreeData',     
  26.         success: function(data) {     
  27.             zNodes=data;  
  28.             $.fn.zTree.init($("#treeDemo"), setting, zNodes);  
  29.         }     
  30.     });    
  31.       
  32. }  
  33.   
  34. //單擊事件  
  35. function beforeClickZtree(treeId, treeNode){  
  36.     alert(treeNode.id+","+treeNode.name);  
  37. }  
  38.   
  39. //設置字體  
  40. function setFontCss(treeId, treeNode) {  
  41.     if(treeNode.level==0){  
  42.         return {'font-weight':'bold','color':'red'};  
  43.     }else if(treeNode.level==1){  
  44.         return {'font-weight':'bold','color':'green'};  
  45.     }else if(treeNode.level==2){  
  46.         return {'font-weight':'bold','color':'blue'};  
  47.     }else{  
  48.         return {};  
  49.     }  
  50. };  

 

CZTestAction.java:

Java代碼  收藏代碼
  1. package com.cz.action;  
  2.   
  3. import java.io.IOException;  
  4. import java.io.PrintWriter;  
  5. import java.sql.ResultSet;  
  6. import java.sql.SQLException;  
  7. import java.util.ArrayList;  
  8. import java.util.List;  
  9.   
  10. import javax.servlet.http.HttpServletResponse;  
  11.   
  12. import net.sf.json.JSONArray;  
  13.   
  14. import org.apache.struts2.ServletActionContext;  
  15.   
  16. import com.cz.model.TreeNode;  
  17. import com.cz.util.SqlHelper;  
  18. import com.opensymphony.xwork2.ActionSupport;  
  19.   
  20. public class CZTestAction extends ActionSupport{  
  21.     private String id;  
  22.   
  23.     public String getId() {  
  24.         return id;  
  25.     }  
  26.   
  27.     public void setId(String id) {  
  28.         this.id = id;  
  29.     }  
  30.   
  31.   
  32.     /** 
  33.      * @author chenzheng 
  34.      * @since 2013-8-21 
  35.      * @Description: ztree測試 
  36.      * @throws 
  37.      * @return 
  38.      * String 
  39.      */  
  40.     public String getZtreeData(){  
  41.         System.out.println("*********"+id+"**********");  
  42.         if("null".equals(id)||"".equals(id)||id==null){  
  43.             id="0";  
  44.         }  
  45.         String sql="select t.jgid,t.jgmc,t.fjgid,t.jgbm,(select count(*) from sys_dept sd where sd.fjgid=t.jgid) as ispar from SYS_DEPT t where t.fjgid="+id;  
  46.         ResultSet rs=SqlHelper.executeQuery(sql, null);  
  47.         JSONArray jarray=new JSONArray();  
  48.         List<TreeNode> list=new ArrayList<TreeNode>();  
  49.         try {  
  50.             while(rs.next()){  
  51.                 TreeNode tnode=new TreeNode();  
  52.                 tnode.setId(rs.getString(1));  
  53.                 tnode.setName(rs.getString(2));  
  54.                 tnode.setpId(rs.getString(3));  
  55.                 //判斷當前節點是否還有子節點  
  56.                 if(Integer.parseInt(rs.getString(5))>0){  
  57.                     tnode.setIsParent(true);  
  58.                     tnode.setHasChild(true);  
  59.                 }else{  
  60.                     tnode.setIsParent(false);  
  61.                     tnode.setHasChild(false);  
  62.                 }  
  63.                 list.add(tnode);  
  64.             }  
  65.         } catch (SQLException e) {  
  66.             e.printStackTrace();  
  67.         }  
  68.         jarray.addAll(list);  
  69.         System.out.println(jarray.toString());  
  70.         HttpServletResponse response = ServletActionContext.getResponse();  
  71.         response.setCharacterEncoding("utf-8");  
  72.         PrintWriter pw = null;  
  73.         try {  
  74.             pw = response.getWriter();  
  75.             pw.write(jarray.toString());  
  76.         } catch (IOException e) {  
  77.             e.printStackTrace();  
  78.         }  
  79.         pw.flush();  
  80.         pw.close();  
  81.         return null;  
  82.     }  
  83. }  

 

TreeNode.java:

Java代碼  收藏代碼
  1. package com.cz.model;  
  2.   
  3. public class TreeNode {  
  4.   
  5.     private String id;  
  6.     private String pId;  
  7.     private String name;  
  8.     private Boolean isParent;  
  9.     private Boolean hasChild;  
  10.     public String getId() {  
  11.         return id;  
  12.     }  
  13.     public void setId(String id) {  
  14.         this.id = id;  
  15.     }  
  16.     public String getpId() {  
  17.         return pId;  
  18.     }  
  19.     public void setpId(String pId) {  
  20.         this.pId = pId;  
  21.     }  
  22.     public String getName() {  
  23.         return name;  
  24.     }  
  25.     public void setName(String name) {  
  26.         this.name = name;  
  27.     }  
  28.     public Boolean getIsParent() {  
  29.         return isParent;  
  30.     }  
  31.     public void setIsParent(Boolean isParent) {  
  32.         this.isParent = isParent;  
  33.     }  
  34.     public Boolean getHasChild() {  
  35.         return hasChild;  
  36.     }  
  37.     public void setHasChild(Boolean hasChild) {  
  38.         this.hasChild = hasChild;  
  39.     }  
  40.       
  41.       
  42. }  

 

效果圖:

ztree

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章