Ext tree 结合dwr 调用后台数据

还是废话少说
需要dwr loader
引入js 类

<script type="text/javascript"
src="${base}ext/adapter/ext/ext-base.js"></script>
<script type="text/javascript" src="${base}ext/ext-all.js"></script>
<script type="text/javascript"
src="${base}ext/adapter/dwr/DWRTreeLoader.js"></script>


[size=large][color=red]<script type="text/javascript"
src="${base}ext/adapter/dwr/DWRTreeLoader.js"></script>[/color][/size]



Ext.namespace("Ext.ux");
/**
* @class Ext.ux.DWRTreeLoader
* @extends Ext.tree.TreeLoader
* @author Carina Stumpf
*
* DWRTreeloader loads tree nodes by calling a DWR service.
* Version 2.1
*
*/

/**
* @constructor
* @param cfg {Object} config A config object
* @cfg dwrCall the DWR function to call when loading the nodes
*/

Ext.ux.DWRTreeLoader = function(config) {
Ext.ux.DWRTreeLoader.superclass.constructor.call(this, config);
};

Ext.extend(Ext.ux.DWRTreeLoader, Ext.tree.TreeLoader, {
/**
* Load an {@link Ext.tree.TreeNode} from the DWR service.
* This function is called automatically when a node is expanded, but may be used to reload
* a node (or append new children if the {@link #clearOnLoad} option is false.)
* @param {Object} node node for which child elements should be retrieved
* @param {Function} callback function that should be called before executing the DWR call
*/
load : function(node, callback) {
var cs, i;
if (this.clearOnLoad) {
while (node.firstChild) {
node.removeChild(node.firstChild);
}
}
if (node.attributes.children && node.attributes.hasChildren) { // preloaded json children
cs = node.attributes.children;
for (i = 0,len = cs.length; i<len; i++) {
node.appendChild(this.createNode(cs[i]));
}
if (typeof callback == "function") {
callback();
}
} else if (this.dwrCall) {
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) {
var callParams;
var success, error, rootId, dataContainsRoot;

if (this.fireEvent("beforeload", this, node, callback) !== false) {

callParams = this.getParams(node);

success = this.handleResponse.createDelegate(this, [node, callback], 1);
error = this.handleFailure.createDelegate(this, [node, callback], 1);

callParams.push({callback:success, errorHandler:error});

this.transId = true;
this.dwrCall.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];
},

/**
* Handles a successful response.
* @param {Object} childrenData data that was sent back by the server that contains the child nodes
* @param {Object} parent parent node to which the child nodes will be appended
* @param {Function} callback callback that will be performed after appending the nodes
*/
handleResponse : function(childrenData, parent, callback) {
this.transId = false;
this.processResponse(childrenData, parent, callback);
},

/**
* Handles loading error
* @param {Object} response data that was sent back by the server that contains the child nodes
* @param {Object} parent parent node to which child nodes will be appended
* @param {Function} callback callback that will be performed after appending the nodes
*/
handleFailure : function(response, parent, callback) {
this.transId = false;
this.fireEvent("loadexception", this, parent, response);
if (typeof callback == "function") {
callback(this, parent);
}
console.log(e)("DwrTreeLoader: error during tree loading. Received response: " + response);
},

/**
* Process the response that was received from server
* @param {Object} childrenData data that was sent back by the server that contains the attributes for the child nodes to be created
* @param {Object} parent parent node to which child nodes will be appended
* @param {Function} callback callback that will be performed after appending the nodes
*/
processResponse : function(childrenData, parent, callback) {
var i, n, nodeData;
try {
for (var i = 0; i<childrenData.length; i++) {
var n = this.createNode(childrenData[i]);
if (n) {
n.hasChildren = childrenData[i].hasChildren;
parent.appendChild(n);
}
}

if (typeof callback == "function") {
callback(this, parent);
}
} catch(e) {
this.handleFailure(childrenData);
}
}
});


调用例子


Ext.onReady(function() {
tree = new Ext.tree.TreePanel({
el:'left-div',
autoHeight:false,
rootVisible:true,
animate:false,
autoScroll:true,
selModel: new Ext.tree.MultiSelectionModel(),
title: '${login_user.localName}的文件',
loader: new Ext.ux.DWRTreeLoader({
dwrCall:filemanager.getMyFolderById
}),

root: new Ext.tree.AsyncTreeNode({
text: '我的上传目录',
hasChildren:true,
id: 'myroot' //you could use a constant here or whatever fits best for your application
})

});


页面调用关键代码

[color=red][size=x-large]loader: new Ext.ux.DWRTreeLoader({
dwrCall:filemanager.getMyFolderById
}),[/size][/color]

服务端代码

public class FileManager extends EntityManager {
........
public List<Folder> getMyFolderById(String parentid) {
WebContext ctx = WebContextFactory.get();
User owner = (User) ctx.getSession().getAttribute(
AppContextVar.LOGIN_USER);
if(owner==null)return null;
List<Folder> folders = null;
DetachedCriteria criteria = DetachedCriteria.forClass(Folder.class);
criteria.add(Restrictions.eq("owner", owner.getName()));
criteria.add(Restrictions.eq("parent", parentid));
folders = hibernate.findByCriteria(criteria);
return folders;
}
.........
}


Folder 简单代码

public class Folder implements Serializable {

private String id;
private String text;
private boolean leaf;
private Timestamp createTime;
private String parent;
private String owner;
。。。。。。。

dwr 相关配置我就不配置了,懂dwr的都明白
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值