【问题标题】:bootstrap-daterangepicker change custom range selection behaviorbootstrap-daterangepicker 更改自定义范围选择行为
【发布时间】:2015-12-04 05:42:21
【问题描述】:

有人知道如何更改bootstrap-daterangepicker 允许用户选择自定义日期范围的方式吗?

目前,似乎认为第一次点击的日期是“开始”日期,第二个是“结束”日期。这样做的问题是,如果用户想要一个过去的日期范围并决定首先选择“到”日期,因为它在可见日历上,然后再浏览日历并选择“从”日期。

我想更改它,以便首先选择的任何日期都被视为“开始”日期。并且第二个日期设置为“To”日期,除非第二个日期更早,在这种情况下,它应该交换日期。

我知道用户可以简单地更正输入字段上的日期,但这会破坏日历 UI 的使用。

我希望有人已经采取了类似的方法。如果没有,我可能不得不求助于创建这个插件的一个版本。 (从代码来看,似乎是我需要关注的clickDate函数。)

【问题讨论】:

  • 我在bootstrap中已经遇到过这种问题,我所做的就是使用另一个库并编辑一些代码。如果您想要代码,只需反馈此评论,我会给出答案。 ^_^
  • 我不太清楚你的意思,但我认为这不是引导程序的问题。
  • 是的,你是对的。您可以在这里发布您的代码吗?

标签: jquery daterangepicker


【解决方案1】:

抱歉我之前的回答,我在这里更新了我的代码。也许这会有所帮助。

CLICK HERE FOR THE CODE

【讨论】:

  • 感谢您的回复。我实际上将 daterangepicker 与 jqGrid 的过滤字段一起使用,所以我只有一个日期范围的输入字段。
【解决方案2】:

好的,因为我不想切换到不同的日期范围选择器,所以我决定坚持使用这个,但改变它的 clickDate 行为。我用于clickDate 的代码是这样的,它对它如何设置开始和结束日期进行了注释。注意注释掉的原始行为,然后是指示新行为的新块。还要注意在此之后,为if (this.singleDatePicker) 块添加的else 条件:

    clickDate: function(e) {

        if (!$(e.target).hasClass('available')) return;

        var title = $(e.target).attr('data-title');
        var row = title.substr(1, 1);
        var col = title.substr(3, 1);
        var cal = $(e.target).parents('.calendar');
        var date = cal.hasClass('left') ? this.leftCalendar.calendar[row][col] : this.rightCalendar.calendar[row][col];

        // This was the original behaviour which I replaced below.
        //
        // this function needs to do a few things:
        // * alternate between selecting a start and end date for the range,
        // * if the time picker is enabled, apply the hour/minute/second from the select boxes to the clicked date
        // * if autoapply is enabled, and an end date was chosen, apply the selection
        // * if single date picker mode, and time picker isn't enabled, apply the selection immediately
        //
        // if (this.endDate || date.isBefore(this.startDate)) {
        //     if (this.timePicker) {
        //         var hour = parseInt(this.container.find('.left .hourselect').val(), 10);
        //         if (!this.timePicker24Hour) {
        //             var ampm = cal.find('.ampmselect').val();
        //             if (ampm === 'PM' && hour < 12)
        //                 hour += 12;
        //             if (ampm === 'AM' && hour === 12)
        //                 hour = 0;
        //         }
        //         var minute = parseInt(this.container.find('.left .minuteselect').val(), 10);
        //         var second = this.timePickerSeconds ? parseInt(this.container.find('.left .secondselect').val(), 10) : 0;
        //         date = date.clone().hour(hour).minute(minute).second(second);
        //     }
        //     this.endDate = null;
        //     this.setStartDate(date.clone());
        // } else {
        //     if (this.timePicker) {
        //         var hour = parseInt(this.container.find('.right .hourselect').val(), 10);
        //         if (!this.timePicker24Hour) {
        //             var ampm = this.container.find('.right .ampmselect').val();
        //             if (ampm === 'PM' && hour < 12)
        //                 hour += 12;
        //             if (ampm === 'AM' && hour === 12)
        //                 hour = 0;
        //         }
        //         var minute = parseInt(this.container.find('.right .minuteselect').val(), 10);
        //         var second = this.timePickerSeconds ? parseInt(this.container.find('.right .secondselect').val(), 10) : 0;
        //         date = date.clone().hour(hour).minute(minute).second(second);
        //     }
        //     this.setEndDate(date.clone());
        //     if (this.autoApply)
        //         this.clickApply();
        // }


        // Change the behaviour as follows:
        // - If the first date selected, assign it to the start date.
        // - When the second date is selected, check if it is later than (or equal to) the first date (which is currently the start date). 
        //   If the second date is later than (or is equal to) the first date, then make it the end date.
        //   If the second date is earlier than the first date, make the first date the end date, and make the second date the start date. 
        // - If auto-apply is off, and another date is selected, then treat it as the first date go back to rule #1.
        if (this.startDate && !this.endDate) {
            if (date.isSame(this.startDate) || date.isAfter(this.startDate)) {
                if (this.timePicker) {
                    var hour = parseInt(this.container.find('.left .hourselect').val(), 10);
                    if (!this.timePicker24Hour) {
                        var ampm = cal.find('.ampmselect').val();
                        if (ampm === 'PM' && hour < 12)
                            hour += 12;
                        if (ampm === 'AM' && hour === 12)
                            hour = 0;
                    }
                    var minute = parseInt(this.container.find('.left .minuteselect').val(), 10);
                    var second = this.timePickerSeconds ? parseInt(this.container.find('.left .secondselect').val(), 10) : 0;
                    date = date.clone().hour(hour).minute(minute).second(second);
                }
                this.setEndDate(date.clone());
            } 
            else {
                if (this.timePicker) {
                    var hour = parseInt(this.container.find('.left .hourselect').val(), 10);
                    if (!this.timePicker24Hour) {
                        var ampm = cal.find('.ampmselect').val();
                        if (ampm === 'PM' && hour < 12)
                            hour += 12;
                        if (ampm === 'AM' && hour === 12)
                            hour = 0;
                    }
                    var minute = parseInt(this.container.find('.left .minuteselect').val(), 10);
                    var second = this.timePickerSeconds ? parseInt(this.container.find('.left .secondselect').val(), 10) : 0;
                    date = date.clone().hour(hour).minute(minute).second(second);
                }
                this.endDate = this.startDate.clone();
                this.setStartDate(date.clone());
            } 
        }
        else {
            if (this.timePicker) {
                var hour = parseInt(this.container.find('.right .hourselect').val(), 10);
                if (!this.timePicker24Hour) {
                    var ampm = this.container.find('.right .ampmselect').val();
                    if (ampm === 'PM' && hour < 12)
                        hour += 12;
                    if (ampm === 'AM' && hour === 12)
                        hour = 0;
                }
                var minute = parseInt(this.container.find('.right .minuteselect').val(), 10);
                var second = this.timePickerSeconds ? parseInt(this.container.find('.right .secondselect').val(), 10) : 0;
                date = date.clone().hour(hour).minute(minute).second(second);
            }
            this.setStartDate(date.clone());
            this.endDate = null;
        }

        if (this.singleDatePicker) {
            this.setEndDate(this.startDate);
            if (!this.timePicker)
                this.clickApply();
        } 
        // I also added this else condition:
        // If a date range picker, and both dates are selected, and it is auto-apply, trigger clickApply.
        else if (this.autoApply && !this.timePicker && this.startDate && this.endDate && !this.startDate.isSame(this.endDate)) {
            this.clickApply();
        }

        this.updateView();

    },

我还完全注释掉了 hoverDate 函数,因为我发现当我悬停日历时输入字段值正在发生变化,这令人困惑。但这是我个人的喜好。

更新

如果你想看看我做了什么,你可以在 GitHub here 上查看。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-01-18
    • 2020-09-18
    • 2020-06-09
    • 2017-08-05
    • 2015-11-29
    • 2023-04-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多