From 58e749887373613fc58f733f610a483298633128 Mon Sep 17 00:00:00 2001 From: TheDartCode Date: Mon, 22 Feb 2016 16:12:23 +0200 Subject: [PATCH] Fix bug where when using autoApply:true and selecting a custom range, 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. --- daterangepicker.js | 57 +++++++++++++++++++++++++--------------------- 1 file changed, 31 insertions(+), 26 deletions(-) diff --git a/daterangepicker.js b/daterangepicker.js index 7eab933b..ae06e16e 100644 --- a/daterangepicker.js +++ b/daterangepicker.js @@ -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) { @@ -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) { @@ -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);