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

19 篇文章 0 订阅
5 篇文章 0 订阅


 

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

页面的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>


  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 15
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 15
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值