ExtJs 之动态列实现

创建DynamicGrid.js

Ext.grid.DynamicGrid = Ext.extend(Ext.grid.GridPanel, {
	initComponent: function() {
		//创建store
		var ds = new Ext.data.Store({
			url: this.storeUrl,
			reader: new Ext.data.JsonReader()
		});
		
		//设置默认配置
		var config = {
			viewConfig: {
				forceFit: true
			},
			enableColLock: false,
			loadMask: true,
			border: true,
			stripeRows: true,
			ds: ds,
			columns: []
		};
		
		//给分页PagingToolbar绑定store
		this.bbar.bindStore(ds, true);
		
		Ext.apply(this, config);
		Ext.apply(this.initialConfig, config);
		Ext.grid.DynamicGrid.superclass.initComponent.apply(this, arguments);
	},
	
	onRender: function(ct, position) {
		this.colModel.defaultSortable = true;
		Ext.grid.DynamicGrid.superclass.onRender.call(this, ct, position);

		this.el.mask('Loading...');
		this.store.on('load', function() {
			if (typeof(this.store.reader.jsonData.columns) === 'object') {
				var columns = [];
				
				if (this.rowNumberer) {
					columns.push(new Ext.grid.RowNumberer());
				}
				
				if (this.checkboxSelModel) {
					columns.push(new Ext.grid.CheckboxSelectionModel());
				}
				
				Ext.each(this.store.reader.jsonData.columns, 
					function(column) {
						columns.push(column);
					}
				);
					
				this.getColumnModel().setConfig(columns);
			}
				
			this.el.unmask();
		}, this);
		
		this.store.load();
	}
});

 

创建grid

var dynamicGrid = new Ext.grid.DynamicGrid({
		title: '测试动态列',
		renderTo: 'dynamic-grid',
		storeUrl: 'goods/dynamicGrid.do',
		width : 600,
		height: 200,
		rowNumberer: true,
		checkboxSelModel: true,
		sm: new Ext.grid.CheckboxSelectionModel(),
		bbar : new Ext.PagingToolbar({
			pageSize : 5,
			displayInfo : true,
			displayMsg : '显示第{0}到{1}条数据,共{2}条',
			emptyMsg : "没有数据",
			beforePageText : "第",
			afterPageText : '页 共{0}页'
		})
	});

 

返回的数据格式

{
	'metaData': {
		'totalProperty': 'total',
		'root': 'records',
		'id': 'id',
		'fields': [
			{'name': 'id', 'type': 'int'},
			{'name': 'name', 'type': 'string'}
		]
	},
	'success': true,
	'total': 50,
	'records': [
		{'id': '1', 'name': 'AAA'},
		{'id': '2', 'name': 'BBB'}
	],
	'columns': [
		{'header': '#', 'dataIndex': 'id'},
		{'header': 'User', 'dataIndex': 'name'}
	]
}
 

 

文章转载至http://erhanabay.com/2009/01/29/dynamic-grid-panel-for-ext-js/,自己另外增加了分页方式。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值