EasyUI_tree根据数据库数据非迭代生成树形结构

 

 

我们经常要根据数据库表的结构,生成树形结构在页面显示;下面就是一个例子:

页面的tree组件采用的是EasyUI 的 Tree 组件。

数据库结构:

表名称: tDict

Id name parentid sortid valid

主键 名称 父ID 排序ID 是否可用

tDict 实体类中,父ID以 tDict 实体类表述,如下:

[java] view plaincopyprint?

  1. public class TDict{
  2. // Fields
  3. private String id;
  4. private TDict tDict;
  5. private Integer sortid;
  6. private String valid;
  7. private String name;
  8. ……省略<Get & Set>
  9. }

public class TDict{ // Fields private String id; private TDict tDict; private Integer sortid; private String valid; private String name; ……省略<Get & Set> }

首先,建立数节点类:

[java] view plaincopyprint?

  1. import java.util.ArrayList;
  2. import java.util.HashMap;
  3. import java.util.List;
  4. import java.util.Map;
  5. public class TreeNode {
  6. privateString id; //要显示的子节点的ID
  7. privateString text; //要显示的子节点的 Text
  8. privateString iconCls; //节点的图标
  9. privateString parentId; //父节点的ID
  10. privateList<TreeNode> children; //孩子节点的List
  11. publicTreeNode(){}
  12. publicTreeNode(String id, String text, String iconCls, String parentId,
  13. List<TreeNode>children) {
  14. super();
  15. this.id= id;
  16. this.text= text;
  17. this.iconCls= iconCls;
  18. this.parentId= parentId;
  19. this.children= children;
  20. }
  21. publicString getId() {
  22. returnid;
  23. }
  24. publicvoid setId(String id) {
  25. this.id= id;
  26. }
  27. publicString getText() {
  28. returntext;
  29. }
  30. publicvoid setText(String text) {
  31. this.text= text;
  32. }
  33. publicString getIconCls() {
  34. returniconCls;
  35. }
  36. publicvoid setIconCls(String iconCls) {
  37. this.iconCls= iconCls;
  38. }
  39. publicString getParentId()
  40. returnparentId;
  41. }
  42. publicvoid setParentId(String parentId) {
  43. this.parentId= parentId;
  44. }
  45. publicList<TreeNode> getChildren() {
  46. returnchildren;
  47. }
  48. publicvoid setChildren(List<TreeNode> children) {
  49. this.children= children;
  50. }
  51. //添加孩子的方法
  52. publicvoid addChild(TreeNode node){
  53. if(this.children == null){
  54. children= new ArrayList<TreeNode>();
  55. children.add(node);
  56. }else{
  57. children.add(node);
  58. }
  59. }
  60. }

import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class TreeNode { privateString id; //要显示的子节点的ID privateString text; //要显示的子节点的 Text privateString iconCls; //节点的图标 privateString parentId; //父节点的ID privateList<TreeNode> children; //孩子节点的List publicTreeNode(){} publicTreeNode(String id, String text, String iconCls, String parentId, List<TreeNode>children) { super(); this.id= id; this.text= text; this.iconCls= iconCls; this.parentId= parentId; this.children= children; } publicString getId() { returnid; } publicvoid setId(String id) { this.id= id; } publicString getText() { returntext; } publicvoid setText(String text) { this.text= text; } publicString getIconCls() { returniconCls; } publicvoid setIconCls(String iconCls) { this.iconCls= iconCls; } publicString getParentId() returnparentId; } publicvoid setParentId(String parentId) { this.parentId= parentId; } publicList<TreeNode> getChildren() { returnchildren; } publicvoid setChildren(List<TreeNode> children) { this.children= children; } //添加孩子的方法 publicvoid addChild(TreeNode node){ if(this.children == null){ children= new ArrayList<TreeNode>(); children.add(node); }else{ children.add(node); } } }

下面是生成树的方法:

[java] view plaincopyprint?

  1. @SuppressWarnings("unchecked")
  2. public List fillTree(String tableName){
  3. String hql = " from TDictt where valid='1' order by t.sortid ";
  4. List<TreeNode> list = new ArrayList<TreeNode>();
  5. Map map = new HashMap<String, TreeNode>();
  6. try {
  7. //拉出数据库的数据,放入list2中
  8. ArrayList<TDict> list2 = (ArrayList<TDict>)this.find(hql);
  9. //将list2中的数据,转换成TreeNode类型,放入Map中备用
  10. for (TDict tDict : list2) {
  11. TreeNode node = new TreeNode();
  12. node.setId(tDict.getId());
  13. node.setText(tDict.getName());
  14. if(tDict.gettDict()!=null){
  15. node.setParentId(tDict.gettDict().getId());
  16. }
  17. map.put(tDict.getId(), node);
  18. }
  19. //遍历list2的数据,把每个节点加入他的父节点的孩子List
  20. for (TDict tDict : list2) {
  21. if(tDict.gettDict()!= null){
  22. if(tDict.gettDict().getId() == null)
  23. {
  24. list.add((TreeNode)map.get(tDict.getId()));
  25. }else{
  26. String pidString = tDict.gettDict().getId();
  27. TreeNode pnode = (TreeNode)map.get(pidString);
  28. TreeNode cnode=(TreeNode)map.get(tDict.getId());
  29. pnode.addChild(cnode);
  30. }
  31. }else{
  32. list.add((TreeNode)map.get(tDict.getId()));
  33. }
  34. }
  35. }catch (Exception e) {
  36. // TODO: handleexception
  37. e.printStackTrace();
  38. }
  39. return list;
  40. }

@SuppressWarnings("unchecked") public List fillTree(String tableName){ String hql = " from TDictt where valid='1' order by t.sortid "; List<TreeNode> list = new ArrayList<TreeNode>(); Map map = new HashMap<String, TreeNode>(); try { //拉出数据库的数据,放入list2中 ArrayList<TDict> list2 = (ArrayList<TDict>)this.find(hql); //将list2中的数据,转换成TreeNode类型,放入Map中备用 for (TDict tDict : list2) { TreeNode node = new TreeNode(); node.setId(tDict.getId()); node.setText(tDict.getName()); if(tDict.gettDict()!=null){ node.setParentId(tDict.gettDict().getId()); } map.put(tDict.getId(), node); } //遍历list2的数据,把每个节点加入他的父节点的孩子List for (TDict tDict : list2) { if(tDict.gettDict()!= null){ if(tDict.gettDict().getId() == null) { list.add((TreeNode)map.get(tDict.getId())); }else{ String pidString = tDict.gettDict().getId(); TreeNode pnode = (TreeNode)map.get(pidString); TreeNode cnode=(TreeNode)map.get(tDict.getId()); pnode.addChild(cnode); } }else{ list.add((TreeNode)map.get(tDict.getId())); } } }catch (Exception e) { // TODO: handleexception e.printStackTrace(); } return list; }

在页面显示的时候,把list转换成Json格式:

[java] view plaincopyprint?

  1. List treeSet = treeManagerImpl.fillTree(null);
  2. String treeString = gson.toJson(treeSet);
  3. session.setAttribute("tree", treeString);

List treeSet = treeManagerImpl.fillTree(null); String treeString = gson.toJson(treeSet); session.setAttribute("tree", treeString);

在页面取Json数据,并显示:

[javascript] view plaincopyprint?

  1. <script type="text/javascript">
  2. window.onload =function(){
  3. tree = ${tree};
  4. $('#tt').tree({
  5. data:tree
  6. });
  7. };
  8. </script>
posted @ 2012-05-18 14:13  易独  阅读(701)  评论(0编辑  收藏  举报
乐巴儿 一个有声音的公众号
长按,识别二维码,加关注