Ext动态树

jsp页面代码:
<script type="text/javascript">

//生成标签页tab 我的index.jsp使用了viewport视图布局 此tab 要加载center中
var tab = new Ext.TabPanel({
region:'center',
deferredRender:false,
activeTab:0,
resizeTabs:true, // turn on tab resizing
minTabWidth: 115,
tabWidth:135,
enableTabScroll:true
});

Ext.onReady(function(){



var Tree = Ext.tree;

//定义根节点的Loader
var treeloader=new Tree.TreeLoader({dataUrl:'tree.jsp?DID=1'});

//异步加载根节点
var rootnode=new Tree.AsyncTreeNode({
id:'1',
text:'规划系统' ,
leaf:false //若为true 则树节点的图标将改变,显示默认的图片
});

var treepanel = new Tree.TreePanel({
//renderTo:"tree_div",//如果使用renderTo,则不能使用setRootNode()方法,需要在TreePanel中设置root属性。
//el:'tree_div', //填充区域
rootVisible:true, //隐藏根节点
useArrows:false, //文件夹前显示的图标改变了不在是+号了
border:true, //边框
animate:true, //动画效果
autoScroll:true, //自动滚动
enableDD:false, //拖拽节点
containerScroll:true,
loader:treeloader
});

//设置根节点
treepanel.setRootNode(rootnode);

//响应事件,传递node参数 也就是说每一次加载树之前都要通过'beforeload'事件加载子节点
treepanel.on('beforeload',
function(node,event){
//alert(node.id);
treepanel.loader.dataUrl='tree.jsp?DID='+node.id; //定义子节点的Loader
}
); 

//点击节点时的click事件,当点击子节点时,就会动态的打开一个tabpanel 这里使用了iframe 
treepanel.on('click',
function(node,event){
var n = tab.getComponent(node.id);
if (node.isLeaf()){
if (!n) {
var n = tab.add({
'id' : node.id,
'title' : node.text,
closable:true,   
html : '<iframe name="main'+node.id+'" scrolling="auto" frameborder="0"    width="100%"   height="100%" src="+node.url+"></iframe>'+node.text
});
}
tab.setActiveTab(n);
}

}

);



//layout

var viewport = new Ext.Viewport({
layout:'border',
items:[
new Ext.BoxComponent({
region:'north',
el: 'north',
height:80
}),new Ext.BoxComponent({
region:'south',
el: 'south',
height:25
}),

{
region:'west',
el:'west',
split:true,
width: 200,
minSize: 175,
maxSize: 400,
margins:'0 0 0 0',
layout:'accordion',
title:'系统菜单',
collapsible :false, //右上角收缩按钮
layoutConfig:{
animate:true
},
items: [
{
title:'EXT控件使用',
border:false,
html:'<div id="tree-div" style="overflow:auto;width:100%;height:100%"></div>'
//iconCls:'nav'
}]
},
tab //初始标签页
]
});


//end loding
setTimeout(
function() {
Ext.get('loading').remove();
Ext.get('loading-mask').fadeOut({remove:true});
}, 250
);
treepanel.render('tree-div');

rootnode.expand(false,false);
});
</script>
-----------------------------------------------------------------------------------------------------------
tree.jsp代码,此页面接收index.jsp的请求

<%@ page language="java" pageEncoding="UTF-8"%>

<jsp:useBean class="com.nbw.pub.JSONTree" id="JSONTree"></jsp:useBean>

<%
String uplevel = "";
if (request.getParameter("DID") != null) {
uplevel = request.getParameter("DID").toString();
}
JSONTree.setUplevel(uplevel);
%>

<%=JSONTree.getJSONString()%>
数据库表结构:

create table SYS_MODULE (
ID NUMBER(12) primary key, 、//ID主键                
      NAME VARCHAR2(50),                                           //节点名称
UPLEVEL VARCHAR2(5),                                 //父节点ID
SERIALNO NUMBER(2),                                     //子节点排序号
MEN1 VARCHAR2(40),                                       
MEN2 VARCHAR2(40),
TITLEPIC VARCHAR2(40),                                  //图片
      URL VARCHAR2(200),                              //URL 只有子节点才有URL
      LEAF NUMBER(1),                                         //是否是叶子节点
);

JsonTreeModule.java代码

package com.nbw.sysadmin.domain;

public class JsonTreeModule {

/*
* 此bean用于构建树向其中注入有关的信息,将SysModule中的信息拷贝到JsonTreeModule中
*/
private String id; //ID
private String text; //节点显示
private String cls; //图标
private boolean leaf; //是否叶子
private String href; //链接
private String hrefTarget; //链接指向
private boolean expandable; //是否展开
private String description; //描述信息


public JsonTreeModule() {
super();
}


public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getText() {
return text;
}

public void setText(String text) {
this.text = text;
}

public String getCls() {
return cls;
}

public void setCls(String cls) {
this.cls = cls;
}

public boolean isLeaf() {
return leaf;
}

public void setLeaf(boolean leaf) {
this.leaf = leaf;
}

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 isExpandable() {
return expandable;
}

public void setExpandable(boolean expandable) {
this.expandable = expandable;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}


}

JSONTree.java代码 最核心的

package com.nbw.pub;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import net.sf.json.JSONArray;

import org.hibernate.HibernateException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.nbw.common.HibernateSessionFactory;
import com.nbw.sysadmin.domain.JsonTreeModule;

public class JSONTree extends HibernateDaoSupport {

//通过set方法注入父节点值
private String uplevel;

/*
* 返回构建tree的JSON格式的字符串
*/
public String getJSONString() {
Connection conn = null;
Statement st = null;
ResultSet rs = null;
ArrayList<JsonTreeModule> TreeNodeArray = null;

String SQLString = "select * from sys_module s where s.uplevel='" + this.uplevel+"' order by s.uplevel";
try {
conn = HibernateSessionFactory.getSession().connection();
st = conn.createStatement();
rs = st.executeQuery(SQLString);
TreeNodeArray = new ArrayList<JsonTreeModule>();

while (rs.next()) {
JsonTreeModule TreeNode = new JsonTreeModule();

TreeNode.setId(rs.getString("moduleid"));
TreeNode.setText(rs.getString("modulename"));
TreeNode.setDescription(rs.getString("modulename"));
TreeNode.setHref(rs.getString("url"));
TreeNode.setHrefTarget("main"+rs.getString("moduleid"));

//父节点
if (Integer.parseInt(rs.getString("isleaf"))== 0)
{
TreeNode.setCls("folder");
TreeNode.setLeaf(false);
TreeNode.setExpandable(true);
} else // 子节点
{
TreeNode.setCls("file");
TreeNode.setLeaf(true);
TreeNode.setExpandable(false);
}
TreeNodeArray.add(TreeNode);
}

// 得到JSON数组
JSONArray JsonArray = JSONArray.fromObject(TreeNodeArray);
// 返回JSON字符串
return JsonArray.toString();
} catch (Exception e) {
System.out.println("getJSONString() of JSONTree.java throws : "+ e.toString());
return "";
} finally {
try {
HibernateSessionFactory.getSession().connection().close();
} catch (HibernateException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
}

public String getUplevel() {
return uplevel;
}

public void setUplevel(String uplevel) {
this.uplevel = uplevel;
}
}

你可能感兴趣的:(sql,jsp,Hibernate,json,ext)