我们经常要根据数据库表的结构,生成树形结构在页面显示;下面就是一个例子:
页面的tree组件采用的是EasyUI 的 Tree 组件。
数据库结构:
表名称: tDict
Id name parentid sortid valid
主键 名称 父ID 排序ID 是否可用
tDict 实体类中,父ID以 tDict 实体类表述,如下:
[java] view plaincopyprint?
- public class TDict{
- // Fields
- private String id;
- private TDict tDict;
- private Integer sortid;
- private String valid;
- private String name;
- ……省略<Get & Set>
- }
public class TDict{ // Fields private String id; private TDict tDict; private Integer sortid; private String valid; private String name; ……省略<Get & Set> }
首先,建立数节点类:
[java] view plaincopyprint?
- 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);
- }
- }
- }
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?
- @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;
- }
@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?
- List treeSet = treeManagerImpl.fillTree(null);
- String treeString = gson.toJson(treeSet);
- session.setAttribute("tree", treeString);
List treeSet = treeManagerImpl.fillTree(null); String treeString = gson.toJson(treeSet); session.setAttribute("tree", treeString);
在页面取Json数据,并显示:
[javascript] view plaincopyprint?
- <script type="text/javascript">
- window.onload =function(){
- tree = ${tree};
- $('#tt').tree({
- data:tree
- });
- };
- </script>