因项目中用到下拉框内容是季度标识,如2012003,201206,201209,201212
要求下拉框根据当天日期算出前面10年内的季度标识
Ext.define('DateModel', {
extend: 'Ext.data.Model',
fields: [
{ name: 'name', type: 'string' },
{ name: 'id', type: 'string' }
]
});
var comboStoreEndDate = Ext.create('Ext.data.Store', {
model: 'DateModel'
});
//季度标识:201203,201206,201209,201212
var dtCurrent = new Date();
dtCurrent = Ext.Date.add(dtCurrent, Ext.Date.MONTH, 3);
var dateY = Ext.Date.format(dtCurrent, 'Y');
var dateM = Ext.Date.format(dtCurrent, 'm');
var QuarterStartDate = '';
var QuarterEndDate = '';
//根据当前日期算出所在季度,若月份小于3则选上一季度12
if (dateM < 3) {
dtCurrent = Ext.Date.add(dtCurrent, Ext.Date.YEAR, -1);
dateY = Ext.Date.format(dtCurrent, 'Y');
dateM = '12';
}
else if (dateM >= 3 && dateM < 6) {
dateM = '03';
}
else if (dateM >= 6 && dateM < 9) {
dateM = '06';
}
else if (dateM >= 9 && dateM < 12) {
dateM = '09';
}
else if (dateM == 12) {
dateM = '12';
}
//上个季度
var dtPrevious;
var strPreviousDate = '';
var strCurrentDate = '';
strCurrentDate = dateY + '' + dateM;
//生成数据实体
var recordDate = Ext.ModelManager.create({
name: strCurrentDate,
id: strCurrentDate
}, 'DateModel');
//将实体插入store中
comboStoreEndDate.insert(0, recordDate);
//季度标识,算出前10年的
dtCurrent = new Date(dateM + '/15/' + dateY + '');
for (var i = 1; i <= 40; i++) {
dtPrevious = Ext.Date.add(dtCurrent, Ext.Date.MONTH, -(3 * i));
strPreviousDate = Ext.Date.format(dtPrevious, 'Ym');
//生成数据实体
recordDate = Ext.ModelManager.create({
name: strPreviousDate,
id: strPreviousDate
}, 'DateModel');
//将实体插入store中
comboStoreEndDate.insert(0, recordDate);
}
{
xtype: 'combobox',
width: 357,
store: comboStoreEndDate,
queryMode: 'local',
displayField: 'name', valueField: 'id',
fieldLabel: '季度',
listeners: {
afterrender: function (onme, options) {
//alert(QuarterEndDate);
// var endIndex = comboStoreEndDate.find('name', QuarterEndDate);
onme.setValue(strCurrentDate);
}
}
}