基於jquery+ztree+java擷取json資料構建樹執行個體

來源:互聯網
上載者:User

zTree 是利用 JQuery 的核心代碼,實現一套能完成大部分常用功能的 Tree 外掛程式

相容 IE、FireFox、Chrome 等瀏覽器
在一個頁面內可同時產生多個 Tree 執行個體
支援 JSON 資料
支援一次性靜態產生 和 Ajax 非同步載入 兩種方式
支援多種事件響應及反饋
支援 Tree 的節點移動、編輯、刪除
支援任意更換皮膚 / 個人化表徵圖(依靠css)
支援極其靈活的 checkbox 或 radio 選擇功能
簡單的參數配置實現 靈活多變的功能
因為ztree的格式支援Json,所以我們就建立了一個Node類【TreeNode.java】


/**
 * TreeNode.java
 * 著作權(C) 2012
 * 建立:cuiran 2012-06-12 15:37:40
 */
package com.fastjson.test;

/**
 * TODO
 * @author cuiran
 * @version TODO
 */
public class TreeNode {
 private Integer id;
 private Integer pId;
 private String name;
 private Boolean checked;
 private Boolean open;
 /**
  * @return the id
  */
 public Integer getId() {
  return id;
 }
 /**
  * @param id the id to set
  */
 public void setId(Integer id) {
  this.id = id;
 }
 /**
  * @return the pId
  */
 public Integer getPId() {
  return pId;
 }
 /**
  * @param id the pId to set
  */
 public void setPId(Integer id) {
  pId = id;
 }
 /**
  * @return the name
  */
 public String getName() {
  return name;
 }
 /**
  * @param name the name to set
  */
 public void setName(String name) {
  this.name = name;
 }
 /**
  * @return the checked
  */
 public Boolean isChecked() {
  return checked;
 }
 /**
  * @param checked the checked to set
  */
 public void setChecked(Boolean checked) {
  this.checked = checked;
 }
 /**
  * @return the open
  */
 public Boolean isOpen() {
  return open;
 }
 /**
  * @param open the open to set
  */
 public void setOpen(Boolean open) {
  this.open = open;
 }
 public TreeNode(Integer id, Integer pId, String name, Boolean checked, Boolean open) {
  super();
  this.id = id;
  this.pId = pId;
  this.name = name;
  this.checked = checked;
  this.open = open;
 }
 public TreeNode() {
  super();
 }
 

}
建立一個TreeServlet【TreeServlet.java】


package com.fastjson.test;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;


public class TreeServlet extends HttpServlet {

 /**
  * Constructor of the object.
  */
 public TreeServlet() {
  super();
 }

 /**
  * Destruction of the servlet. <br>
  */
 public void destroy() {
  super.destroy(); // Just puts "destroy" string in log
  // Put your code here
 }

 /**
  * The doGet method of the servlet. <br>
  *
  * This method is called when a form has its tag value method equals to get.
  *
  * @param request the request send by the client to the server
  * @param response the response send by the server to the client
  * @throws ServletException if an error occurred
  * @throws IOException if an error occurred
  */
 public void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {

  this.doPost(request, response);
 }

 /**
  * The doPost method of the servlet. <br>
  *
  * This method is called when a form has its tag value method equals to post.
  *
  * @param request the request send by the client to the server
  * @param response the response send by the server to the client
  * @throws ServletException if an error occurred
  * @throws IOException if an error occurred
  */
 public void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  response.setContentType("text/html");
  response.setCharacterEncoding("utf-8");
  
  PrintWriter out = response.getWriter();
  
  TreeNode node1=new TreeNode(1,0,"北京市",false,true);
  TreeNode node2=new TreeNode(2,1,"朝陽區",false,true);
  TreeNode node3=new TreeNode(3,1,"海澱區",false,true);
  
  TreeNode node4=new TreeNode(4,0,"湖北省",false,true);
  TreeNode node5=new TreeNode(5,4,"武漢市",false,true);
  TreeNode node6=new TreeNode(6,4,"襄陽市",false,true);
  
  List<TreeNode> list=new ArrayList<TreeNode>();
  list.add(node1);
  list.add(node2);
  list.add(node3);
  list.add(node4);
  list.add(node5);
  list.add(node6);
  
  String jsonString = JSON.toJSONString(list);
  System.out.println("調用後台:"+jsonString);
  out.println(jsonString);
  out.flush();
  out.close();
 }

 /**
  * Initialization of the servlet. <br>
  *
  * @throws ServletException if an error occurs
  */
 public void init() throws ServletException {
  // Put your code here
 }

}

 


網頁代碼【tree.jsp】


<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
   
    <title>My JSP 'tree.jsp' starting page</title>
   
 <meta http-equiv="pragma" content="no-cache">
 <meta http-equiv="cache-control" content="no-cache">
 <meta http-equiv="expires" content="0">   
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
 <meta http-equiv="description" content="This is my page">
 <!--
 <link rel="stylesheet" type="text/css" href="styles.css">
 -->
 <link rel="stylesheet" href="css/demo.css" type="text/css">
 <link rel="stylesheet" href="css/zTreeStyle/zTreeStyle.css" type="text/css">
 <script type="text/javascript" src="js/jquery-1.4.4.min.js"></script>
 <script type="text/javascript" src="js/jquery.ztree.core-3.2.js"></script>
 <script type="text/javascript" src="js/jquery.ztree.excheck-3.2.js"></script>
 
 
 <SCRIPT type="text/javascript">
  <!--
  var setting = {
   check: {
    enable: true
   },
   data: {
    simpleData: {
     enable: true
    }
   }
  };

  var zNodes ;
  
  
  var code;
 
  function setCheck() {
  
   var zTree = $.fn.zTree.getZTreeObj("treeDemo"),
   py = $("#py").attr("checked")? "p":"",
   sy = $("#sy").attr("checked")? "s":"",
   pn = $("#pn").attr("checked")? "p":"",
   sn = $("#sn").attr("checked")? "s":"",
   type = { "Y":py + sy, "N":pn + sn};
   zTree.setting.check.chkboxType = type;
   showCode('setting.check.chkboxType = { "Y" : "' + type.Y + '", "N" : "' + type.N + '" };');
  }
  function showCode(str) {
   if (!code) code = $("#code");
   code.empty();
   code.append("<li>"+str+"</li>");
  }
  
 
  function fuzhi(data){
   
  
   zNodes=eval("("+data+")");
  
   
   $.fn.zTree.init($("#treeDemo"), setting, zNodes);
  
   setCheck();
   $("#py").bind("change", setCheck);
   $("#sy").bind("change", setCheck);
   $("#pn").bind("change", setCheck);
   $("#sn").bind("change", setCheck);
   
  
  }
  
  $(document).ready(function(){
   
  
  $.get("http://localhost:8090/Test/servlet/tree",function(data){
  
   $('#result').text(data);//直接展示JSON資料
   fuzhi(data);
   
   });

  });
  //-->
 </SCRIPT>
  </head>
 
  <body >
    This is my JSP page. <br>
    <div  id="result">
 </div>
    <div class="zTreeDemoBackground left">
  <ul id="treeDemo" class="ztree"></ul>
 </div>
 <div class="right">
  <ul class="info">
   <li class="title"><h2>1、setting 配置資訊說明</h2>
    <ul class="list">
    <li class="highlight_red">使用 checkbox,必須設定 setting.check 中的各個屬性,詳細請參見 API 文檔中的相關內容</li>
    <li><p>父子關聯關係:<br/>
      被勾選時:<input type="checkbox" id="py" class="checkbox first" checked /><span>關聯父</span>
      <input type="checkbox" id="sy" class="checkbox first" checked /><span>關聯子</span><br/>
      取消勾選時:<input type="checkbox" id="pn" class="checkbox first" checked /><span>關聯父</span>
      <input type="checkbox" id="sn" class="checkbox first" checked /><span>關聯子</span><br/>
      <ul id="code" class="log" style="height:20px;"></ul>
      </p>
    </li>
    </ul>
   </li>
   <li class="title"><h2>2、treeNode 節點資料說明</h2>
    <ul class="list">
    <li class="highlight_red">1)、如果需要初始化預設節點被勾選,請設定 treeNode.checked 屬性,詳細請參見 API 文檔中的相關內容</li>
    <li class="highlight_red">2)、如果某節點禁用 checkbox,請設定 treeNode.chkDisabled 屬性,詳細請參見 API 文檔中的相關內容 和 'chkDisabled 示範'</li>
    <li class="highlight_red">3)、如果某節點不顯示 checkbox,請設定 treeNode.nocheck 屬性,詳細請參見 API 文檔中的相關內容 和 'nocheck 示範'</li>
    <li class="highlight_red">4)、如果更換 checked 屬性,請參考 API 文檔中 setting.data.key.checked 的詳細說明</li>
    <li>5)、其他請參考 API 文檔中 treeNode.checkedOld / getCheckStatus / check_Child_State / check_Focus 的詳細說明</li>
    </ul>
   </li>
  </ul>
 </div>
  </body>
</html>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.