tree

 

       这几天忙着移植树,原先项目里用到是静态树(xTree),因为数据比较少,因此一次性加载,没有太多效率的问题,后来因为涉及的数据太多,如果一次性加载会耗费6、7秒的时间,用户体验特别差,实在难以忍受,考虑一次性加载完成的这个问题,决定换成动态树(xLoadTree2),也就是每一次只加载树的一级节点,采用异步加载的方式,速度上的确是快了很多,用户体验也还可以,但多少有一些缺憾,后来便换成了另一种动态树(dhtmlxTree),个人感觉比xLoadTree2要更加强大,实现方法:

StringBuffer sb = new StringBuffer();
sb.append("<script language=\"javascript\">");
sb.append("tree=new dhtmlXTreeObject(\"" + divId
+ "\",\"100%\",\"100%\",\"newsCenter\");");//1
sb.append("tree.loadXML(\"/tree/list1.do?id=newsCenter\");");//2
sb.append("tree.setOnOpenEndHandler(doOpen);");//3
sb.append("tree.setXMLAutoLoading(\"/tree/list1.do\");");//4
sb.append("tree.setOnClickHandler(doClick);");//5
sb.append("tree.setImagePat(\"/images/dhtmlxtree/csh_bluebooks/\");");//6
sb.append("</script>");

 1,divId是页面中的document标识id,

 2,初次加载树时,获取树的路径,后面展示该方法

 3,doOpen是一个自定义的js函数,当打开树时所要处理的事情

 4,setXMLAutoLoading:自动加载数据

 5,doClick同样是一个自定义的js函数,用于处理点击节点时所要触发的事件,比如链接等等

 6,是树图片的路径

接下来是struts中生成xml(数据)的方法:

public String list1() throws IOException {// dhtmlxtree
		this.getResponse().setContentType("text/xml; charset=utf-8");
		PrintWriter out = this.getResponse().getWriter();
		String id = this.getRequest().getParameter("id");

		if (Util.isblank(id)) {
			doc = new Document(new Element("tree").setAttribute("id", "a"));
			Element tree = doc.getRootElement();
			Element inner = new Element("item").setAttribute("text", "/");
			inner.setAttribute("id", "0");
			inner.setAttribute("child", "1");
			tree.addContent(inner);
		} else {
			generateXML(id);
		}
		Format format = Format.getCompactFormat();
		format.setEncoding("utf-8");
		format.setIndent("\t");
		XMLOutputter xout = new XMLOutputter(format);

		xout.output(doc, out);
		out.flush();
		out.close();
		return null;
	}
private void transfer1(Document doc, List<Category> r, String type) {
		String oper = "";
		if (type.split("_").length > 1) {
			oper = type.split("_")[1];
		}
		if ("newsPublish".equals(type) || "add".equals(oper)) {// 区别是发布还是维护
			oper = "_add";
		}
		if ("newsModify".equals(type) || "list".equals(oper)) {
			oper = "_list";
		}
		for (Iterator<Category> it = r.iterator(); it.hasNext();) {
			Category one = (Category) it.next();
			Element tree = null;
			tree = doc.getRootElement();
			Element inner = new Element("item").setAttribute("text", one
					.getName());
			inner.setAttribute("id", "" + one.getId() + oper);
			if (!one.isLeaf()) {
				inner.setAttribute("child", "1");
			}
			tree.addContent(inner);
		}
	}
private void generateXML(String id) {
		if ("newsCenter".equals(id)) {// 信息管理中心
			doc = new Document(new Element("tree").setAttribute("id", id));
			Element tree = doc.getRootElement();
			Element inner = new Element("item").setAttribute("text", "信息发布");
			inner.setAttribute("id", "newsPublish");
			inner.setAttribute("child", "1");
			tree.addContent(inner);
			inner = new Element("item").setAttribute("text", "信息维护");
			inner.setAttribute("id", "newsModify");
			inner.setAttribute("child", "1");
			tree.addContent(inner);
		} else if ("newsPublish".equals(id) || "newsModify".equals(id)) {
			Category ca = categoryService.getByEname("newscenter");
			doc = new Document(new Element("tree").setAttribute("id", id));
			List<Category> list = categoryService.getCategorys(ca.getId(), "");
			transfer1(doc, list, id);
		}else {
			List<Category> list = categoryService.getCategorys(new Long(id
					.split("_")[0]), "");
			doc = new Document(new Element("tree").setAttribute("id", id));
			transfer1(doc, list, id);
		}
	}

 

 待续.......

你可能感兴趣的:(JavaScript,Ajax,xml,struts,项目管理)