zTree后台动态取数例子

       tree的插件有很多种,比如说有jstree,extjs的tree,有想用tree实现功能的朋友我建议使用ztree,ztree现在做得很强大,一般的功能需求都能满足,而且相应的文档也比较全,使用起来很方便。在附件中我上传了ztree的插件以及在项目中要用到的动态从后台取数例子,有兴趣的朋友可以参阅一下。

public class ZtreeAction {
	//数据结点
	private  String ztreeNode;
	
	/*
	 * 页面跳转方法
	 */
  public String zreePage(){
	  return "success";
  }
  
  /*
   * 获取数据生成树
   */
  public String showTree(){
	  //此为静态数据,如要从后台数据库取数无疑是想办法拼装成这个串返回到前台就行了
	  ztreeNode="[{\"name\" : \"第一层\",\"children\":[{\"name\":\"第二层\",\"children\":[{\"name\":\"第三层\"}]},{\"name\":\"第二层\"}]}]";  //\"为双引号的转义字符,即要组装成的格式为["name":"第一层"]
	  return "success";
  }
  

  
  /*----get set ----*/
	public String getZtreeNode() {
		return ztreeNode;
	}
	
	public void setZtreeNode(String ztreeNode) {
		this.ztreeNode = ztreeNode;
	}
}

 

<%@ 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>zTree</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" href="css/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.4.js"></script>
	<SCRIPT type="text/javascript">
    var zNodes;
    var setting = {};
		$(document).ready(function(){
			 $.ajax({
				   url:"../zTreeDemo/ztree/showTree.action",  
				   type:"post",
				   data:"json",
				   success:function(data){	
                   //json格式转换成对象
                   zNodes=$.parseJSON(data);
				   $.fn.zTree.init($("#treeDemo"), setting, zNodes);
				   } 
				});	
		
		});
	</SCRIPT>
</HEAD>
  
<BODY>
<div class="content_wrap">
	<div class="zTreeDemoBackground left">
		<ul id="treeDemo" class="ztree"></ul>
	</div>
</div>
</BODY>
</html>

 

 

你可能感兴趣的:(zTree后台动态取数例子)