您的位置:首页 > Web前端 > JavaScript

Ext.js5的数组表格(3)

2015-12-17 18:34 731 查看
1、数组表格,这是我第一次接触到使用Store和Model(理论的概念介绍请看层级结构文章)

view

Ext.define('KitchenSink.view.grid.ArrayGrid', {
extend: 'Ext.grid.Panel',//当我要做表格的时候我可以继承这个类(类什么的我还不太清楚20151217)
requires: [
'Ext.grid.column.Action'
],
//我需要这个东西,至于为什么是需要Ext.grid.column.Action我还不清楚20151217
xtype: 'array-grid',
store: 'Companies',//仓库出现了!
stateful: true,
//默认值是false,此处使用true是因为The grid is stateful so you
//can move or hide columns, reload the page, and come back to the grid in the same state
//you left it in.总是就是无论你改变表格的状态之后,他会保存这个改变的状态
//(不过例子并没有啊,骗纸┑( ̄Д  ̄)┍,可能是我还没有领会到他的点20151217)
collapsible: true,
multiSelect: true,
stateId: 'stateGrid',// 唯一标识符,用于表格的state。非全局唯一标识符。列状态不是单独存在的,它依赖于所在表格的状态。
height: 350,
title: 'Array Grid',
viewConfig: {
enableTextSelection: true//The cells are selectable due to use of the `enableTextSelection` option.
},

initComponent: function () {
var me = this;
//推荐用这样的方式来写this,为什么的话来源于上将军的一篇博客,地址暂时找不到了,20151217,强烈推荐

me.width = 750;
me.columns = [
{
text     : 'Company',
flex     : 1,
sortable : false,
dataIndex: 'name'//表格的Ext.data.Store的Ext.data.Model的field,用于获取需要显示到单元格的值。 必须属性。默认值为null。
},
{
text     : 'Price',
width    : 95,
sortable : true,
formatter: 'usMoney',//转换公式,要做数据处理
dataIndex: 'price'
},
{
text     : 'Change',
width    : 80,
sortable : true,
renderer : function(val) {
var out = Ext.util.Format.number(val, '0.00');
if (val > 0) {
return '<span style="color:' + "#73b51e" + ';">' + out + '</span>';
} else if (val < 0) {
return '<span style="color:' + "#cf4c35" + ';">' + out + '</span>';
}
return out;
},//renderer 渲染器,还不太懂20151217。这里是渲染输出值的样式。
dataIndex: 'change'
},
{
text     : '% Change',
width    : 100,
sortable : true,
renderer : function(val) {
var out = Ext.util.Format.number(val, '0.00%');
if (val > 0) {
return '<span style="color:' + "#73b51e" + ';">' + out + '</span>';
} else if (val < 0) {
return '<span style="color:' + "#cf4c35" + ';">' + out + '</span>';
}
return out;
},
dataIndex: 'pctChange'
},
{
text     : 'Last Updated',
width    : 115,
sortable : true,
formatter: 'date("m/d/Y")',
dataIndex: 'lastChange'
},
{
menuDisabled: true,//隐藏列是否在menu中出现,详细:http://blog.csdn.net/dracotianlong/article/details/8545487
sortable: false,
xtype: 'actioncolumn',
width: 50,
items: [{
iconCls: 'sell-col',
tooltip: 'Sell stock',
//getStore()得到仓库,getAt( Number index )的参数是index,返回index处的记录Record,如果没有找到返回undefined

handler: function(grid, rowIndex, colIndex) {
var rec = grid.getStore().getAt(rowIndex);
Ext.Msg.alert('Sell', 'Sell ' + rec.get('name'));
}

}, {
//getClass获取所提供的对象类,此处的change是变化(详见model)
getClass: function(v, meta, rec) {
if (rec.get('change') < 0) {
return 'alert-col';
} else {
return 'buy-col';
}
},
//getTip提示标签
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('name'));
}
}]
}
];

me.callParent();
}
});


store

Ext.define('KitchenSink.store.Companies', {
extend: 'Ext.data.ArrayStore',
alias: 'store.companies',//仓库的别名是store.xxx
model: 'KitchenSink.model.Company',//模型出现了!
data: [
[0,  '3m Co',                                       71.72, 0.02,  0.03,  '9/1 12:00am', 'Manufacturing'],
[1,  'Alcoa Inc',                                   29.01, 0.42,  1.47,  '9/1 12:00am', 'Manufacturing'],
[2,  'Altria Group Inc',                            83.81, 0.28,  0.34,  '9/1 12:00am', 'Manufacturing'],
[3,  'American Express Company',                    52.55, 0.01,  0.02,  '9/1 12:00am', 'Finance'],
[4,  'American International Group, Inc.',          64.13, 0.31,  0.49,  '9/1 12:00am', 'Services'],
[5,  'AT&T Inc.',                                   31.61, -0.48, -1.54, '9/1 12:00am', 'Services'],
[6,  'Boeing Co.',                                  75.43, 0.53,  0.71,  '9/1 12:00am', 'Manufacturing'],
[7,  'Caterpillar Inc.',                            67.27, 0.92,  1.39,  '9/1 12:00am', 'Services'],
[8,  'Citigroup, Inc.',                             49.37, 0.02,  0.04,  '9/1 12:00am', 'Finance'],
[9,  'E.I. du Pont de Nemours and Company',         40.48, 0.51,  1.28,  '9/1 12:00am', 'Manufacturing'],
[10, 'Exxon Mobil Corp',                            68.1,  -0.43, -0.64, '9/1 12:00am', 'Manufacturing'],
[11, 'General Electric Company',                    34.14, -0.08, -0.23, '9/1 12:00am', 'Manufacturing'],
[12, 'General Motors Corporation',                  30.27, 1.09,  3.74,  '9/1 12:00am', 'Automotive'],
[13, 'Hewlett-Packard Co.',                         36.53, -0.03, -0.08, '9/1 12:00am', 'Computer'],
[14, 'Honeywell Intl Inc',                          38.77, 0.05,  0.13,  '9/1 12:00am', 'Manufacturing'],
[15, 'Intel Corporation',                           19.88, 0.31,  1.58,  '9/1 12:00am', 'Computer'],
[16, 'International Business Machines',             81.41, 0.44,  0.54,  '9/1 12:00am', 'Computer'],
[17, 'Johnson & Johnson',                           64.72, 0.06,  0.09,  '9/1 12:00am', 'Medical'],
[18, 'JP Morgan & Chase & Co',                      45.73, 0.07,  0.15,  '9/1 12:00am', 'Finance'],
[19, 'McDonald\'s Corporation',                     36.76, 0.86,  2.40,  '9/1 12:00am', 'Food'],
[20, 'Merck & Co., Inc.',                           40.96, 0.41,  1.01,  '9/1 12:00am', 'Medical'],
[21, 'Microsoft Corporation',                       25.84, 0.14,  0.54,  '9/1 12:00am', 'Computer'],
[22, 'Pfizer Inc',                                  27.96, 0.4,   1.45,  '9/1 12:00am', 'Medical'],
[23, 'The Coca-Cola Company',                       45.07, 0.26,  0.58,  '9/1 12:00am', 'Food'],
[24, 'The Home Depot, Inc.',                        34.64, 0.35,  1.02,  '9/1 12:00am', 'Retail'],
[25, 'The Procter & Gamble Company',                61.91, 0.01,  0.02,  '9/1 12:00am', 'Manufacturing'],
[26, 'United Technologies Corporation',             63.26, 0.55,  0.88,  '9/1 12:00am', 'Computer'],
[27, 'Verizon Communications',                      35.57, 0.39,  1.11,  '9/1 12:00am', 'Services'],
[28, 'Wal-Mart Stores, Inc.',                       45.45, 0.73,  1.63,  '9/1 12:00am', 'Retail'],
[29, 'Walt Disney Company (The) (Holding Company)', 29.89, 0.24,  0.81,  '9/1 12:00am', 'Services']
]
}, function(cls) {
var data = cls.prototype.config.data;

for(var i = 0; i < data.length; i++){
data[i].push('Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Sed metus nibh, sodales a, porta at, vulputate eget, dui. Pellentesque ut nisl. ');
}
});


model

Ext.define('KitchenSink.model.Company', {
extend: 'KitchenSink.model.Base',
fields: [
{name: 'name'},//Model中字段的映射名称. 此名称被用来作引用,类似于dataIndex
{name: 'price', type: 'float'},
{name: 'change', type: 'float'},//变化
{name: 'pctChange', type: 'float'},
{name: 'lastChange', type: 'date',  dateFormat: 'n/j h:ia'},
{name: 'industry'},//隐藏列
{name: 'desc'},

{
//一个函数, 用来将Reader读取的值转换成存储到Model中的对象。如果此参数为'null', 则读取此Field时不会对原始数据进行转换. 这样会提高性能. 但是你必须确保数据是正确的类型并且不需要进行转换。
//参数:v Reader提供的数据值, 如果为undefined则使用defaultValue的参数值
//参数:rec Reader到目前为止读取的Model的数据对象. 注意此时的Model字段值可能尚未被全部填入, 因为字段的读取顺序是根据你在 fields字段数组中的定义顺序。
name: 'trend',
convert: function(value, record) {
// Record creation call with no trend there: start with current price
if (value === null) {
return [record.get('price')];
}
return Ext.isArray(value) ? value : [ value ];
}
},
// Rating dependent upon performance 0 = best, 2 = worst
{
name: 'rating',
type: 'int',
convert: function(value, record) {
var pct = record.get('pctChange');
if (pct < 0)
return 2;
if (pct < 1)
return 1;
return 0;
}
}
],

// Override to keep the last 10 prices in the trend field
//set 引用类设置名称 参数:name value 返回值:this
set: function(fieldName, value) {
if (fieldName === 'price') {
this.callParent([{
price: value,
trend: this.addToTrend(fieldName.price)
}]);
}
else {
if (typeof fieldName !== 'string' && 'price' in fieldName) {
fieldName.trend = this.addToTrend(fieldName.price);
}
this.callParent(arguments);
}
},

// Override to keep the last 10 prices in the trend field
addToTrend: function(value) {
var trend = this.data.trend.concat(value);

if (trend.length > 10) {
Ext.Array.splice(trend, 0, trend.length - 10);
}
return trend;
}
});


感慨:男同事提到的生活现实让我觉得好绝望,我很想逃避。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: