Extjs的jsp代码:
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
ext 的js代码:
Ext.onReady(function(){
var treestore = Ext.create('Ext.data.TreeStore', {
fields: [
{name: 'id', type: 'string'},
{name: 'node', type: 'string'},//为节点名称
{name: 'content', type: 'string'},
{name: 'leaf', type: 'bool'} //这个一定要, 不然不能区分 是否是叶子
],
root:{
node:'广州',//根的名称
id:'-100',//根的id
expanded:false,
leaf: false
}
});
treepanel = Ext.create('Ext.tree.Panel',{
id: 'treepanel',
region: 'center',
flex:3,
renderTo:'tree',//对应jsp的
title: '文档结构',
store: treestore,
rootVisible: true,// 显示根节点,false 不显示
singleExpand: false,
frame: true,
tbar: ['-','-',
{
text:'全部折叠',
iconCls:'icon-collapse-all',
handler:function(){
treepanel.collapseAll();
}
}
],
listeners:{
'beforeitemexpand':function(node,optd)// 节点展开监听
{
var ids=node.get('id');//获取节点ID
treestore.setProxy({
type: 'ajax',
url:'getTreeData.action?ids='+ids,//传父ID到后台
reader:{
type: 'json'
// root:'children'//不写默认children
}
});
}
},
columns: [
{
xtype: 'treecolumn',//树列
dataIndex: 'node',//节点名字
text: '节点名字',
flex: 1//比例显示
},
{
text : '内容预览',
xtype : 'gridcolumn',//表列
dataIndex: 'content',//显示内容对应 store的fields里的content
flex: 1
}
]
});
});
建立一个树的实体:
package excelTree;
import java.util.List;
public class TreeModel {
public String id;
public String node;
public String content;
public boolean leaf;
public List
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getNode() {
return node;
}
public void setNode(String node) {
this.node = node;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public boolean isLeaf() {
return leaf;
}
public void setLeaf(boolean leaf) {
this.leaf = leaf;
}
public List
return children;
}
public void setChildren(List
this.children = children;
}
}
获取树的data的java方法
package excelTree;
import java.util.ArrayList;
import java.util.List;
import javax.naming.NamingException;
import com.opensymphony.xwork2.ActionSupport;
import common.tools;
import excel.Excel;
import excel.ExcelBeanRemote;
public class TreeData extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 1L;
final String pjName = new tools().moduleName();
public List
public String ids;//get和set得到 前台传来的 父ID
public List
return children;
}
public void setChildren(List
this.children = children;
}
public String getIds() {
return ids;
}
public void setIds(String ids) {
this.ids = ids;
}
public ExcelBeanRemote remote(){
ExcelBeanRemote remote = null;
try {
remote = (ExcelBeanRemote) new tools().getContext().lookup(pjName+"ExcelBean!excel.ExcelBeanRemote");
} catch (NamingException e) {
e.printStackTrace();
}
return remote;
}
public String GetTreenode()
{
children=new ArrayList
List
excel=remote().getExcelParts(Integer.parseInt(ids));//根据父ID到 数据库获取其 下的节点
for(int i=0;i { TreeModel ok=new TreeModel(); ok.setId(excel.get(i).getTestid().toString()); ok.setContent(excel.get(i).getNaturetext()); ok.setNode(excel.get(i).getNaturename()); ok.setLeaf(Boolean.parseBoolean(excel.get(i).getSheetname()));//如果 String 参数不是 null 且在忽略大小写时等于"true",则返回的 boolean 表示 true 值。 children.add(ok); } return SUCCESS;//返回数据children } } structs.xml配置
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd">