修改Daterangepicker双日历选择日期的功能

原本是选择起始日期后要再选择结束日期。


功能改成,左边日历选择起始日期,右边日历选择结束日期。


showdaterangepicker2.gif

Demo的js代码:

$('#demo').daterangepicker({
    startDate: moment().subtract(6, 'days'),
    endDate: moment(),
    "opens": "left",
    "linkedCalendars": false,
    "locale": {
        format: 'YYYY-MM-DD',
        separator: ' ~ '
    },
    ranges: {
        'Last 7 Days': [moment().subtract(6, 'days'), moment()],
        'Last 30 Days': [moment().subtract(29, 'days'), moment()],
        'This Month': [moment().startOf('month'), moment().endOf('month')],
        'Last Month': [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')]
    }
}, function(start, end, label) {
    console.log(this.startDate.format(this.locale.format));
    console.log(this.endDate.format(this.locale.format));
});


修改daterangepicker.js的代码:

1、修改在1200行的 hoverDate函数,注释五行代码,新增十行代码:

hoverDate: function(e) {
    //ignore mouse movements while an above-calendar text input has focus
    //if (this.container.find('input[name=daterangepicker_start]').is(":focus") || this.container.find('input[name=daterangepicker_end]').is(":focus"))
    //    return;

    //ignore dates that can't be selected
    if (!$(e.target).hasClass('available')) return;

    //have the text inputs above calendars reflect the date being hovered over
    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];

//注释下面五行*******************************************
    // if (this.endDate && !this.container.find('input[name=daterangepicker_start]').is(":focus")) {
    //     this.container.find('input[name=daterangepicker_start]').val(date.format(this.locale.format));
    // } else if (!this.endDate && !this.container.find('input[name=daterangepicker_end]').is(":focus")) {
    //     this.container.find('input[name=daterangepicker_end]').val(date.format(this.locale.format));
    // }

    //highlight the dates between the start date and the date being hovered as a potential end date
    var leftCalendar = this.leftCalendar;
    var rightCalendar = this.rightCalendar;
    var startDate = this.startDate;
    if (!this.endDate) {
        this.container.find('.calendar tbody td').each(function(index, el) {

            //skip week numbers, only look at dates
            if ($(el).hasClass('week')) return;

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

            if ((dt.isAfter(startDate) && dt.isBefore(date)) || dt.isSame(date, 'day')) {
                $(el).addClass('in-range');
            } else {
                $(el).removeClass('in-range');
            }

        });
    }

//新增下面十行,鼠标悬停在左边日历,就改变左边日历的输入框的样式*******************
    var leri = cal.hasClass('left') ? "left" : "right";
    if(leri == "left"){
        this.container.find('input[name=daterangepicker_start]').val(date.format(this.locale.format));
        this.container.find('input[name="daterangepicker_end"]').removeClass('active');
        this.container.find('input[name="daterangepicker_start"]').addClass('active');
    }else{
        this.container.find('input[name=daterangepicker_end]').val(date.format(this.locale.format));
        this.container.find('input[name="daterangepicker_start"]').removeClass('active');
        this.container.find('input[name="daterangepicker_end"]').addClass('active');
    }
},

2、修改在1300行的 clickDate函数,注释两行代码,新增十二行代码:

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 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 one of the inputs above the calendars was focused, cancel that manual input
    //

    if (this.endDate || date.isBefore(this.startDate, 'day')) { //picking start
        if (this.timePicker) {
            var hour = parseInt(this.container.find('.left .hourselect').val(), 10);
            if (!this.timePicker24Hour) {
                var ampm = this.container.find('.left .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());

//新增十二行,鼠标点击了左边日历,就改变起始日期********************************
        var cal = $(e.target).parents('.calendar');
        var leri = cal.hasClass('left') ? "left" : "right";
        if(leri == "left"){
            if(date.isAfter(this.endDate, 'day')){
                this.setStartDate(date.clone());
                this.setEndDate(date.clone());
            }else{
                this.setStartDate(date.clone());
            }
        }else{
            this.setEndDate(date.clone());
        }

    } else if (!this.endDate && date.isBefore(this.startDate)) {
        //special case: clicking the same date for start/end,
        //but the time of the end date is before the start date
        this.setEndDate(this.startDate.clone());
    } else { // picking end
        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.calculateChosenLabel();
          this.clickApply();
        }
    }

    if (this.singleDatePicker) {
        this.setEndDate(this.startDate);
        if (!this.timePicker)
            this.clickApply();
    }

    this.updateView();

    //This is to cancel the blur event handler if the mouse was in one of the inputs
    e.stopPropagation();

},

3、修改在500行的 updateView函数,注释七行代码:

updateView: function() {
    if (this.timePicker) {
        this.renderTimePicker('left');
        this.renderTimePicker('right');
        if (!this.endDate) {
            this.container.find('.right .calendar-time select').attr('disabled', 'disabled').addClass('disabled');
        } else {
            this.container.find('.right .calendar-time select').removeAttr('disabled').removeClass('disabled');
        }
    }

//注释下面七行,因为点击右边日历时,左边日历的输入框会闪一下,去掉***********************
    // if (this.endDate) {
    //     this.container.find('input[name="daterangepicker_end"]').removeClass('active');
    //     this.container.find('input[name="daterangepicker_start"]').addClass('active');
    // } else {
    //     this.container.find('input[name="daterangepicker_end"]').addClass('active');
    //     this.container.find('input[name="daterangepicker_start"]').removeClass('active');
    // }
    this.updateMonthsInView();
    this.updateCalendars();
    this.updateFormInputs();
},
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 160,026评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,655评论 1 296
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,726评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,204评论 0 213
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,558评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,731评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,944评论 2 314
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,698评论 0 203
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,438评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,633评论 2 247
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,125评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,444评论 3 255
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,137评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,103评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,888评论 0 197
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,772评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,669评论 2 271

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 11,623评论 4 59
  • 文/何生 昨天看了《长发公主》很想聊聊女巫这个角色。说起“女巫”二字,大多会被贴上“坏人“这个标签,别不相信问问你...
    堂鼓笙未满阅读 207评论 0 0
  • 冷月映疏桐,元旦人欢笑。 犹见知音热闹来,共祝新年到。 逝去记心头,感谢时光早。 展望新春无限美,大展宏图好。
    三门峡745沈莉红阅读 494评论 10 9
  • 几年前我还是个职场的新手,转眼间,几经锤炼,我已经成为一个职场老人。如今,我碰到越来越多的职场新人,犹如见到自己曾...
    张文昭阅读 450评论 0 0
  • 档案室大叔这两天很苦恼,因为他的卖水果的生意被有心机的人举报了,为此他的领导找他谈话,让他最近还是收敛一点,...
    45度向阳阅读 187评论 0 0