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

来源:互联网 发布:highlight.min.js下载 编辑:程序博客网 时间:2024/06/06 16:36


 

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

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

 

数据库结构:

表名称: tDict

Id      name    parentid    sortid         valid

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

 

 

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

 

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

要用到Hibernate的支持,以下是模型层的映射文件 hbm

<?xml version="1.0" encoding="utf-8"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><!--     Mapping file autogenerated by MyEclipse Persistence Tools--><hibernate-mapping default-lazy="false">    <class name="com.bzinfo.crm.model.TDict" table="T_DICT">        <id name="id" type="java.lang.String">            <column name="ID" length="50" />            <generator class="assigned" />        </id>        <many-to-one name="tDict" class="com.bzinfo.crm.model.TDict"   lazy="false">            <column name="PARENTID" length="50" />        </many-to-one>        <property name="sortid" type="java.lang.Integer">            <column name="SORTID" precision="6" scale="0" />        </property>        <property name="valid" type="java.lang.String">            <column name="VALID" length="1" />        </property>        <property name="name" type="java.lang.String">            <column name="NAME" length="50" />        </property></hibernate-mapping>



 

首先,建立数节点类:

 

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);       }              }   }


 

下面是生成树的方法:

 

@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格式:

 

 

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

 

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

 

<script type="text/javascript">window.onload =function(){       tree = ${tree};       $('#tt').tree({         data:tree        });     };</script>


原创粉丝点击