123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- package com.hcks.cmfds.commons.lang;
- import java.io.Serializable;
- import java.util.ArrayList;
- import java.util.List;
- /**
- * easui中的tree_data.json数据,只能有一个root节点
- * [{
- "id":1,
- "text":"Folder1",
- "iconCls":"icon-save",
- "children":[{
- "text":"File1",
- "checked":true
- }]
- }]
- * 提供静态方法formatTree(List<TreeJson> list) 返回结果
- * TreeJson.formatTree(treeJsonlist) ;
- * @author lwb
- *
- */
- public class TreeNode1 implements Serializable {
- /**
- *
- */
- private static final long serialVersionUID = 1L;
- private String id ;
- private String pid ;
- private String text ;
- private String url ;
- private String iconCls ;
- private String state ;
- private String checked ;
- private List<TreeNode1> children = new ArrayList<TreeNode1>() ;
-
- /******** setter and getter **********/
- public static List<TreeNode1> formatTree(List<TreeNode1> list) {
- TreeNode1 root = new TreeNode1();
- TreeNode1 node = new TreeNode1();
- List<TreeNode1> treelist = new ArrayList<TreeNode1>();// 拼凑好的json格式的数据
- List<TreeNode1> parentnodes = new ArrayList<TreeNode1>();// parentnodes存放所有的父节点
-
- if (list != null && list.size() > 0) {
- root = list.get(0) ;
- //循环遍历oracle树查询的所有节点
- for (int i = 1; i < list.size(); i++) {
- node = list.get(i);
- if(node.getPid().equals(root.getId())){
- //为tree root 增加子节点
- parentnodes.add(node) ;
- root.getChildren().add(node) ;
- }else{//获取root子节点的孩子节点
- getChildrenNodes(parentnodes, node);
- parentnodes.add(node) ;
- }
- }
- }
- treelist.add(root) ;
- return treelist ;
- }
- private static void getChildrenNodes(List<TreeNode1> parentnodes, TreeNode1 node) {
- //循环遍历所有父节点和node进行匹配,确定父子关系
- for (int i = parentnodes.size() - 1; i >= 0; i--) {
-
- TreeNode1 pnode = parentnodes.get(i);
- //如果是父子关系,为父节点增加子节点,退出for循环
- if (pnode.getId().equals(node.getPid())) {
- pnode.setState("closed") ;//关闭二级树
- pnode.getChildren().add(node) ;
- return ;
- } else {
- //如果不是父子关系,删除父节点栈里当前的节点,
- //继续此次循环,直到确定父子关系或不存在退出for循环
- parentnodes.remove(i) ;
- }
- }
- }
- 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 String getText() {
- return text;
- }
- public void setText(String text) {
- this.text = text;
- }
- public String getIconCls() {
- return iconCls;
- }
- public void setIconCls(String iconCls) {
- this.iconCls = iconCls;
- }
- public String getState() {
- return state;
- }
- public void setState(String state) {
- this.state = state;
- }
- public String getChecked() {
- return checked;
- }
- public void setChecked(String checked) {
- this.checked = checked;
- }
- public String getUrl() {
- return url;
- }
- public void setUrl(String url) {
- this.url = url;
- }
- public List<TreeNode1> getChildren() {
- return children;
- }
- public void setChildren(List<TreeNode1> children) {
- this.children = children;
- }
-
- }
|