java_easyui体系之Tree的加载(二)

2014-11-24 03:26:50 · 作者: · 浏览: 1
(count > 0){ //注意state这个属性、当他为open时、说明这个节点是个文件夹、会以文件夹的形式显示、 //当他是closed的时候、说明这个节点是一个具体的文件节点、不会以文件夹的形式显示。 e.setState("closed"); } eList.add(e); } } id = null; //将含有用于显示tree的信息的集合、转换成json格式、传到前台。 WriteJson.pwObj(eList); }
底层DAO中代码:


	public List
  
    getTreesByParentId(String id) {
		StringBuffer hql = new StringBuffer();
		if(id == null || "".equals(id)){
			hql.append("from TreeDTO t where t.pid is null ");
		}else{
			hql.append("from TreeDTO t where t.pid = '"+id+"' ");
		}	
		List
   
     list = new ArrayList
    
     (); try { list = this.getHibernateTemplate().find(hql.toString()); } catch (DataAccessException e) { e.printStackTrace(); } return list; }
    
   
  


iii. 将查询、处理后的结果集使用json工具处理成json格式字符串、传到前台用于显示

WriteJson.pwObj(eList)的实现:


package com.chy.ssh.utils;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;

import org.apache.struts2.ServletActionContext;
import org.codehaus.jackson.JsonFactory;
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.map.ObjectMapper;

public class WriteJson {
	public static String getJson(Object o) {
		StringWriter sw = new StringWriter();
		try {
			ObjectMapper om = new ObjectMapper();
			JsonGenerator jg = new JsonFactory().createJsonGenerator(sw);
			om.writeva lue(jg, o);
			jg.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return sw.toString();
	}

	public static void pwObj(Object obj){
		try {
			PrintWriter pw = ServletActionContext.getResponse().getWriter();
			pw.print(getJson(obj));
			pw.flush();
			pw.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

4、使用ajax一次性加载树

有前面的东西这里就很简单了:只要在初始化树时为其添加一个onLoadSuccess事件就好、代码:

//异步动态加载树
		$('#tt3').tree({
			url : 'treeAction_treeLoad.action',
			lines : true,
			checkbox : true,
			onLoadSuccess : function(node, data) {

				console.info(node);
				console.info(data);
				var t = $(this);
				if (data) {
					$(data).each(function(index, d) {
						if (this.state == 'closed') {
							t.tree('expandAll');
						}
					});
				}
			}
		});

四:源码补充


1、tree.jsp:

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






My JSP 'tree.jsp' starting page


  

  

  

  

  

  
<script type="text/java script">
	$(function() {
		//使用java script初始化
		$('#tt2').tree({
			data : [ {
				"id" : 1,
				"text" : "Folder1",
				"iconCls" : "icon-save",
				"children" : [ {
					"text" : "File1",
					"checked" : true
				}, {
					"text" : "Books",
					"state" : "open",
					"attributes" : {
						"url" : "/demo/book/abc",
						"price" : 100
					},
					"children" : [ {
						"text" : "PhotoShop",
						"checked" : true
					}, {
						"id" : 8,
						"text" : "Sub Bookds",
						"state" : "closed"
					} ]
				} ]
			}, {
				"text" : "Languages",
				"state" : "closed",
				"children" : [ {
					"text" : "Java"
				}, {
					"text" : "C#"
				} ]
			} ]
		});

		//异步动态加载树
		$('#tt3').tree({
			url : 'treeAction_treeLoad.action',
			lines : true,
			checkbox : true,
			onLoadSuccess : function(node, data) {

				console.info(node);
				console.info(data);
				var t = $(this);
				if (data) {
					$(data).each(function(index, d) {
						if (this.state == 'closed') {
							t.tree('expandAll');
						}
					});
				}
			}
		});

	});




	

initial tree by cla