Ext JS Kitchen Sink [Learning by doing](2)ArrayGrid

一、数据

Ext.define('Company', {
	 extend: 'Ext.data.Model',
	 fields: [
		 {name: 'company', type: 'string'},
		 {name: 'price', type: 'string'},
		 {name: 'change', type: 'string'},
		 {name: 'pctChange', type: 'string'},
		 {name: 'lastChange', type: 'string'}
	 ]
 });
 var Companies = Ext.create('Ext.data.Store', {
	 model: 'Company',
	 //报错,不能找到buffered属性;通过添加storeid解决
	 storeId:'Companies',
	 data:[
		 {company: '3m Co', price: '71.72', change: '0.02', 
			pctChange: '0.03%', lastChange: '09/01/2015' },
		 {company: 'Alcoa Inc', price: '29.01', change: '0.42', 
			pctChange: '1.47%', lastChange: '09/01/2015' },
		 {company: 'Altria Group Inc', price: '83.81', change: '0.28', 
			pctChange: '0.34%', lastChange: '09/01/2015' },
		 {company: 'American Express Company', price: '52.55', change: '0.01', 
			pctChange: '0.02%', lastChange: '09/01/2015' },
		 {company: 'American International Group, Inc', price: '64.13', change: '0.31', 
			pctChange: '0.49%', lastChange: '09/01/2015' },
		 {company: 'AT&T Inc.', price: '31.61', change: '-0.48', 
			pctChange: '-1.54%', lastChange: '09/01/2015' },
		 {company: 'Boeing Co.', price: '75.43', change: '0.53', 
			pctChange: '0.71%', lastChange: '09/01/2015' },
		 {company: 'Caterpillar Inc.', price: '67.27', change: '0.92', 
			pctChange: '1.39%', lastChange: '09/01/2015' }
	]
 });

二、ArrayGrid

Ext.onReady(function(){
	//初始化智能提示
	Ext.QuickTips.init();
	var cp = Ext.create('Ext.state.CookieProvider', {
		expires: new Date(new Date().getTime()+(1000*60*60*24*30))//30D
	});
	Ext.state.Manager.setProvider(cp);
	
	Ext.define('ArrayGrid', {
		extend: 'Ext.grid.Panel',
		store: 'Companies',
		/*
			保存当前状态,保证刷新或者跨页面加载后页面状态一致
			必须实例化Ext.state.Provider,
			选择CookieProvider或者LocalStorageProvider
		*/
		stateful: true,
		stateId: 'stateGrid',
		collapsible: true,
		height: 350,
		title: 'Array Grid',
		viewConfig: {
			stripeRows: true,
			enableTextSelection: true
		},

		initComponent: function () {
			this.width = 650;
			this.columns = [
				{
					text     : 'Company',
					flex     : 1,
					sortable : false,
					dataIndex: 'company'
				},
				{
					text     : 'Price',
					width    : 75,
					sortable : true,
					//改变渲染到单元格的值和样式
					renderer : 'usMoney',
					dataIndex: 'price'
				},
				{
					text     : 'Change',
					width    : 80,
					sortable : true,
					renderer : function(val) {
						if (val > 0) {
							return '<span style="color:' + '#73b51e' + ';">' + val + '</span>';
						} else if (val < 0) {
							return '<span style="color:' + '#cf4c35' + ';">' + val + '</span>';
						}
						return val;
					},
					dataIndex: 'change'
				},
				{
					text     : '% Change',
					width    : 100,
					sortable : true,
					renderer : function(val) {
						if (val > 0) {
							return '<span style="color:' + '#73b51e' + '">' + val + '%</span>';
						} else if (val < 0) {
							return '<span style="color:' + '#cf4c35' + ';">' + val + '%</span>';
						}
						return val;
					},
					dataIndex: 'pctChange'
				},
				{
					text     : 'Last Updated',
					width    : 115,
					sortable : true,
					renderer : Ext.util.Format.dateRenderer('m/d/Y'),
					dataIndex: 'lastChange'
				},
				{
					menuDisabled: true,
					sortable: false,
					xtype: 'actioncolumn',
					width: 50,
					items: [{
						//指定CSS图片
						iconCls: 'sell-col',
						//智能提示
						tooltip: 'Sell stock',
						//图标的点击事件处理函数
						handler: function(grid, rowIndex, colIndex) {
							var rec = grid.getStore().getAt(rowIndex);
							Ext.Msg.alert('Sell', 'Sell ' + rec.get('company'));
						}
					}, {
						/*
							获取需要应用到图标的CSS类名
							参数:详见Ext.grid.column.Action配置项items
						*/
						getClass: function(v, meta, rec) {
							if (rec.get('change') < 0) {
								return 'alert-col';
							} else {
								return 'buy-col';
							}
						},
						//返回tooltip
						getTip: function(v, meta, rec) {
							if (rec.get('change') < 0) {
								return 'Hold stock';
							} else {
								return 'Buy stock';
							}
						},
						//图标的点击事件处理函数
						handler: function(grid, rowIndex, colIndex) {
							var rec = grid.getStore().getAt(rowIndex),
								action = (rec.get('change') < 0 ? 'Hold' : 'Buy');

							Ext.Msg.alert(action, action + ' ' + rec.get('company'));
						}
					}]
				}
			];

			this.callParent();
		},
		renderTo: "ArrayGrid"
	});
	Ext.create('ArrayGrid',{});
});



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值