實現原理:樹的每一個節點都有自己的ID,和它的父親節點的ID,還有自己的文本內容,以及點擊後在哪個frame中打開哪個連接,是否是葉子節點等內容,樹的第一級節點的父親節點的ID我們將它置爲0,以後每次點解一個非葉子節點的時候,我們都去異步加載他的所有孩子結點,將信息組裝成JSON字符串,返回給前臺,前臺的EXT Tree使用JSON數據構造樹
主要步驟:
第一步:構造 樹的 表結構
- CREATE TABLE `ump_functions` (
- `item_id` int(11) NOT NULL,
- `item_name` varchar(60) DEFAULT NULL,
- `parent_id` int(11) DEFAULT NULL,
- `isleaf` tinyint(1) DEFAULT NULL,
- `Item_url` varchar(60) DEFAULT NULL,
- `remark` varchar(120) DEFAULT NULL,
- PRIMARY KEY (`item_id`)
- ) ENGINE=InnoDB DEFAULT CHARSET=gbk
第二步 構造和表關聯的 javaBean對象 FuctionTreeNode.java
- package cn.com.xinli.tree.bean;
- /**
- *
- * @author huxl
- *
- * 代表系統左邊的導航樹的節點,根據節點的信息 異步動態加載 extTree
- * 根節點的 父節點的id是0
- */
- public class FuctionTreeNode
- {
- /*樹節點id*/
- private int id;
- /*樹節點名稱*/
- private String text;
- /*樹節點url*/
- private String href;
- /*點擊葉子在指定的 frame中刷新*/
- private String hrefTarget="_blank";
- /*是否是葉子節點 */
- private boolean leaf;
- /*樹節點的樣式*/
- private String cls;
- public int getId()
- {
- return id;
- }
- public void setId(int id)
- {
- this.id = id;
- }
- public String getText()
- {
- return text;
- }
- public void setText(String text)
- {
- this.text = text;
- }
- public String getHref()
- {
- return href;
- }
- public void setHref(String href)
- {
- this.href = href;
- }
- public String getHrefTarget()
- {
- return hrefTarget;
- }
- public void setHrefTarget(String hrefTarget)
- {
- this.hrefTarget = hrefTarget;
- }
- public boolean isLeaf()
- {
- return leaf;
- }
- public void setLeaf(boolean leaf)
- {
- this.leaf = leaf;
- }
- public String getCls()
- {
- return cls;
- }
- public void setCls(String cls)
- {
- this.cls = cls;
- }
- }
第三步 根據節點id去找節點的孩子結點 FuctionTreeDaoImpl.java
- package cn.com.xinli.tree.dao.impl;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.ArrayList;
- import java.util.List;
- import org.apache.log4j.Logger;
- import cn.com.xinli.tree.dao.FuctionTreeDao;
- import cn.com.xinli.tree.bean.FuctionTreeNode;
- import cn.com.xinli.tree.util.DBUtil;
- public class FuctionTreeDaoImpl implements FuctionTreeDao
- {
- private Logger log=Logger.getLogger(FuctionTreeDaoImpl.class);
- public List<FuctionTreeNode> queryNodeById(String nodeId) throws Exception
- {
- Connection conn=null;
- PreparedStatement pstmt;
- ResultSet rs;
- List<FuctionTreeNode> nodeList=new ArrayList<FuctionTreeNode>();
- try {
- conn=DBUtil.getConnection();
- // String sql="select t.* from ump_functions t ,ump_role_function s where t.item_id = s.item_id and t.parent_id="+nodeId+" and s.role_id="+roleId ;
- String sql="select t.* from ump_functions t where t.parent_id="+nodeId;
- log.info("sql:"+sql);
- pstmt=conn.prepareStatement(sql);
- //pstmt.setInt(1, nodeId);
- rs=pstmt.executeQuery();
- while(rs.next())
- {
- FuctionTreeNode node=new FuctionTreeNode();
- node.setId(rs.getInt("item_id"));
- node.setText(rs.getString("item_name"));
- node.setLeaf(rs.getBoolean("isleaf"));
- node.setHref(rs.getString("item_url"));
- nodeList.add(node);
- }
- return nodeList;
- }
- catch (Exception e)
- {
- log.info("查詢節點出錯:", e);
- throw new Exception("查詢節點出錯");
- }
- finally
- {
- if(conn!=null)
- {
- try
- {
- conn.close();
- }
- catch (SQLException e)
- {
- log.info("數據庫連接出錯:", e);
- throw new Exception("數據庫連接出錯");
- }
- }
- }
- }
- }
第4步: 寫ext 的 tree 的js
- /*
- * Ext JS Library 2.0.1
- * Copyright(c) 2006-2008, Ext JS, LLC.
- * [email protected]
- *
- * http://extjs.com/license
- */
- Ext.onReady(function(){
- //從本地加載樹的圖片
- Ext.BLANK_IMAGE_URL = 'extjs/resources/images/vista/s.gif';
- var Tree = Ext.tree;
- var tree = new Tree.TreePanel({
- el:'tree-div',
- rootVisible:true, //隱藏根節點
- border:true, //邊框
- animate:true, //動畫效果
- autoScroll:true, //自動滾動
- enableDD:false, //拖拽節點
- containerScroll:true,
- loader: new Tree.TreeLoader({
- // dataUrl:'http://localhost:9090/struts2/menus.action'
- })
- });
- // set the root node
- var root = new Tree.AsyncTreeNode({
- text: '統一監控平臺',
- draggable:false,
- //樹的根節點的ID設置成0有個好處就是初始化樹的時候默認先加載父親節點爲0的孩子結點
- id:'0'
- });
- tree.setRootNode(root);
- tree.on('beforeload',
- function(node)
- {
- tree.loader.dataUrl='http://localhost:9090/struts2/menus!loadTree.action?pid='+node.id
- //tree.loader.dataUrl='treedata2.txt'
- });
- // render the tree
- tree.render();
- root.expand();
- //展開樹的所有節點,有一些特殊需求會要求我們一次展開所有的節點,傳true
- //root.expand(true);
- //只展開根節點
- root.expand();
- });
還有一個Ext 發送Ajax 請求的一個小例子 主要是使用到了 Ext.Ajax.request