当前位置: 代码迷 >> 综合 >> EXTJS2--grid通过后台返回数据动态生成表头和列表
  详细解决方案

EXTJS2--grid通过后台返回数据动态生成表头和列表

热度:57   发布时间:2023-11-03 07:33:53.0

1.需要引入Ext.ux.data.PagingMemoryProxy.js

Ext.namespace("Ext.ux");
Ext.namespace("Ext.ux.data");/* Fix for Opera, which does not seem to include the map function on Array's */
if(!Array.prototype.map){Array.prototype.map = function(fun){var len = this.length;if(typeof fun != "function"){throw new TypeError();}var res = new Array(len);var thisp = arguments[1];for(var i = 0; i < len; i++){if(i in this){res[i] = fun.call(thisp, this[i], i, this);}}return res;};
}/* Paging Memory Proxy, allows to use paging grid with in memory dataset */
Ext.ux.data.PagingMemoryProxy = function(data, config) {Ext.ux.data.PagingMemoryProxy.superclass.constructor.call(this);this.data = data;Ext.apply(this, config);
};Ext.extend(Ext.ux.data.PagingMemoryProxy, Ext.data.MemoryProxy, {customFilter: null,load : function(params, reader, callback, scope, arg) {params = params || {};var result;try {result = reader.readRecords(this.data);} catch(e) {this.fireEvent("loadexception", this, arg, null, e);callback.call(scope, null, arg, false);return;}// filtering   if (this.customFilter!=null) {result.records = result.records.filter(this.customFilter);result.totalRecords = result.records.length;} else if (params.filter!==undefined) {result.records = result.records.filter(function(el){if (typeof(el)=="object"){var att = params.filterCol || 0;return String(el.data[att]).match(params.filter)?true:false;} else {return String(el).match(params.filter)?true:false;}});result.totalRecords = result.records.length;}// sorting   if (params.sort!==undefined) {// use integer as params.sort to specify column, since arrays are not named   // params.sort=0; would also match a array without columns   var dir = String(params.dir).toUpperCase() == "DESC" ? -1 : 1;var fn = function(r1, r2){return r1==r2 ? 0 : r1};var st = reader.recordType.getField(params.sort).sortType;result.records.sort(function(a, b) {var v = 0;if (typeof(a)=="object"){v = fn(st(a.data[params.sort]), st(b.data[params.sort])) * dir;} else {v = fn(a, b) * dir;}if (v==0) {v = (a.index < b.index ? -1 : 1);}return v;});}// paging (use undefined cause start can also be 0 (thus false))   if (params.start!==undefined && params.limit!==undefined) {result.records = result.records.slice(params.start, params.start+params.limit);}callback.call(scope, result, arg, true);}
});   

2.页面extjs代码

<% String id= request.getParameter("id")==null?"-1":request.getParameter("id");
%>	
<script type="text/javascript" src="<%=request.getContextPath()%>/ext/ux/Ext.ux.data.PagingMemoryProxy.js"></script>
<script type="text/javascript">
Ext.onReady(function(){Ext.QuickTips.init();Ext.page.pageSize = 25;var path = "<%=request.getContextPath() %>";var columnNames = [];var dataArray = [];Ext.Ajax.request({url:path+'/getResults.action',method: 'post',params: {id: <%=id%>},success: function (f, a) {var data = Ext.decode(f.responseText);columnNames = data.columnNames;var cmap = data.columnNamesMap;var column = [];for (var c in cmap) {if(cmap[c].columnName){column.push({header:cmap[c].columnName,dataIndex:cmap[c].columnName});}}var store = new Ext.data.Store({proxy: new Ext.ux.data.PagingMemoryProxy(data),reader : new Ext.data.JsonReader({root : 'EntityArray',totalProperty: 'TotalResults',},columnNames)});var pagebar = new Ext.PagingToolbar({store: store,pageSize: Ext.page.pageSize,displayInfo: true,displayMsg : '共有 {2} 条记录.当前显示 {0} - {1}条记录.'});var cm = new Ext.grid.ColumnModel(column);var center = new Ext.grid.GridPanel({region:'center',title:'检核问题明细列表',id:'listq_grid',store: store,cm: cm,bodyStyle:'width:100%',autoWidth:true,autoscroll: true,autoSizeColumns : true,loadMask : true,bbar:pagebar});var viewport = new Ext.Viewport({layout : 'border',border: false,items:[center]});dataArray = data.EntityArray;store.load({params : {start : 0,limit : 25} });pagebar.doLoad(pagebar.cursor);//这是刷新分页插件,重新加载,博主不加这句代码的时候,会出现列表只显示第一页的数据,没有正确分页},failure: function (f, a) {Ext.MessageBox.alert('错误', "查询失败!")}});
})
</script>

3.后台返回json数据格式

{"columnNames":["INSTANCE_ID","INSTANCE_NAME","INSTANCE_CODE"],"columnNamesMap":[{"columnName":"INSTANCE_ID"},{"columnName":"INSTANCE_NAME"},{"columnName":"INSTANCE_CODE"}],"EntityArray":[{"INSTANCE_ID":"j4cbbbf443ac4b6cb0e99753c5d250d2","INSTANCE_NAME":"审计码","INSTANCE_CODE":"DAC"},{"INSTANCE_ID":"ofe3ebacc10f490788d37c848b8a6f56","INSTANCE_NAME":"数据校验码","INSTANCE_CODE":"DDVC"},{"INSTANCE_ID":"u690c38bce8f4125af7da3293ea7d3d3","INSTANCE_NAME":"记录更新系统","INSTANCE_CODE":"DK_SYSTEM_OF_UPD"},{"INSTANCE_ID":"tee1fa75b25c418b8e91253395021df9","INSTANCE_NAME":"插入时间戳","INSTANCE_CODE":"INSERTTIME"},{"INSTANCE_ID":"j2a902cd32e04a5cbcd9f9fd4e24f9e6","INSTANCE_NAME":"更新时间戳","INSTANCE_CODE":"UPDATETIME"}]
}
  相关解决方案