1、 在jsp页面中引入DwrTreeLoader.js文件,如下:
<script type="text/javascript" src="<%=request.getContextPath()%>/baj/util/DwrTreeLoader.js"></script> |
2、 前台lbo中的树加载器treeload使用DWRTreeLoader加载树节点,如下:
var dataIn = [{compName:"uiAllMgr", boName:"bo_uiAll_dwrTree", funcName:"uiAll_dwrTree_getTree"}]; //tree dataIn。; var treeload = new Ext.tree.DWRTreeLoader({ dwrMethod: mqMgr.callObjectMethod, params: dataIn }); |
说明:
dwrMethod:dwr调用的方法,为mqMgr.callObjectMethod。
params:输入参数(数组类型),为dataIn。
3、 树的定义
var treeRoot = new Ext.tree.AsyncTreeNode({ text: 'Invisible root', hasChildren:true, id: "0" });
var tree = new Ext.tree.TreePanel({ width:552, autoHeight:true, rootVisible:false, autoScroll:true, loader: treeload, //使用第2步中创建树的加载器 root: treeRoot }); |
4、 后台bo中方法uiAll_dwrTree_getTree如下:
public Map<String,Object> uiAll_dwrTree_getTree(Map<String,Object> dataIn){ Map<String, Object> dataOut = new HashMap<String, Object>(); String nodeId = (String)dataIn.get("nodeId"); String comboxId = (String)dataIn.get("comboxId"); System.out.println("当前的节点为:"+nodeId); dataOut.put("resultCode", -10); //没有根 List nodeList = new ArrayList();
try{ if("0".equals(nodeId)){ //load出根节点 List<Ent_uiAll_dwrTree> treeList = dao_uiAll_dwrTree.load(" pid = '' or pid is null"); if(treeList != null && treeList.size()>0){ Map map = new HashMap(); map.put("id", treeList.get(0).id); map.put("text", treeList.get(0).name); nodeList.add(map); } }else{ List<Ent_uiAll_dwrTree> treeList = dao_uiAll_dwrTree.load(" pid = '"+nodeId+"'"); if(treeList != null && treeList.size()>0){ for(int i=0; i<treeList.size(); i++){ Map map = new HashMap(); map.put("id", treeList.get(i).id); map.put("text", treeList.get(i).name); nodeList.add(map); } } }
dataOut.put("getNodes", nodeList); dataOut.put("resultCode", 1); dataOut.put("resultMsg", "成功"); }catch(Exception e){ dataOut.put("resultCode", -10000); dataOut.put("resultMsg", "树加载失败,请联系管理员"); log.error("树加载失败, 请联系管理员"); e.printStackTrace(); }
return dataOut; } |
说明:
dataOut中getNodes、resultCode、resultMsg为返回的参数值(固定不变)。
getNodes:为List类型。
5、附:这里DWRTreeLoader跟网上下载的做了一些修改,代码如下:
/**
* @constructor
* @param {Object}
* config A config object
* @cfg dwrCall the DWR function to call when loading the nodes
*/
Ext.tree.DWRTreeLoader = function(config) {
Ext.tree.DWRTreeLoader.superclass.constructor.call(this, config);
if(config && config.dwrMethod){
this.dwrMethod = config.dwrMethod;
}
if(config && config.params){
this.params = config.params;
}
};
Ext.extend(Ext.tree.DWRTreeLoader, Ext.tree.TreeLoader, {
load : function(node, callback){
if(this.clearOnLoad){
while(node.firstChild){
node.removeChild(node.firstChild);
}
}
if(node.attributes.children){ // preloaded json children
var cs = node.attributes.children;
for(var i = 0, len = cs.length; i < len; i++){
node.appendChild(this.createNode(cs[i]));
}
if(typeof callback == "function"){
callback();
}
}else if(this.dwrMethod){
this.requestData(node, callback);
}
},
/**
* Performs the actual load request
*
* @param {Object}
* node node for which child elements should be retrieved
* @param {Function}
* callback function that should be called before executing the
* DWR call
*/
requestData : function(node, callback) {
if (this.fireEvent("beforeload", this, node, callback) !== false) {
var callParams = [];
var success = this.handleResponse.createDelegate(this, [node, callback], 1);
var error = this.handleFailure.createDelegate(this, [node, callback], 1);
var dataInParams = this.params;
var nodeparams = this.getParams(node);
dataInParams[0].nodeId = nodeparams[0];
if(dataInParams instanceof Array){
callParams = callParams.concat(dataInParams);
}else{
alert("传入参数必需为数组,不应为json格式");
}
if(dataInParams == ""){ //占用数组的第一个作为map的传入参数
callParams = callParams.concat([{}]);
}
//将调用DWR方法的元数据对象加入参数数组中
callParams.push({callback:success, errorHandler:error});
this.transId=true;
this.dwrMethod.apply(this, callParams);
} else {
// if the load is cancelled, make sure we notify
// the node that we are done
if (typeof callback == "function") {
callback();
}
}
},
/**
* Override this to add custom request parameters. Default adds the node id as
* first and only parameter
*/
getParams : function(node) {
return [node.id];
},
/**
* Process the response that server sent back via DWR.
*
* @param {Object}
* response data that was sent back by the server that contains the
* child nodes
* @param {Object}
* node parent node to which child nodes will be appended
* @param {Function}
* callback callback that will be performed after appending the nodes
*/
processResponse : function(response, node, callback){
try {
response = response.getNodes;
for(var i = 0; i < response.length; i++){
var n = this.createNode(response[i]);
if(n){
node.appendChild(n);
}
}
if(typeof callback == "function"){
callback(this, node);
}
}catch(e){
this.handleFailure(response);
}
},
/**
* Handles a sucessful response.
*
* @param {Object}
* response data that was sent back by the server that contains the
* child nodes
* @param {Object}
* node parent node to which child nodes will be appended
* @param {Function}
* callback callback that will be performed after appending the nodes
*/
handleResponse : function(response, node, callback){
this.transId = false;
this.processResponse(response, node, callback);
this.fireEvent("load", this, node, response);
},
/**
* Handles load error
*
* @param {Object}
* response data that was sent back by the server that contains the
* child nodes
* @param {Object}
* node parent node to which child nodes will be appended
* @param {Function}
* callback callback that will be performed after appending the nodes
*/
handleFailure : function(response, node, callback){
this.transId = false;
this.fireEvent("loadexception", this, node, response);
if(typeof callback == "function"){
callback(this, node);
}
}
});