Ext tree 结合dwr 调用后台数据
关键字: ext dwr
还是废话少说
需要dwr loader
引入js 类
<script type="text/javascript"
src="${base}ext/adapter/dwr/DWRTreeLoader.js"></script>
调用例子
页面调用关键代码
loader: new Ext.ux.DWRTreeLoader({
dwrCall:filemanager.getMyFolderById
}),
服务端代码
Folder 简单代码
dwr 相关配置我就不配置了,懂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>
<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>
<script type="text/javascript"
src="${base}ext/adapter/dwr/DWRTreeLoader.js"></script>
- 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.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
- })
- ;
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
})
});
页面调用关键代码
loader: new Ext.ux.DWRTreeLoader({
dwrCall:filemanager.getMyFolderById
}),
服务端代码
- 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;
- }
- .........
- }
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;
- 。。。。。。。
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的都明白