前台JS
Ext.onReady(function(){
var treeLoader = new Ext.tree.TreeLoader({
dataUrl:'../servlet/basic?action=treePanelTest',
listeners:{
beforeload:function(treeLoader,node){
alert(node.attributes.id);
treeLoader.baseParams.parentID = node.attributes.id;
//将AsyncTreeNode中的id传给变量parentID,然后交个Servlet处理
}
}
});
var form = new Ext.tree.TreePanel({
renderTo:'hello',
root: new Ext.tree.AsyncTreeNode({id:'-1',text : '根节点'}),
rootVisible:true,//判断根节点是否显示
loader:treeLoader
});
});
后台Servlet:
JSONArray array = new JSONArray();
JSONObject json;
if ("-1".equals(req.getParameter("parentID"))) {
List<Company> list = CompanyService.getAllCompanyList();
for (Company company : list) {
json = new JSONObject();
json.accumulate("id", company.getId());
json.accumulate("text", company.getName());
json.accumulate("leaf", false);
array.add(json);
}
} else {
long parentID = Long.parseLong(req.getParameter("parentID"));
Company company = CompanyService.findCompanyByID(parentID);
for (Company dept : company.getDepartments()) {
json = new JSONObject();
json.accumulate("id", dept.getId());
json.accumulate("text", dept.getName());
json.accumulate("leaf", false);
array.add(json);
}
for (User user : company.getStaff()) {
json = new JSONObject();
json.accumulate("id", user.getId());
json.accumulate("text", user.getUserName());
json.accumulate("leaf", true);
array.add(json);
}
}
String retJSON = array.toString();
Ext.onReady(function(){
var treeLoader = new Ext.tree.TreeLoader({
dataUrl:'../servlet/basic?action=treePanelTest',
listeners:{
beforeload:function(treeLoader,node){
alert(node.attributes.id);
treeLoader.baseParams.parentID = node.attributes.id;
//将AsyncTreeNode中的id传给变量parentID,然后交个Servlet处理
}
}
});
var form = new Ext.tree.TreePanel({
renderTo:'hello',
root: new Ext.tree.AsyncTreeNode({id:'-1',text : '根节点'}),
rootVisible:true,//判断根节点是否显示
loader:treeLoader
});
});
后台Servlet:
JSONArray array = new JSONArray();
JSONObject json;
if ("-1".equals(req.getParameter("parentID"))) {
List<Company> list = CompanyService.getAllCompanyList();
for (Company company : list) {
json = new JSONObject();
json.accumulate("id", company.getId());
json.accumulate("text", company.getName());
json.accumulate("leaf", false);
array.add(json);
}
} else {
long parentID = Long.parseLong(req.getParameter("parentID"));
Company company = CompanyService.findCompanyByID(parentID);
for (Company dept : company.getDepartments()) {
json = new JSONObject();
json.accumulate("id", dept.getId());
json.accumulate("text", dept.getName());
json.accumulate("leaf", false);
array.add(json);
}
for (User user : company.getStaff()) {
json = new JSONObject();
json.accumulate("id", user.getId());
json.accumulate("text", user.getUserName());
json.accumulate("leaf", true);
array.add(json);
}
}
String retJSON = array.toString();
本文介绍了一个使用ExtJS库实现的树状面板示例。该示例通过前台JavaScript代码与后台Servlet交互来动态加载树结构数据。前台代码使用TreeLoader组件加载数据,并在页面上渲染一个树形面板;后台Servlet负责接收请求参数并返回相应的树节点数据。
106

被折叠的 条评论
为什么被折叠?



