精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2012-03-22
最后修改:2012-03-22
var zNodes =[ { id:1, pId:0, name:"父节点 1", open:true}, { id:11, pId:1, name:"叶子节点 1-1"}, { id:12, pId:1, name:"叶子节点 1-2"}, { id:13, pId:1, name:"叶子节点 1-3"}, { id:2, pId:0, name:"父节点 2", open:true}, { id:21, pId:2, name:"叶子节点 2-1"}, { id:22, pId:2, name:"叶子节点 2-2"}, { id:23, pId:2, name:"叶子节点 2-3"}, { id:3, pId:0, name:"父节点 3", open:true}, { id:31, pId:3, name:"叶子节点 3-1"}, { id:32, pId:3, name:"叶子节点 3-2"}, { id:33, pId:3, name:"叶子节点 3-3"} ]; java文件递归: static List sl =new ArrayList(); public void listName (File root,List li){ File [] fileList =root.listFiles(); int len = fileList.length; for (int i = 0; i < len; i++) { if(fileList[i].isDirectory()){ List sli =new ArrayList(); listName(fileList[i], sli ); sl.add(sli); }else{ String fileName = fileList[i].getName(); li.add(fileName); } } 请问如何才能实现ztree 格式的要求啊? 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2012-03-23
没搞明白为什么要递归? 表结构也这样设计 直接读出来不就行了?
|
|
返回顶楼 | |
发表时间:2012-03-24
不用递归,如果树节点不是特别多(比如一次加载超过500个节点,节点多考虑异步)。就直接读出来然后将结果用jackson把它转为simple格式的json。相当方便!
|
|
返回顶楼 | |
发表时间:2012-03-25
如果要在同一个页面动态构建多个树怎么操作呢
|
|
返回顶楼 | |
发表时间:2012-03-25
最后修改:2012-03-25
/** * FileName TreeNode.java * Description 树节点数据 * All rights Reserved, Designed By SK * Copyright Copyright(C) 2010-2011 * Company SK * @author ry * @version V1.0 * Createdate 2012-3-8 下午10:06:08 * * Modification History: * Date Author Version Discription * ----------------------------------------------------------------------------------- * 2012-3-8 r.y 1.0 create * Why & What is modified: <修改原因描述> */ package com.s4ysoft.framework.dto; import java.io.Serializable; import java.util.Collection; import java.util.Comparator; import java.util.HashMap; import java.util.Map; /** * 树节点数据 * * @ClassName TreeNode * @Description 树节点数据 * @author ry * @date 2012-3-8 下午10:06:08 */ public abstract class TreeNode implements Serializable { private static final long serialVersionUID = -697743058726107859L; /** ID */ protected String id; /** 父ID */ protected String pid; /** 排序 */ protected int sortNo; /** 当前节点层级 */ protected int level; /** 是否叶子节点 */ protected boolean leaf; /** 数据 */ protected Map<String, Object> data; /** 孩子节点 */ protected Collection<TreeNode> child; protected TreeNode() { this.leaf = true; this.data = new HashMap<String, Object>(); } protected TreeNode(String id, String pid, Map<String, Object> data) { this.id = id; this.pid = pid; this.leaf = true; this.data = (data == null) ? (new HashMap<String, Object>()) : data; } /** * 添加孩子节点 * * @param childNode */ protected abstract void addChildNode(TreeNode childNode); /** * 对Child进行排序 * * @param isAsc true:升序;false:降序 */ protected abstract void sortChild(boolean isAsc); /** * 查找指定ID的节点 * * @param id * @return */ protected abstract TreeNode getTreeNodeById(String id); /** * 移除所有孩子节点 */ protected abstract void removeChildNode(); /** * 移除指定ID的节点 * * @param id * @return */ protected abstract void removeChildNodeById(String id); public String getId() { return id; } public void setId(String id) { this.id = id; } public String getPid() { return pid; } public void setPid(String pid) { this.pid = pid; } public int getSortNo() { return sortNo; } public void setSortNo(int sortNo) { this.sortNo = sortNo; } public int getLevel() { return level; } public void setLevel(int level) { this.level = level; } public boolean isLeaf() { return leaf; } public void setLeaf(boolean leaf) { this.leaf = leaf; } public Map<String, Object> getData() { return data; } public void setData(Map<String, Object> data) { this.data = data; } public Collection<TreeNode> getChild() { return child; } public void setChild(Collection<TreeNode> child) { this.child = child; } /** * TreeNode比较类 * * @ClassName TreeNodeComparator * @Description TreeNode比较类 * @author ry * @date 2012-3-9 下午02:56:22 */ class TreeNodeComparator implements Serializable, Comparator<TreeNode> { private static final long serialVersionUID = -6291371529241047911L; /** 是否升序 */ private boolean isAsc; /** * 默认升序构造方法 * * @Title TreeNodeComparator */ public TreeNodeComparator() { this.isAsc = true; } /** * 指定排序构造方法 * * @Title TreeNodeComparator * @param isAsc */ public TreeNodeComparator(boolean isAsc) { this.isAsc = isAsc; } @Override public int compare(TreeNode o1, TreeNode o2) { if (o1 != null && o2 != null) { int result = o2.getSortNo() - o1.getSortNo(); if (result == 0) { result = o2.getId().compareTo(o1.getId()); } if (isAsc) { result = -result; } return result; } return 0; } public boolean isAsc() { return isAsc; } public void setAsc(boolean isAsc) { this.isAsc = isAsc; } } } /** * FileName TreeNodeSet.java * Description 树节点数据 * All rights Reserved, Designed By SK * Copyright Copyright(C) 2010-2011 * Company SK * @author ry * @version V1.0 * Createdate 2012-3-8 下午10:06:08 * * Modification History: * Date Author Version Discription * ----------------------------------------------------------------------------------- * 2012-3-8 r.y 1.0 create * Why & What is modified: <修改原因描述> */ package com.s4ysoft.framework.dto; import java.io.Serializable; import java.util.Iterator; import java.util.Map; import java.util.Set; import java.util.TreeSet; /** * 树节点数据 * * @ClassName TreeNodeSet * @Description 树节点数据 * @author ry * @date 2012-3-8 下午10:06:08 */ public class TreeNodeSet extends TreeNode implements Serializable { private static final long serialVersionUID = 2544265242831973399L; public TreeNodeSet() { super(); this.child = new TreeSet<TreeNode>(new TreeNodeComparator()); } public TreeNodeSet(boolean isAsc) { super(); this.child = new TreeSet<TreeNode>(new TreeNodeComparator(isAsc)); } public TreeNodeSet(String id, String pid, Map<String, Object> data) { super(id, pid, data); this.child = new TreeSet<TreeNode>(new TreeNodeComparator()); } public TreeNodeSet(String id, String pid, Map<String, Object> data, boolean isAsc) { super(id, pid, data); this.child = new TreeSet<TreeNode>(new TreeNodeComparator(isAsc)); } public TreeNodeSet(String id, String pid, Map<String, Object> data, Set<TreeNode> child) { super(id, pid, data); this.child = (child == null) ? (new TreeSet<TreeNode>(new TreeNodeComparator())) : child; if (child != null && child.size() != 0) { this.leaf = false; } else { this.leaf = true; } } public TreeNodeSet(String id, String pid, Map<String, Object> data, Set<TreeNode> child, boolean isAsc) { super(id, pid, data); this.child = (child == null) ? (new TreeSet<TreeNode>(new TreeNodeComparator(isAsc))) : child; if (child != null && child.size() != 0) { this.leaf = false; } else { this.leaf = true; } } /** * 添加孩子节点 * * @param childNode */ public void addChildNode(TreeNode childNode) { if (childNode != null) { childNode.level = this.level + 1; this.leaf = false; this.child.add(childNode); } } /** * 对Child进行排序 * * @param isAsc true:升序;false:降序 */ public void sortChild(boolean isAsc) { return; } /** * 查找指定ID的节点 * * @param id * @return */ public TreeNode getTreeNodeById(String id) { if (this.id != null && this.id.equals(id)) { return this; } if (this.child != null && this.child.size() != 0) { Iterator<TreeNode> iterator = this.child.iterator(); while (iterator.hasNext()) { TreeNode node = iterator.next(); TreeNode subNode = node.getTreeNodeById(id); if (subNode == null) { continue; } return subNode; } } return null; } /** * 移除所有孩子节点 */ public void removeChildNode() { if (this.child != null && this.child.size() != 0) { this.child.clear(); } } /** * 移除指定ID的节点 * * @param id * @return */ public void removeChildNodeById(String id) { if (this.child != null && this.child.size() != 0) { Iterator<TreeNode> iterator = this.child.iterator(); while (iterator.hasNext()) { TreeNode node = iterator.next(); if (node == null || node.getId() == null) { continue; } if (!node.getId().equals(id)) { node.removeChildNodeById(id); } else { iterator.remove(); } } } } } 自己可以根据需求在TreeNode中增加需要的属性。 |
|
返回顶楼 | |
发表时间:2012-03-25
用TreeSet能解决排序的问题,只是效率低一点,如果你的数据库是Oracle的话,就用sql把数据准备好,直接查询的结果就是树形结构,就可以不用TreeSet了,用list。
|
|
返回顶楼 | |
发表时间:2012-03-25
/** * FileName TreeNodeList.java * Description 树节点数据 * All rights Reserved, Designed By SK * Copyright Copyright(C) 2010-2011 * Company SK * @author ry * @version V1.0 * Createdate 2012-3-8 下午10:06:08 * * Modification History: * Date Author Version Discription * ----------------------------------------------------------------------------------- * 2012-3-8 r.y 1.0 create * Why & What is modified: <修改原因描述> */ package com.s4ysoft.framework.dto; import java.io.Serializable; import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; import java.util.List; import java.util.Map; /** * 树节点数据 * * @ClassName TreeNodeList * @Description 树节点数据 * @author ry * @date 2012-3-8 下午10:06:08 */ public class TreeNodeList extends TreeNode implements Serializable { private static final long serialVersionUID = 4354844612924223052L; public TreeNodeList() { super(); this.child = new ArrayList<TreeNode>(); } public TreeNodeList(String id, String pid, Map<String, Object> data) { super(id, pid, data); this.child = new ArrayList<TreeNode>(); } public TreeNodeList(String id, String pid, Map<String, Object> data, List<TreeNode> child) { super(id, pid, data); this.child = (child == null) ? (new ArrayList<TreeNode>()) : child; if (child != null && child.size() != 0) { this.leaf = false; } else { this.leaf = true; } } /** * 添加孩子节点 * * @param childNode */ public void addChildNode(TreeNode childNode) { if (childNode != null) { childNode.level = this.level + 1; this.leaf = false; this.child.add(childNode); } } /** * 对Child进行排序 * * @param isAsc true:升序;false:降序 */ @SuppressWarnings("unchecked") public void sortChild(boolean isAsc) { if (this.child != null && this.child.size() != 0) { Collections.sort((List) this.child, new TreeNodeComparator(isAsc)); Iterator<TreeNode> iterator = this.child.iterator(); while (iterator.hasNext()) { TreeNode node = iterator.next(); node.sortChild(isAsc); } } } /** * 查找指定ID的节点 * * @param id * @return */ public TreeNode getTreeNodeById(String id) { if (this.id != null && this.id.equals(id)) { return this; } if (this.child != null && this.child.size() != 0) { Iterator<TreeNode> iterator = this.child.iterator(); while (iterator.hasNext()) { TreeNode node = iterator.next(); TreeNode subNode = node.getTreeNodeById(id); if (subNode == null) { continue; } return subNode; } } return null; } /** * 移除所有孩子节点 */ public void removeChildNode() { if (this.child != null && this.child.size() != 0) { this.child.clear(); } } /** * 移除指定ID的节点 * * @param id * @return */ public void removeChildNodeById(String id) { if (this.child != null && this.child.size() != 0) { Iterator<TreeNode> iterator = this.child.iterator(); while (iterator.hasNext()) { TreeNode node = iterator.next(); if (node == null || node.getId() == null) { continue; } if (!node.getId().equals(id)) { node.removeChildNodeById(id); } else { iterator.remove(); } } } } } |
|
返回顶楼 | |
发表时间:2012-03-25
测试
public class TreeNodeTest implements Serializable { private static final long serialVersionUID = -697743058726107859L; public static void main(String[] args) { for (int ii = 0; ii < 5; ii++) { Map<String, TreeNode> map = new HashMap<String, TreeNode>(); for (int i = 0; i < 1; i++) { for (int j = 0; j < 2; j++) { for (int k = 0; k < ii; k++) { TreeNode nk = new TreeNodeSet((i + 1) + "-" + (j + 1) + "-" + (k + 1), (i + 1) + "-" + (j + 1), null); nk.setSortNo(k + 1); map.put(nk.getId(), nk); } TreeNode nj = new TreeNodeSet((i + 1) + "-" + (j + 1) + "", (i + 1) + "", null); nj.setSortNo(j + 1); map.put(nj.getId(), nj); } TreeNode ni = new TreeNodeSet((i + 1) + "", "root", null); ni.setSortNo(i + 1); map.put(ni.getId(), ni); } new Thread(new Test((ii + 1), map)).start(); } } } class Test implements Runnable { private int idx; private Map<String, TreeNode> map; public Test(int idx, Map<String, TreeNode> map) { this.idx = idx; this.map = map; } public void printNode() { TreeNode root = new TreeNodeSet(); for (Map.Entry<String, TreeNode> entry : map.entrySet()) { TreeNode node = entry.getValue(); String pid = node.getPid(); TreeNode pNode = map.get(pid); if (pNode == null) { root.addChildNode(node); } else { pNode.addChildNode(node); } } root.setSortNo(idx); //root.removeChildNodeById("1-2-1"); //root.removeChildNodeById("1-1-1"); System.out.println(this.idx + "=" + JacksonUtil.objToJson(root)); } public void run() { printNode(); } } |
|
返回顶楼 | |
发表时间:2012-03-25
不推荐用递归实现,可以考虑用异步加载机制,也就是一个节点一个节点地读取
|
|
返回顶楼 | |
发表时间:2012-03-25
我不是用递归实现的,就直接取出所有的结果,然后对结果进行处理。将结果集专程JSON对象后装载一个TreeMap里面,这个TreeMap的Key就是level,value也是一个TreeMap类型,key和value分别为json对象的id,和json对象自己。然后再将不同低级别的赋给高级别的。这样来回比几次,搞定。
|
|
返回顶楼 | |