PDA

View Full Version : [FIXED] Date Picker doesn't allow reverse ordering years



sencha-noob
9 May 2012, 6:06 AM
REQUIRED INFORMATION
Ext version tested:

Sencha Touch 2.0.1 final
Browser versions tested against:

Chrome 20.0.1123.4 dev
DOCTYPE tested against:

html
Description:Steps to reproduce the problem:
Create a date on a form with a date picker
Set the date picker yearFrom to be 2012
Set the date picker yearTo to be 1980
The date picker incorrectly goes from 1980 to 2012
The result that was expected:

I expected that the year would order from the yearFrom to the yearTo values.
The result that occurs instead:

Instead the date picker check & swaps the two if it's not the order it expects. It will only sort from the lesser number to the greater number, regardless of what the yearFrom & yearTo is.
Code Snippet override fixes the problem

Don't restrict the developer - if they want descending ordered years, let 'em do it. This override appears to fix the problem.


Ext.define('common.override.DatePicker', {
override: 'Ext.picker.Date',


/**
* Generates all slots for all years specified by this component, and then sets them on the component
* @private
*/
createSlots: function() {
var me = this,
slotOrder = this.getSlotOrder(),
yearsFrom = me.getYearFrom(),
yearsTo = me.getYearTo(),
years = [],
days = [],
months = [],
reverse = yearsFrom > yearsTo,
ln, tmp, i,
daysInMonth;


// Smart for loop; sorts asc or desc depending whether years reversed
for (i = yearsFrom; ((reverse && i >= yearsTo) || (!reverse && i <= yearsTo)); (reverse && i-- || i++)) {
years.push({
text: i,
value: i
});
}


daysInMonth = this.getDaysInMonth(1, new Date().getFullYear());


for (i = 0; i < daysInMonth; i++) {
days.push({
text: i + 1,
value: i + 1
});
}


for (i = 0, ln = Ext.Date.monthNames.length; i < ln; i++) {
months.push({
text: Ext.Date.monthNames[i],
value: i + 1
});
}


var slots = [];


slotOrder.forEach(function(item) {
slots.push(this.createSlot(item, days, months, years));
}, this);


me.setSlots(slots);
}
});

mitchellsimoens
9 May 2012, 6:25 AM
This is really a feature request than a bug. Thanks for the report.

sencha-noob
9 May 2012, 8:58 AM
Agreed. Thanks.

sencha-noob
10 May 2012, 5:03 AM
Or more simply:



createSlots: function() {
var me = this,
slotOrder = this.getSlotOrder(),
yearsFrom = me.getYearFrom(),
yearsTo = me.getYearTo(),
years = [],
days = [],
months = [],
reverse = yearsFrom > yearsTo,
ln, tmp, i,
daysInMonth;


if(reverse) {
for (i = yearsFrom; i >= yearsTo; i--) {
years.push({
text: i,
value: i
});
}
} else {
for (i = yearsFrom; i <= yearsTo; i++) {
years.push({
text: i,
value: i
});
}
}


daysInMonth = this.getDaysInMonth(1, new Date().getFullYear());


for (i = 0; i < daysInMonth; i++) {
days.push({
text: i + 1,
value: i + 1
});
}


for (i = 0, ln = Ext.Date.monthNames.length; i < ln; i++) {
months.push({
text: Ext.Date.monthNames[i],
value: i + 1
});
}


var slots = [];


slotOrder.forEach(function(item) {
slots.push(this.createSlot(item, days, months, years));
}, this);


me.setSlots(slots);
}

mitchellsimoens
28 May 2012, 10:04 AM
I went a little different route in my fix:


createSlots: function() {
var me = this,
slotOrder = me.getSlotOrder(),
yearsFrom = me.getYearFrom(),
yearsTo = me.getYearTo(),
years = [],
days = [],
months = [],
reverse = yearsFrom > yearsTo,
ln, i, daysInMonth;

while (yearsFrom) {
years.push({
text : yearsFrom,
value : yearsFrom
});

if (yearsFrom === yearsTo) {
break;
}

if (reverse) {
yearsFrom--;
} else {
yearsFrom++;
}
}

daysInMonth = me.getDaysInMonth(1, new Date().getFullYear());

for (i = 0; i < daysInMonth; i++) {
days.push({
text : i + 1,
value : i + 1
});
}

for (i = 0, ln = Ext.Date.monthNames.length; i < ln; i++) {
months.push({
text : Ext.Date.monthNames[i],
value : i + 1
});
}

var slots = [];

slotOrder.forEach(function (item) {
slots.push(me.createSlot(item, days, months, years));
});

me.setSlots(slots);
},

Reason I did this way was have one spot for the pushing of slot configs.