Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Datepicker: handling yearRange when generating the HTML (fixes #7782) #521

Closed
wants to merge 2 commits into from

2 participants

@ghostd

Datepicker: handling yearRange when generating the HTML (fixes #7782: Datepicker does not render proper month when yearRange does not include now

There is one weird thing in the current code (was here before this patch):
why _generateHTML initializes 'currentDate' with new Date(9999, 9, 9) instaed of new Date() ?

@ghostd ghostd Datepicker: handling yearRange when generating the HTML (fixes #7782:…
… Datepicker does not render proper month when yearRange does not include now
c024039
@ghostd

Not sure what is the expected behavior, according to http://bugs.jqueryui.com/ticket/4641, yearRange and maxDate should be independant, so maybe

        if (yearRange) {
            var years = this._getYearRange(yearRange, minDate, maxDate);
            maxDate = maxDate ? maxDate : currentDate;
            maxDate = new Date(years[1], maxDate.getMonth() + 1, maxDate.getDate());
        }

should be

        if (yearRange) {
            var years = this._getYearRange(yearRange, minDate, maxDate);
            drawYear = years[1];
        }
@mikesherov
Collaborator

As kbwood points out here: http://bugs.jqueryui.com/ticket/4641 "The yearRange setting just restricts the years that appear in the drop-down list. If you only want people to select dates within these years, you must use the minDate and maxDate settings."

http://bugs.jqueryui.com/ticket/7782 is a duplicate of that ticket, and so this is a wontfix. Thanks for the contribution anyway, and please feel free to submit more pull requests for any of the other bugs in our tracker: http://bugs.jqueryui.com/

@mikesherov mikesherov closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Nov 8, 2011
  1. @ghostd

    Datepicker: handling yearRange when generating the HTML (fixes #7782:…

    ghostd authored
    … Datepicker does not render proper month when yearRange does not include now
  2. @ghostd
This page is out of date. Refresh to see the latest.
Showing with 28 additions and 13 deletions.
  1. +28 −13 ui/jquery.ui.datepicker.js
View
41 ui/jquery.ui.datepicker.js
@@ -1424,12 +1424,17 @@ $.extend(Datepicker.prototype, {
new Date(inst.currentYear, inst.currentMonth, inst.currentDay)));
var minDate = this._getMinMaxDate(inst, 'min');
var maxDate = this._getMinMaxDate(inst, 'max');
+ var yearRange = this._get(inst, 'yearRange');
var drawMonth = inst.drawMonth - showCurrentAtPos;
var drawYear = inst.drawYear;
if (drawMonth < 0) {
drawMonth += 12;
drawYear--;
}
+ if (yearRange) {
+ var years = this._getYearRange(yearRange, minDate, maxDate);
+ drawYear = years[1];
+ }
if (maxDate) {
var maxDraw = this._daylightSavingAdjust(new Date(maxDate.getFullYear(),
maxDate.getMonth() - (numMonths[0] * numMonths[1]) + 1, maxDate.getDate()));
@@ -1614,22 +1619,12 @@ $.extend(Datepicker.prototype, {
html += '<span class="ui-datepicker-year">' + drawYear + '</span>';
else {
// determine range of years to display
- var years = this._get(inst, 'yearRange').split(':');
- var thisYear = new Date().getFullYear();
- var determineYear = function(value) {
- var year = (value.match(/c[+-].*/) ? drawYear + parseInt(value.substring(1), 10) :
- (value.match(/[+-].*/) ? thisYear + parseInt(value, 10) :
- parseInt(value, 10)));
- return (isNaN(year) ? thisYear : year);
- };
- var year = determineYear(years[0]);
- var endYear = Math.max(year, determineYear(years[1] || ''));
- year = (minDate ? Math.max(year, minDate.getFullYear()) : year);
- endYear = (maxDate ? Math.min(endYear, maxDate.getFullYear()) : endYear);
+ var yearRange = this._get(inst, 'yearRange');
+ var years = this._getYearRange(yearRange, minDate, maxDate);
inst.yearshtml += '<select class="ui-datepicker-year" ' +
'onchange="DP_jQuery_' + dpuuid + '.datepicker._selectMonthYear(\'#' + inst.id + '\', this, \'Y\');" ' +
'>';
- for (; year <= endYear; year++) {
+ for (var year = years[0]; year <= years[1]; year++) {
inst.yearshtml += '<option value="' + year + '"' +
(year == drawYear ? ' selected="selected"' : '') +
'>' + year + '</option>';
@@ -1647,6 +1642,26 @@ $.extend(Datepicker.prototype, {
return html;
},
+ /*
+ * Computes the correct year range according to yearRange, minDate and maxDate
+ * Returns [startYear, endYear]
+ */
+ _getYearRange: function(yearRange, minDate, maxDate) {
+ var years = yearRange.split(':');
+ var thisYear = new Date().getFullYear();
+ var determineYear = function(value) {
+ var year = (value.match(/c[+-].*/) ? drawYear + parseInt(value.substring(1), 10) :
+ (value.match(/[+-].*/) ? thisYear + parseInt(value, 10) :
+ parseInt(value, 10)));
+ return (isNaN(year) ? thisYear : year);
+ };
+ var startYear = determineYear(years[0]);
+ var endYear = Math.max(startYear, determineYear(years[1] || ''));
+ startYear = (minDate ? Math.max(startYear, minDate.getFullYear()) : startYear);
+ endYear = (maxDate ? Math.min(endYear, maxDate.getFullYear()) : endYear);
+ return [startYear, endYear];
+ },
+
/* Adjust one of the date sub-fields. */
_adjustInstDate: function(inst, offset, period) {
var year = inst.drawYear + (period == 'Y' ? offset : 0);
Something went wrong with that request. Please try again.