论坛首页 Web前端技术论坛

zTree 正式搬家到 iteye

浏览 56811 次
该帖已经被评为精华帖
作者 正文
   发表时间:2012-03-14  
强烈要求给个JAVA版本的完成例子
0 请登录后投票
   发表时间:2012-03-14  
shiny_txdd 写道
强烈要求给个JAVA版本的完成例子

不好意思,实在没有。 而且 对于 java 来说只需要能实现 server 与 ajax 之间的通信就可以了。 你可以去 zTree 吧 上问问,最近有不少新人愿意共享资料
0 请登录后投票
   发表时间:2012-03-27  
我们公司用的js tree 必须满足 一个节点下能一次性加载出 1W条子节点
而且时间必须是5秒以内... 一次性加载  分页以及 一段一段加载都不符合
不知道还有没有其他的树....
0 请登录后投票
   发表时间:2012-03-28  
dennislee414 写道
我们公司用的js tree 必须满足 一个节点下能一次性加载出 1W条子节点
而且时间必须是5秒以内... 一次性加载  分页以及 一段一段加载都不符合
不知道还有没有其他的树....

如果要求到这种情况,那么请建议不要考虑 js 方式的树了。(尤其是在IE浏览器下)
0 请登录后投票
   发表时间:2012-03-28  
dennislee414 写道
我们公司用的js tree 必须满足 一个节点下能一次性加载出 1W条子节点
而且时间必须是5秒以内... 一次性加载  分页以及 一段一段加载都不符合
不知道还有没有其他的树....



这种情况肯定会出现滚动条的,可以利用滚动加载,计算出视窗内的数据的位置,显示出来,不过有难度,我觉得这是一个较好的解决方案
0 请登录后投票
   发表时间:2012-03-28  
Ztree 真的好用,画面很酷!官网也做得很好看,很炫,赞一个!
0 请登录后投票
   发表时间:2012-03-28  
dingchao.lonton 写道
dennislee414 写道
我们公司用的js tree 必须满足 一个节点下能一次性加载出 1W条子节点
而且时间必须是5秒以内... 一次性加载  分页以及 一段一段加载都不符合
不知道还有没有其他的树....



这种情况肯定会出现滚动条的,可以利用滚动加载,计算出视窗内的数据的位置,显示出来,不过有难度,我觉得这是一个较好的解决方案

按照他的需求,我觉得这是一个思路,但他们的要求绝不是简单的利用滚动条进行延迟加载。 还要配合节点进行一系列高度的计算,总之一定要实现需要特殊定制+专门调试
0 请登录后投票
   发表时间:2012-03-28  
vanner007 写道
Ztree 真的好用,画面很酷!官网也做得很好看,很炫,赞一个!

  很高兴你能喜欢 zTree
0 请登录后投票
   发表时间:2012-03-30   最后修改:2012-03-30
zTreeAPI 写道
shiny_txdd 写道
强烈要求给个JAVA版本的完成例子

不好意思,实在没有。 而且 对于 java 来说只需要能实现 server 与 ajax 之间的通信就可以了。 你可以去 zTree 吧 上问问,最近有不少新人愿意共享资料




贴一个之前写的一个例子:

 

引入:

 

 

<script type="text/javascript" src="<%=basePath%>/javascript/jquery-1.4.2.js"></script>
	<script type="text/javascript" src="<%=basePath%>/javascript/MQuery.js"></script>
	<script type="text/javascript" src="<%=basePath%>/javascript/jquery.ztree-2.6.min.js"></script>
	<link rel="stylesheet" href="<%=basePath%>/zTreeStyle/zTreeStyle.css" type="text/css">

 

 

 

 

<script type="text/javascript">
	
	var zTree;
	var setting = {
		isSimpleData: false,
		treeNodeKey: "id",
		nodesCol : "children",
		showLine: true,
		root:{ 
			isRoot:true,
			children:[]
		}
	};
	
	var url = "<%=basePath%>/servlet/InventoryClassServlet";
	$(document).ready(function(){
		MQuery.url = url;
		MQuery.data.forward = "listJqueryTreeData";
		MQuery.data.random = Math.random();
		MQuery.responseFun = addSuccess;
		MQuery.submit();
		
	
	});
	
	function addSuccess(data){
		setting.expandSpeed = ($.browser.msie && parseInt($.browser.version)<=6)?"":"fast";
		zTree = $("#tree").zTree(setting, data);
	}


	//...........
	
	
	</script>

 

html :

 

<h1>Simple Tree example参考:</h1><a href="http://www.baby666.cn/hunter/zTree.html" target="_blank">http://www.baby666.cn/hunter/zTree.html</a>
	<ul id="tree" class="tree" style="width:230px; overflow:auto;"></ul>
 

 

 

MQuery.js

 

 

var MQuery = {
	url:"",
	data:{},
	responseFun:null,
	submit:function(){
		var _responseFun = this.responseFun;
		$.ajax({
				type:"get",
				data:this.data,
				url:this.url,
				dataType:"jsonp",
				jsonp:"jsoncallback",
				success:function(responseJson){
						if (responseJson.status == "fail") {
							alert(responseJson.message);
							return;
						}
						if (_responseFun) {
							_responseFun(responseJson);
						}

				}
			});
	}
}

 

 @Override

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String forward = request.getParameter("forward");
		if (null == forward || "".equals(forward)) {
			MessagePrinter.printErrorMessage("请检查访问参数是否正确",request, response);
			return;
		}
		service = (IU8Service) WebApplicationContextUtils
				.getRequiredWebApplicationContext(
						request.getSession().getServletContext()).getBean(
						"u8Service");
		if (forward.equals("listMootoolsTreeData")) {
			MessagePrinter.printObject(service.getInventoryClassListJson(),request, response);
			return;
		}
///////////这里是列出ztree
		if (forward.equals("listJqueryTreeData")) {
			MessagePrinter.printObject(service.getInventoryClassListJsonForJquery(),request, response);
			return;
		}
		if (forward.equals("add")) {
			addInventoryClass(request, response);
		}
	}
 

 

MessagePrinter.java

 

 

public static void printObject(String message,HttpServletRequest request,HttpServletResponse response){
		String callback = request.getParameter("jsoncallback");
		response.setHeader("Cache-Control", "no-cache");
		response.setContentType("text/json;charset=UTF-8");
		PrintWriter out;
		try {
			out = response.getWriter();
			out.print(callback+"("+message+")");
			out.flush();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
 

 

getInventoryClassListJsonForJquery 方法:

 

 

@Override
	public String getInventoryClassListJsonForJquery() {
		List<InventoryClass> inventoryClassList = inventoryClassDao.getInventoryClassList();
		List<InventoryClass> fenleiList = new ArrayList<InventoryClass>();
		for (InventoryClass inventoryClass : inventoryClassList) {
			if (inventoryClass.getCinvCcode() != null
					&& inventoryClass.getCinvCcode().length() == 2) {// 第一级菜单
				fenleiList.add(generatorClassChildList(inventoryClassList,
						inventoryClass));
			}
		}

		// StringBuilder sb = new StringBuilder("{\"result\":[");
		StringBuilder sb = new StringBuilder(
				"[{\"id\":\"0\",\"name\":\"0-分类\",\"children\":[");
		for (int i = 0; i < fenleiList.size(); i++) {
			InventoryClass inventoryClass = fenleiList.get(i);
			sb.append(inventoryClass.toJSONForJQuery());
			if (i != fenleiList.size() - 1) {
				sb.append(",");
			}
		}
		sb.append("]}]");
		return sb.toString();
	}

 

 

拼接子节点的循环 generatorClassChildList

 

 

private InventoryClass generatorClassChildList(List<InventoryClass> ls,
			InventoryClass inv) {
		for (InventoryClass invclass : ls) {
			int tempL = invclass.getCinvCcode().replaceFirst(
					inv.getCinvCcode(), "").length();
			boolean isSon = tempL > 1 && tempL < 4;
			boolean startWith = invclass.getCinvCcode().startsWith(
					inv.getCinvCcode());
			if (!inv.getCinvCcode().equals(invclass.getCinvCcode())
					&& startWith && isSon) { // 如果是子节点
				// 判断是否包含子节点
				if (null != inv && null == inv.getChildren()) {
					inv.setChildren(new ArrayList<InventoryClass>());
				}
				invclass = generatorClassChildList(ls, invclass);
				inv.getChildren().add(invclass);
			}
		}
		return inv;
	}
 

 

 

InventoryClass  vo 类:

 

 

package com.thams.entity;

import java.util.List;

/**
 * 瀛樿揣鍒嗙被 InventoryClass entity.
 * 
 * @author MyEclipse Persistence Tools
 */
public class InventoryClass implements java.io.Serializable {

	// Fields

	private static final long serialVersionUID = 1L;
	/** 存货父类编码 */
	private String parentCinvCcode;
	/** 存货大类编码 */
	private String cinvCcode;
	/** 存货大类名称 */
	private String cinvCname;
	/** 编码级次 */
	private Short iinvCgrade;
	/** 是否末级 */
	private Byte binvCend = 1;//默认添加的就是最后一级
	/** 所属经济分类编码 */
	private String cecoCode;
	/** 对应条形码编码 */
	private String cbarCode;
	/** 时间戳 */
	private String pubufts;
	private List<InventoryClass> children;

	// Constructors

	public List<InventoryClass> getChildren() {
		return children;
	}

	public void setChildren(List<InventoryClass> children) {
		this.children = children;
	}

	// Property accessors

	public String getCinvCcode() {
		return this.cinvCcode;
	}

	public void setCinvCcode(String cinvCcode) {
		this.cinvCcode = cinvCcode;
	}

	public String getCinvCname() {
		return this.cinvCname;
	}

	public void setCinvCname(String cinvCname) {
		this.cinvCname = cinvCname;
	}

	public Short getIinvCgrade() {
		return this.iinvCgrade;
	}

	public void setIinvCgrade(Short iinvCgrade) {
		this.iinvCgrade = iinvCgrade;
	}

	public Byte getBinvCend() {
		return this.binvCend;
	}

	public void setBinvCend(Byte binvCend) {
		this.binvCend = binvCend;
	}

	public String getCecoCode() {
		return this.cecoCode;
	}

	public void setCecoCode(String cecoCode) {
		this.cecoCode = cecoCode;
	}

	public String getCbarCode() {
		return this.cbarCode;
	}

	public void setCbarCode(String cbarCode) {
		this.cbarCode = cbarCode;
	}

	public String getPubufts() {
		return pubufts;
	}

	public void setPubufts(String pubufts) {
		this.pubufts = pubufts;
	}

	public String toJSON() {
		StringBuilder sb = new StringBuilder("{\"property\":{");
		sb.append("\"id\":").append("\"" + cinvCcode + "\",");
		sb.append("\"name\":").append("\"" + cinvCcode+"-"+cinvCname + "\",");
		sb.append("\"binvCend\":").append("\"" + binvCend + "\"");
		// sb.append("\"iinvCgrade\":").append("\""+iinvCgrade+"\",");
		// sb.append("\"binvCend\":").append("\""+binvCend+"\",");
		// sb.append("\"cecoCode\":").append("\""+cecoCode+"\",");
		// sb.append("\"cbarCode\":").append("\""+cbarCode+"\",");
		// sb.append("\"pubufts\":").append("\""+pubufts+"\"");
		sb.append("}");
		if (children != null && children.size() > 0) {
			sb.append(",\"children\":[");
			for (int i = 0; i < children.size(); i++) {
				InventoryClass iv = children.get(i);
				sb.append(iv.toJSON());
				if (i != children.size() - 1) {
					sb.append(",");
				}
			}
			sb.append("]");
		}
		sb.append("}");
		return sb.toString();
	}
	public String toJSONForJQuery() { ////这里是获取jquerytree data
		StringBuilder sb = new StringBuilder("{");
		sb.append("\"id\":").append("\"" + cinvCcode + "\",");
		sb.append("\"name\":").append("\"" + cinvCcode+"-"+cinvCname + "\",");
		sb.append("\"binvCend\":").append("\"" + binvCend + "\"");
		// sb.append("\"iinvCgrade\":").append("\""+iinvCgrade+"\",");
		// sb.append("\"binvCend\":").append("\""+binvCend+"\",");
		// sb.append("\"cecoCode\":").append("\""+cecoCode+"\",");
		// sb.append("\"cbarCode\":").append("\""+cbarCode+"\",");
		// sb.append("\"pubufts\":").append("\""+pubufts+"\"");
		if (children != null && children.size() > 0) {
			sb.append(",\"children\":[");
			for (int i = 0; i < children.size(); i++) {
				InventoryClass iv = children.get(i);
				sb.append(iv.toJSONForJQuery());
				if (i != children.size() - 1) {
					sb.append(",");
				}
			}
			sb.append("]");
		}
		sb.append("}");
		return sb.toString();
	}

	public String getParentCinvCcode() {
		return parentCinvCcode;
	}

	public void setParentCinvCcode(String parentCinvCcode) {
		this.parentCinvCcode = parentCinvCcode;
	}

}

 

用的是jsonp get方式获取的 json对象.

 

大致的流程代码都贴上去了. 

 

程序代码没法给大家整个共享出来.

 

 

 

1 请登录后投票
   发表时间:2012-03-30  
jhaij 写道
zTreeAPI 写道
shiny_txdd 写道
强烈要求给个JAVA版本的完成例子

不好意思,实在没有。 而且 对于 java 来说只需要能实现 server 与 ajax 之间的通信就可以了。 你可以去 zTree 吧 上问问,最近有不少新人愿意共享资料




贴一个之前写的一个例子:

 

引入:

 

 

<script type="text/javascript" src="<%=basePath%>/javascript/jquery-1.4.2.js"></script>
	<script type="text/javascript" src="<%=basePath%>/javascript/MQuery.js"></script>
	<script type="text/javascript" src="<%=basePath%>/javascript/jquery.ztree-2.6.min.js"></script>
	<link rel="stylesheet" href="<%=basePath%>/zTreeStyle/zTreeStyle.css" type="text/css">

 

 

 

 

<script type="text/javascript">
	
	var zTree;
	var setting = {
		isSimpleData: false,
		treeNodeKey: "id",
		nodesCol : "children",
		showLine: true,
		root:{ 
			isRoot:true,
			children:[]
		}
	};
	
	var url = "<%=basePath%>/servlet/InventoryClassServlet";
	$(document).ready(function(){
		MQuery.url = url;
		MQuery.data.forward = "listJqueryTreeData";
		MQuery.data.random = Math.random();
		MQuery.responseFun = addSuccess;
		MQuery.submit();
		
	
	});
	
	function addSuccess(data){
		setting.expandSpeed = ($.browser.msie && parseInt($.browser.version)<=6)?"":"fast";
		zTree = $("#tree").zTree(setting, data);
	}


	//...........
	
	
	</script>

 

html :

 

 

<h1>Simple Tree example参考:</h1><a href="http://www.baby666.cn/hunter/zTree.html" target="_blank">http://www.baby666.cn/hunter/zTree.html</a>
	<ul id="tree" class="tree" style="width:230px; overflow:auto;"></ul>
 

 

 

MQuery.js

 

 

var MQuery = {
	url:"",
	data:{},
	responseFun:null,
	submit:function(){
		var _responseFun = this.responseFun;
		$.ajax({
				type:"get",
				data:this.data,
				url:this.url,
				dataType:"jsonp",
				jsonp:"jsoncallback",
				success:function(responseJson){
						if (responseJson.status == "fail") {
							alert(responseJson.message);
							return;
						}
						if (_responseFun) {
							_responseFun(responseJson);
						}

				}
			});
	}
}

 

 @Override

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String forward = request.getParameter("forward");
		if (null == forward || "".equals(forward)) {
			MessagePrinter.printErrorMessage("请检查访问参数是否正确",request, response);
			return;
		}
		service = (IU8Service) WebApplicationContextUtils
				.getRequiredWebApplicationContext(
						request.getSession().getServletContext()).getBean(
						"u8Service");
		if (forward.equals("listMootoolsTreeData")) {
			MessagePrinter.printObject(service.getInventoryClassListJson(),request, response);
			return;
		}
///////////这里是列出ztree
		if (forward.equals("listJqueryTreeData")) {
			MessagePrinter.printObject(service.getInventoryClassListJsonForJquery(),request, response);
			return;
		}
		if (forward.equals("add")) {
			addInventoryClass(request, response);
		}
	}
 

 

MessagePrinter.java

 

 

public static void printObject(String message,HttpServletRequest request,HttpServletResponse response){
		String callback = request.getParameter("jsoncallback");
		response.setHeader("Cache-Control", "no-cache");
		response.setContentType("text/json;charset=UTF-8");
		PrintWriter out;
		try {
			out = response.getWriter();
			out.print(callback+"("+message+")");
			out.flush();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
 

 

getInventoryClassListJsonForJquery 方法:

 

 

@Override
	public String getInventoryClassListJsonForJquery() {
		List<InventoryClass> inventoryClassList = inventoryClassDao.getInventoryClassList();
		List<InventoryClass> fenleiList = new ArrayList<InventoryClass>();
		for (InventoryClass inventoryClass : inventoryClassList) {
			if (inventoryClass.getCinvCcode() != null
					&& inventoryClass.getCinvCcode().length() == 2) {// 第一级菜单
				fenleiList.add(generatorClassChildList(inventoryClassList,
						inventoryClass));
			}
		}

		// StringBuilder sb = new StringBuilder("{\"result\":[");
		StringBuilder sb = new StringBuilder(
				"[{\"id\":\"0\",\"name\":\"0-分类\",\"children\":[");
		for (int i = 0; i < fenleiList.size(); i++) {
			InventoryClass inventoryClass = fenleiList.get(i);
			sb.append(inventoryClass.toJSONForJQuery());
			if (i != fenleiList.size() - 1) {
				sb.append(",");
			}
		}
		sb.append("]}]");
		return sb.toString();
	}

 

 

拼接子节点的循环 generatorClassChildList

 

 

private InventoryClass generatorClassChildList(List<InventoryClass> ls,
			InventoryClass inv) {
		for (InventoryClass invclass : ls) {
			int tempL = invclass.getCinvCcode().replaceFirst(
					inv.getCinvCcode(), "").length();
			boolean isSon = tempL > 1 && tempL < 4;
			boolean startWith = invclass.getCinvCcode().startsWith(
					inv.getCinvCcode());
			if (!inv.getCinvCcode().equals(invclass.getCinvCcode())
					&& startWith && isSon) { // 如果是子节点
				// 判断是否包含子节点
				if (null != inv && null == inv.getChildren()) {
					inv.setChildren(new ArrayList<InventoryClass>());
				}
				invclass = generatorClassChildList(ls, invclass);
				inv.getChildren().add(invclass);
			}
		}
		return inv;
	}
 

 

 

InventoryClass  vo 类:

 

 

package com.thams.entity;

import java.util.List;

/**
 * 瀛樿揣鍒嗙被 InventoryClass entity.
 * 
 * @author MyEclipse Persistence Tools
 */
public class InventoryClass implements java.io.Serializable {

	// Fields

	private static final long serialVersionUID = 1L;
	/** 存货父类编码 */
	private String parentCinvCcode;
	/** 存货大类编码 */
	private String cinvCcode;
	/** 存货大类名称 */
	private String cinvCname;
	/** 编码级次 */
	private Short iinvCgrade;
	/** 是否末级 */
	private Byte binvCend = 1;//默认添加的就是最后一级
	/** 所属经济分类编码 */
	private String cecoCode;
	/** 对应条形码编码 */
	private String cbarCode;
	/** 时间戳 */
	private String pubufts;
	private List<InventoryClass> children;

	// Constructors

	public List<InventoryClass> getChildren() {
		return children;
	}

	public void setChildren(List<InventoryClass> children) {
		this.children = children;
	}

	// Property accessors

	public String getCinvCcode() {
		return this.cinvCcode;
	}

	public void setCinvCcode(String cinvCcode) {
		this.cinvCcode = cinvCcode;
	}

	public String getCinvCname() {
		return this.cinvCname;
	}

	public void setCinvCname(String cinvCname) {
		this.cinvCname = cinvCname;
	}

	public Short getIinvCgrade() {
		return this.iinvCgrade;
	}

	public void setIinvCgrade(Short iinvCgrade) {
		this.iinvCgrade = iinvCgrade;
	}

	public Byte getBinvCend() {
		return this.binvCend;
	}

	public void setBinvCend(Byte binvCend) {
		this.binvCend = binvCend;
	}

	public String getCecoCode() {
		return this.cecoCode;
	}

	public void setCecoCode(String cecoCode) {
		this.cecoCode = cecoCode;
	}

	public String getCbarCode() {
		return this.cbarCode;
	}

	public void setCbarCode(String cbarCode) {
		this.cbarCode = cbarCode;
	}

	public String getPubufts() {
		return pubufts;
	}

	public void setPubufts(String pubufts) {
		this.pubufts = pubufts;
	}

	public String toJSON() {
		StringBuilder sb = new StringBuilder("{\"property\":{");
		sb.append("\"id\":").append("\"" + cinvCcode + "\",");
		sb.append("\"name\":").append("\"" + cinvCcode+"-"+cinvCname + "\",");
		sb.append("\"binvCend\":").append("\"" + binvCend + "\"");
		// sb.append("\"iinvCgrade\":").append("\""+iinvCgrade+"\",");
		// sb.append("\"binvCend\":").append("\""+binvCend+"\",");
		// sb.append("\"cecoCode\":").append("\""+cecoCode+"\",");
		// sb.append("\"cbarCode\":").append("\""+cbarCode+"\",");
		// sb.append("\"pubufts\":").append("\""+pubufts+"\"");
		sb.append("}");
		if (children != null && children.size() > 0) {
			sb.append(",\"children\":[");
			for (int i = 0; i < children.size(); i++) {
				InventoryClass iv = children.get(i);
				sb.append(iv.toJSON());
				if (i != children.size() - 1) {
					sb.append(",");
				}
			}
			sb.append("]");
		}
		sb.append("}");
		return sb.toString();
	}
	public String toJSONForJQuery() { ////这里是获取jquerytree data
		StringBuilder sb = new StringBuilder("{");
		sb.append("\"id\":").append("\"" + cinvCcode + "\",");
		sb.append("\"name\":").append("\"" + cinvCcode+"-"+cinvCname + "\",");
		sb.append("\"binvCend\":").append("\"" + binvCend + "\"");
		// sb.append("\"iinvCgrade\":").append("\""+iinvCgrade+"\",");
		// sb.append("\"binvCend\":").append("\""+binvCend+"\",");
		// sb.append("\"cecoCode\":").append("\""+cecoCode+"\",");
		// sb.append("\"cbarCode\":").append("\""+cbarCode+"\",");
		// sb.append("\"pubufts\":").append("\""+pubufts+"\"");
		if (children != null && children.size() > 0) {
			sb.append(",\"children\":[");
			for (int i = 0; i < children.size(); i++) {
				InventoryClass iv = children.get(i);
				sb.append(iv.toJSONForJQuery());
				if (i != children.size() - 1) {
					sb.append(",");
				}
			}
			sb.append("]");
		}
		sb.append("}");
		return sb.toString();
	}

	public String getParentCinvCcode() {
		return parentCinvCcode;
	}

	public void setParentCinvCcode(String parentCinvCcode) {
		this.parentCinvCcode = parentCinvCcode;
	}

}

 

用的是jsonp get方式获取的 json对象.

 

大致的流程代码都贴上去了. 

 

程序代码没法给大家整个共享出来.

 

 

 

好人呀!!

0 请登录后投票
论坛首页 Web前端技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics