处理树最主要是如何获得树的子节点,我通过ajax调用action的方式获得了树的下级节点,返回json字符串.
以下是获得部门树的部分代码
ext js 创建树
var Tree = Ext.tree;
deptTree = new Tree.TreePanel({
el : elementId,
autoScroll:true,
animate:true,
enableDD:true,
containerScroll: true,
loader: new Tree.TreeLoader({
dataUrl: CONTEXT_PATH + "/depts/deptTree.do",
baseParams: filter
})
});
// set the root node
root = new Tree.AsyncTreeNode({
text: rootDesc,
draggable:false,
id:'-1'
});
deptTree.setRootNode(root);
// render the deptTree
deptTree.render();
root.expand();
ext js 重新加载树
/**
* 重新加载树
*
* @param {f}
* f 过滤树的条件 如:{placeid:'xxxxx'}
*/
reload : function (f){
filter = f;
var loader = deptTree.getLoader();
deptTree.on('beforeload', function(){
loader.dataUrl = loader.dataUrl ;
loader.baseParams = filter;
});
root.reload();
}
XWork.xml
<action name="deptTree" method="getSubNodes"
class="cn.org.coral.biz.depts.view.deptExtAction">
</action>
Action(Webwork)
/**
* ExtTree获得下级节点数据
* @return
*/
public String getSubNodes() throws Exception {
this.getHttpServletResponse().setCharacterEncoding("UTF-8");
PrintWriter out = this.getHttpServletResponse().getWriter();
String jsonString = service.getSubNodes(getFilter());
out.print(jsonString);
return null;
}
Service(通过Spring管理)
/**
* ExtTree获得下级节点数据
* @param filter
* @return
*/
public String getSubNodes(Map filter) {
JSONArray josnArray = new JSONArray();
List<ExtTreeNode> nodes = manager.getSubNodes(filter);
if(nodes !=null && !nodes.isEmpty()){
josnArray = JSONArray.fromObject(nodes);
}
return josnArray == null ? "":josnArray.toString();
}
自定义ExtTreeNode 树节点对象
package cn.org.coral.core.ext.objects;
import java.io.Serializable;
import org.directwebremoting.annotations.DataTransferObject;
/**
* ExtTreeNode对象
* @author Lib
*
*/
@DataTransferObject
public class ExtTreeNode implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
public ExtTreeNode(String id, String text, String cls, boolean leaf) {
super();
this.id = id;
this.text = text;
this.cls = cls;
this.leaf = leaf;
}
private String id;
private String text;
private String cls;
private boolean leaf;
/**
* @return the cls
*/
public String getCls() {
return cls;
}
/**
* @param cls the cls to set
*/
public void setCls(String cls) {
this.cls = cls;
}
/**
* 节点的id
* @return the id
*/
public String getId() {
return id;
}
/**
* 节点的id
* @param id the id to set
*/
public void setId(String id) {
this.id = id;
}
/**
* 是否是叶子节点
* @return the leaf
*/
public boolean getLeaf() {
return leaf;
}
/**
* 是否是叶子节点
* @param leaf the leaf to set
*/
public void setLeaf(boolean leaf) {
this.leaf = leaf;
}
/**
* 节点显示名
* @return the text
*/
public String getText() {
return text;
}
/**
* 节点显示名
* @param text the text to set
*/
public void setText(String text) {
this.text = text;
}
}