为Extjs 树提供数据的递归算法
来源:互联网 发布:windows基于什么内核 编辑:程序博客网 时间:2024/06/10 05:11
(转自:http://emlyn1180.iteye.com/blog/315334 )
首先先建立Node模型
下面这个类先手工建立List,在实际应用中是从数据库读取List,然后再Main方法里调用递归方法,得到Json字符串
import java.util.ArrayList;import java.util.Iterator;import java.util.List;public class Recursion {List nodeList =new ArrayList();Recursion(){//构造方法里初始化模拟ListNode node1 = new Node(1,0); Node node2 = new Node(2,1); Node node3 = new Node(3,1); Node node4 = new Node(4,2); Node node5 = new Node(5,2); Node node6 = new Node(6,2); Node node7 = new Node(7,6); Node node8 = new Node(8,6); nodeList.add(node1); nodeList.add(node2); nodeList.add(node3); nodeList.add(node4); nodeList.add(node5); nodeList.add(node6); nodeList.add(node7); nodeList.add(node8); }StringBuffer returnStr=new StringBuffer(); public void recursionFn(List list , Node node){ if(hasChild(list,node)){ returnStr.append("{id:"); returnStr.append(node.getId()); returnStr.append(",parentId:"); returnStr.append(node.getParentId()); returnStr.append(",children:["); List childList = getChildList(list,node); Iterator it = childList.iterator(); while(it.hasNext()){ Node n = (Node)it.next(); recursionFn(list,n); } returnStr.append("]},"); }else{ returnStr.append("{id:"); returnStr.append(node.getId()); returnStr.append(",parentId:"); returnStr.append(node.getParentId()); returnStr.append(",leaf:true},"); } } public boolean hasChild(List list, Node node){ //判断是否有子节点 return getChildList(list,node).size()>0?true:false; } public List getChildList(List list , Node node){ //得到子节点列表 List li = new ArrayList(); Iterator it = list.iterator(); while(it.hasNext()){ Node n = (Node)it.next(); if(n.getParentId()==node.getId()){ li.add(n); } } return li; } public String modifyStr(String returnStr){//修饰一下才能满足Extjs的Json格式 return ("["+returnStr+"]").replaceAll(",]", "]"); } public static void main(String[] args) { Recursion r = new Recursion(); r.recursionFn(r.nodeList, new Node(1,0)); System.out.println(r.modifyStr(r.returnStr.toString())); } }
import java.util.ArrayList;import java.util.Iterator;import java.util.List;public class Recursion {List nodeList =new ArrayList();Recursion(){//构造方法里初始化模拟ListNode node1 = new Node(1,0); Node node2 = new Node(2,1); Node node3 = new Node(3,1); Node node4 = new Node(4,2); Node node5 = new Node(5,2); Node node6 = new Node(6,2); Node node7 = new Node(7,6); Node node8 = new Node(8,6); nodeList.add(node1); nodeList.add(node2); nodeList.add(node3); nodeList.add(node4); nodeList.add(node5); nodeList.add(node6); nodeList.add(node7); nodeList.add(node8); }StringBuffer returnStr=new StringBuffer(); public void recursionFn(List list , Node node){ if(hasChild(list,node)){ returnStr.append("{id:"); returnStr.append(node.getId()); returnStr.append(",parentId:"); returnStr.append(node.getParentId()); returnStr.append(",children:["); List childList = getChildList(list,node); Iterator it = childList.iterator(); while(it.hasNext()){ Node n = (Node)it.next(); recursionFn(list,n); } returnStr.append("]},"); }else{ returnStr.append("{id:"); returnStr.append(node.getId()); returnStr.append(",parentId:"); returnStr.append(node.getParentId()); returnStr.append(",leaf:true},"); } } public boolean hasChild(List list, Node node){ //判断是否有子节点 return getChildList(list,node).size()>0?true:false; } public List getChildList(List list , Node node){ //得到子节点列表 List li = new ArrayList(); Iterator it = list.iterator(); while(it.hasNext()){ Node n = (Node)it.next(); if(n.getParentId()==node.getId()){ li.add(n); } } return li; } public String modifyStr(String returnStr){//修饰一下才能满足Extjs的Json格式 return ("["+returnStr+"]").replaceAll(",]", "]"); } public static void main(String[] args) { Recursion r = new Recursion(); r.recursionFn(r.nodeList, new Node(1,0)); System.out.println(r.modifyStr(r.returnStr.toString())); } }
Main方法运行效果如下:
[{id:1,parentId:0,children:[{id:2,parentId:1,children:[{id:4,parentId:2,leaf:true},{id:5,parentId:2,leaf:true},{id:6,par
entId:2,children:[{id:7,parentId:6,leaf:true},{id:8,parentId:6,leaf:true}]}]},{id:3,parentId:1,leaf:true}]}]
在具体的应用中稍加修改即可
(记得,return 要" return r.modifyStr(r.returnStr.toString()); "
- 为Extjs 树提供数据的递归算法
- extjs递归算法生成树
- ExtJs树的递归算法(Java),Json格式
- ExtJs树的递归算法(Java),Json格式
- ExtJs树的递归算法(Java),Json格式
- ExtJs树的递归算法(Java),Json格式
- 让VS2008为ExtJS提供智能提示
- 让VS2008为ExtJS提供智能提示
- 让VS2008为ExtJS提供智能提示
- 让VS2008为ExtJS提供智能提示
- 递归算法转换为非递归算法的技巧
- ExtJs读取Json数据中类型为DateTime的数据
- extjs 递归删除树
- ExtJs的GridPanel通过Spring Mvc--jsp页面提供的json,无法加载数据
- Extjs 将grid的数据批量保存为arrayjson提交
- 采用数据库为Flex Tree组件的提供数据-前言
- 用DataSource控件以外的方法为GridView提供数据
- 使用Adapter为ListView提供数据的问题
- C语言使用注意事项(四)
- activity之生命周期(笔记)
- 关于工作后定居城市的思考以及房价预测(天涯原创)
- xheditor应用
- 文件批量 复制 粘贴 删除 格式限定
- 为Extjs 树提供数据的递归算法
- <filter-mapping>中的<dispatcher>元素
- 图像处理基本算法 动态阈值分割
- 【整理】ubuntu ln命令简单学习
- iphone应用程序开发指南(笔记2)
- 加密算法之BLOWFISH算法
- C# 屏蔽移动盘
- grub 命令行用法
- POJ 1163 求数字三角形由顶到底边的最大数字和 动态规划