Skip to content

Commit

Permalink
Fix bug where when using autoApply:true and selecting a custom range,…
Browse files Browse the repository at this point in the history
… the preset `chosenLabel` is not cleared correctly and keeps the previous value

This happens because the chosenLabel is not recalculated after selecting the endDate, but waits for the Submit button to be clicked.
  • Loading branch information
TheDartCode committed Feb 22, 2016
1 parent 9cc51fb commit 58e7498
Showing 1 changed file with 31 additions and 26 deletions.
57 changes: 31 additions & 26 deletions daterangepicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -587,30 +587,7 @@
this.container.find('.ranges li').removeClass('active');
if (this.endDate == null) return;

var customRange = true;
var i = 0;
for (var range in this.ranges) {
if (this.timePicker) {
if (this.startDate.isSame(this.ranges[range][0]) && this.endDate.isSame(this.ranges[range][1])) {
customRange = false;
this.chosenLabel = this.container.find('.ranges li:eq(' + i + ')').addClass('active').html();
break;
}
} else {
//ignore times when comparing dates if time picker is not enabled
if (this.startDate.format('YYYY-MM-DD') == this.ranges[range][0].format('YYYY-MM-DD') && this.endDate.format('YYYY-MM-DD') == this.ranges[range][1].format('YYYY-MM-DD')) {
customRange = false;
this.chosenLabel = this.container.find('.ranges li:eq(' + i + ')').addClass('active').html();
break;
}
}
i++;
}
if (customRange) {
this.chosenLabel = this.container.find('.ranges li:last').addClass('active').html();
this.showCalendars();
}

this.calculateChosenLabel();
},

renderCalendar: function(side) {
Expand Down Expand Up @@ -1316,8 +1293,10 @@
date = date.clone().hour(hour).minute(minute).second(second);
}
this.setEndDate(date.clone());
if (this.autoApply)
this.clickApply();
if (this.autoApply) {
this.calculateChosenLabel();
this.clickApply();
}
}

if (this.singleDatePicker) {
Expand All @@ -1330,6 +1309,32 @@

},

calculateChosenLabel: function() {
var customRange = true;
var i = 0;
for (var range in this.ranges) {
if (this.timePicker) {
if (this.startDate.isSame(this.ranges[range][0]) && this.endDate.isSame(this.ranges[range][1])) {
customRange = false;
this.chosenLabel = this.container.find('.ranges li:eq(' + i + ')').addClass('active').html();
break;
}
} else {
//ignore times when comparing dates if time picker is not enabled
if (this.startDate.format('YYYY-MM-DD') == this.ranges[range][0].format('YYYY-MM-DD') && this.endDate.format('YYYY-MM-DD') == this.ranges[range][1].format('YYYY-MM-DD')) {
customRange = false;
this.chosenLabel = this.container.find('.ranges li:eq(' + i + ')').addClass('active').html();
break;
}
}
i++;
}
if (customRange) {
this.chosenLabel = this.container.find('.ranges li:last').addClass('active').html();
this.showCalendars();
}
},

clickApply: function(e) {
this.hide();
this.element.trigger('apply.daterangepicker', this);
Expand Down

0 comments on commit 58e7498

Please sign in to comment.