jQuery UI Datepicker - 日期范围 - 突出显示之间的日期

发布于 2024-12-11 02:39:21 字数 1831 浏览 0 评论 0原文

我正在寻找一种方法来突出显示鼠标悬停时两个输入的日期范围之间的日期

这个例子几乎完成了我想要实现的目标: http://hackingon.net/files/jquery_datepicker/range.htm

唯一的区别是所选范围的突出显示应该发生在两个单独的日期选择器和鼠标悬停时。

有什么建议吗?


更新:

好的,更多细节:

从第一个日期选择器中选择日期后,第二个日期选择器应突出显示先前选择的日期。如果您将鼠标悬停在上一个所选日期之后的一天上,则中间的所有日期都应通过添加课程来突出显示。


更新: 这是我走了多远:

    $("#input-service_date_leave, #input-service_date_return").datepicker({
        rangeSelect: true,
        beforeShow: customRange,
        onSelect: customRange,
    });

    function customRange(input) {
        if (input.id == "input-service_date_leave") {

            $("#ui-datepicker-div td").die();

            if (selectedDate != null) { 
                $('#input-service_date_return').datepicker('option', 'minDate', selectedDate).datepicker('refresh');
            }
        }
        if (input.id == "input-service_date_return") {

            $("#ui-datepicker-div td").live({
                mouseenter: function() {
                    $(this).prevAll("td:not(.ui-datepicker-unselectable)").addClass("highlight");
                },
                mouseleave: function() {
                    $("#ui-datepicker-div td").removeClass("highlight");
                }
            });

            var selectedDate = $("#input-service_date_leave").datepicker("getDate");                
            if (selectedDate != null) { 
                $('#input-service_date_return').datepicker('option', 'minDate', selectedDate).datepicker('refresh');
            }
        }
    }

http://jsfiddle.net/mayko/WbWg3/1/

唯一的问题是,实时事件仅突出显示当前悬停行的 td,而不是之前行的 td。

有什么想法吗?

I'm looking for a way of highlighting the days in between the date range of 2 inputs on mouse over.

This example is nearly doing what I want to achieve:
http://hackingon.net/files/jquery_datepicker/range.htm

Only difference is that the highlighting of the selected range should happen on two separate datepickers and on mouse over.

Any suggestions?


Update:

Ok, a bit more details:

After selecting a date from the first datepicker, the second datepicker should highlight the previous selected date. If you then mouse over a day past the previous selected day, all days in between should highlight by adding a class.


Update:
This is how far I got:

    $("#input-service_date_leave, #input-service_date_return").datepicker({
        rangeSelect: true,
        beforeShow: customRange,
        onSelect: customRange,
    });

    function customRange(input) {
        if (input.id == "input-service_date_leave") {

            $("#ui-datepicker-div td").die();

            if (selectedDate != null) { 
                $('#input-service_date_return').datepicker('option', 'minDate', selectedDate).datepicker('refresh');
            }
        }
        if (input.id == "input-service_date_return") {

            $("#ui-datepicker-div td").live({
                mouseenter: function() {
                    $(this).prevAll("td:not(.ui-datepicker-unselectable)").addClass("highlight");
                },
                mouseleave: function() {
                    $("#ui-datepicker-div td").removeClass("highlight");
                }
            });

            var selectedDate = $("#input-service_date_leave").datepicker("getDate");                
            if (selectedDate != null) { 
                $('#input-service_date_return').datepicker('option', 'minDate', selectedDate).datepicker('refresh');
            }
        }
    }

http://jsfiddle.net/mayko/WbWg3/1/

Only problem, the live event just highlights the td's of the current hovered row, but not the td's of the rows before.

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

凉城凉梦凉人心 2024-12-18 02:39:21

我在你的脚本中添加了一些内容。在 JSFiddle 上工作得非常顺利。看一下然后告诉我。

    $("#input-service_date_leave, #input-service_date_return").datepicker({
        rangeSelect: true,
        beforeShow: customRange,
        onSelect: customRange,
    });

    function customRange(input) {
        if (input.id == "input-service_date_leave") {

            $("#ui-datepicker-div td").die();

            if (selectedDate != null) {
                $('#input-service_date_return').datepicker('option', 'minDate', selectedDate).datepicker('refresh');
            }
        }
        if (input.id == "input-service_date_return") {

            $("#ui-datepicker-div td").live({
                mouseenter: function() {
                    $(this).parent().addClass("finalRow");
                    $(".finalRow").prevAll().find("td:not(.ui-datepicker-unselectable)").addClass("highlight");
                    $(this).prevAll("td:not(.ui-datepicker-unselectable)").addClass("highlight");
               },
                mouseleave: function() {
                    $(this).parent().removeClass("finalRow");
                    $("#ui-datepicker-div td").removeClass("highlight");
                }
            });

            var selectedDate = $("#input-service_date_leave").datepicker("getDate");                
            if (selectedDate != null) {
                $('#input-service_date_return').datepicker('option', 'minDate', selectedDate).datepicker('refresh');
            }
        }
    }

I added a bit to your script. Worked like a charm on JSFiddle. Take a look and let me know.

    $("#input-service_date_leave, #input-service_date_return").datepicker({
        rangeSelect: true,
        beforeShow: customRange,
        onSelect: customRange,
    });

    function customRange(input) {
        if (input.id == "input-service_date_leave") {

            $("#ui-datepicker-div td").die();

            if (selectedDate != null) {
                $('#input-service_date_return').datepicker('option', 'minDate', selectedDate).datepicker('refresh');
            }
        }
        if (input.id == "input-service_date_return") {

            $("#ui-datepicker-div td").live({
                mouseenter: function() {
                    $(this).parent().addClass("finalRow");
                    $(".finalRow").prevAll().find("td:not(.ui-datepicker-unselectable)").addClass("highlight");
                    $(this).prevAll("td:not(.ui-datepicker-unselectable)").addClass("highlight");
               },
                mouseleave: function() {
                    $(this).parent().removeClass("finalRow");
                    $("#ui-datepicker-div td").removeClass("highlight");
                }
            });

            var selectedDate = $("#input-service_date_leave").datepicker("getDate");                
            if (selectedDate != null) {
                $('#input-service_date_return').datepicker('option', 'minDate', selectedDate).datepicker('refresh');
            }
        }
    }
哽咽笑 2024-12-18 02:39:21

编辑:此脚本不适用于 jquery 3。但它适用于版本 1 和 2

这个 JSFiddle 是使用 2 个日期表(多个月)执行此操作的示例

$("#input-service_date_leave, #input-service_date_return").datepicker({
    rangeSelect: true,
    beforeShow: customRange,
    onSelect: customRange,
    numberOfMonths: [1, 2],
});

function customRange(input) {
    if (input.id == "input-service_date_leave") {

        $("#ui-datepicker-div td").die();

        if (selectedDate != null) {
            $('#input-service_date_return').datepicker('option', 'minDate', selectedDate).datepicker('refresh');
        }
    }
    if (input.id == "input-service_date_return") {

        $("#ui-datepicker-div td").live({
            mouseenter: function() {
                $(this).parent().addClass("finalRow");
                $(".finalRow").parents('.ui-datepicker-group-last').parent().find('.ui-datepicker-group-first').find('tr').last().addClass("finalRowRangeOtherTable");
                $(".finalRowRangeOtherTable").find("td:not(.ui-datepicker-unselectable)").addClass("highlight");
                $(".finalRowRangeOtherTable").prevAll().find("td:not(.ui-datepicker-unselectable)").addClass("highlight");

                $(".finalRow").prevAll().find("td:not(.ui-datepicker-unselectable)").addClass("highlight");
                $(this).prevAll("td:not(.ui-datepicker-unselectable)").addClass("highlight");
           },
            mouseleave: function() {
                $(this).parent().removeClass("finalRow");
                $("#ui-datepicker-div td").removeClass("highlight");

                  $(".finalRowRange").removeClass("finalRowRange").find('.highlight').removeClass("highlight");
            $(".finalRowRangeOtherTable").removeClass("finalRowRangeOtherTable").find('.highlight').removeClass("highlight");

            }
        });

        var selectedDate = $("#input-service_date_leave").datepicker("getDate");                
        if (selectedDate != null) {
            $('#input-service_date_return').datepicker('option', 'minDate', selectedDate).datepicker('refresh');
        }
    }
}

edit: This script does not work on jquery 3. however it does work on version 1 and 2

this JSFiddle is an example of doing it with 2 date tables ( multiple months )

$("#input-service_date_leave, #input-service_date_return").datepicker({
    rangeSelect: true,
    beforeShow: customRange,
    onSelect: customRange,
    numberOfMonths: [1, 2],
});

function customRange(input) {
    if (input.id == "input-service_date_leave") {

        $("#ui-datepicker-div td").die();

        if (selectedDate != null) {
            $('#input-service_date_return').datepicker('option', 'minDate', selectedDate).datepicker('refresh');
        }
    }
    if (input.id == "input-service_date_return") {

        $("#ui-datepicker-div td").live({
            mouseenter: function() {
                $(this).parent().addClass("finalRow");
                $(".finalRow").parents('.ui-datepicker-group-last').parent().find('.ui-datepicker-group-first').find('tr').last().addClass("finalRowRangeOtherTable");
                $(".finalRowRangeOtherTable").find("td:not(.ui-datepicker-unselectable)").addClass("highlight");
                $(".finalRowRangeOtherTable").prevAll().find("td:not(.ui-datepicker-unselectable)").addClass("highlight");

                $(".finalRow").prevAll().find("td:not(.ui-datepicker-unselectable)").addClass("highlight");
                $(this).prevAll("td:not(.ui-datepicker-unselectable)").addClass("highlight");
           },
            mouseleave: function() {
                $(this).parent().removeClass("finalRow");
                $("#ui-datepicker-div td").removeClass("highlight");

                  $(".finalRowRange").removeClass("finalRowRange").find('.highlight').removeClass("highlight");
            $(".finalRowRangeOtherTable").removeClass("finalRowRangeOtherTable").find('.highlight').removeClass("highlight");

            }
        });

        var selectedDate = $("#input-service_date_leave").datepicker("getDate");                
        if (selectedDate != null) {
            $('#input-service_date_return').datepicker('option', 'minDate', selectedDate).datepicker('refresh');
        }
    }
}
想挽留 2024-12-18 02:39:21

在这里为内联日期选择器制作了日期范围悬停的示例:
http://codepen.io/denissamoilov/pen/RGKyPb?editors=0010

$(function(){
    var datepicker = {
        container: $("#datepicker"),
        dateFormat: 'mm/dd/yy',
        dates: [null, null],
        status: null,
        inputs: {
            checkin: $('#checkin'),
            checkout: $('#checkout'),
            dates: $('#dates')
        }
    };

    datepicker.container.datepicker({
        numberOfMonths: 2,
        dateFormat: datepicker.dateFormat,
        minDate: 0,
        maxDate: null,

        beforeShowDay: function(date) {
            var highlight = false,
            currentTime = date.getTime(),
            selectedTime = datepicker.dates;

            // Highlight date range
            if ((selectedTime[0] && selectedTime[0] == currentTime) || (selectedTime[1] && (currentTime >= selectedTime[0] && currentTime <= selectedTime[1]))) highlight = true;

            return [true, highlight ? 'ui-datepicker-select' : ""];
        },
        onSelect: function(dateText) {

            if (!datepicker.dates[0] || datepicker.dates[1] !== null) {
                // CHOOSE FIRST DATE

                // fill dates array with first chosen date
                datepicker.dates[0] = $.datepicker.parseDate(datepicker.dateFormat, dateText).getTime();
                datepicker.dates[1] = null;

                // clear all inputs
                datepicker.inputs.checkin.val('');
                datepicker.inputs.checkout.val('');
                datepicker.inputs.dates.val('');

                // set current datepicker state
                datepicker.status = 'checkin-selected';

                // create mouseover for table cell
                $('#datepicker').delegate('.ui-datepicker td', 'mouseover', function(){

                    // if it doesn't have year data (old month or unselectable date)
                    if ($(this).data('year') == undefined) return;

                    // datepicker state is not in date range select, depart date wasn't chosen, or return date already chosen then exit
                    if (datepicker.status != 'checkin-selected') return;

                    // get date from hovered cell
                    var hoverDate = $(this).data('year')+'-'+($(this).data('month')+1)+'-'+$('a',this).html();

                    // parse hovered date into milliseconds
                    hoverDate = $.datepicker.parseDate('yy-mm-dd', hoverDate).getTime();

                    $('#datepicker td').each(function(){

                        // compare each table cell if it's date is in date range between selected date and hovered
                        if ($(this).data('year') == undefined) return;

                        var year = $(this).data('year'),
                            month = $(this).data('month'),
                            day = $('a', this).html();

                        var cellDate = $(this).data('year')+'-'+($(this).data('month')+1)+'-'+$('a',this).html();

                        // convert cell date into milliseconds for further comparison
                        cellDate = $.datepicker.parseDate('yy-mm-dd', cellDate).getTime();

                        if ( (cellDate >= datepicker.dates[0] && cellDate <= hoverDate) || (cellDate <= datepicker.dates[0] && cellDate >= hoverDate) ) {
                            $(this).addClass('ui-datepicker-hover');
                        } else {
                            $(this).removeClass('ui-datepicker-hover');
                        }

                    });
                });

            } else {
                // CHOOSE SECOND DATE

                // push second date into dates array
                datepicker.dates[1] = $.datepicker.parseDate(datepicker.dateFormat, dateText).getTime();

                // sort array dates
                datepicker.dates.sort();

                var checkInDate = $.datepicker.parseDate('@', datepicker.dates[0]);
                var checkOutDate = $.datepicker.parseDate('@', datepicker.dates[1]);

                datepicker.status = 'checkout-selected';

                //fill input fields

                datepicker.inputs.checkin.val($.datepicker.formatDate(datepicker.dateFormat, checkInDate));
                datepicker.inputs.checkout.val($.datepicker.formatDate(datepicker.dateFormat, checkOutDate)).change();

                datepicker.inputs.dates.val(datepicker.inputs.checkin.val() + ' - ' + datepicker.inputs.checkout.val());

            }
        }
    });
});

Made an example of date range hover for an inline datepicker here:
http://codepen.io/denissamoilov/pen/RGKyPb?editors=0010

$(function(){
    var datepicker = {
        container: $("#datepicker"),
        dateFormat: 'mm/dd/yy',
        dates: [null, null],
        status: null,
        inputs: {
            checkin: $('#checkin'),
            checkout: $('#checkout'),
            dates: $('#dates')
        }
    };

    datepicker.container.datepicker({
        numberOfMonths: 2,
        dateFormat: datepicker.dateFormat,
        minDate: 0,
        maxDate: null,

        beforeShowDay: function(date) {
            var highlight = false,
            currentTime = date.getTime(),
            selectedTime = datepicker.dates;

            // Highlight date range
            if ((selectedTime[0] && selectedTime[0] == currentTime) || (selectedTime[1] && (currentTime >= selectedTime[0] && currentTime <= selectedTime[1]))) highlight = true;

            return [true, highlight ? 'ui-datepicker-select' : ""];
        },
        onSelect: function(dateText) {

            if (!datepicker.dates[0] || datepicker.dates[1] !== null) {
                // CHOOSE FIRST DATE

                // fill dates array with first chosen date
                datepicker.dates[0] = $.datepicker.parseDate(datepicker.dateFormat, dateText).getTime();
                datepicker.dates[1] = null;

                // clear all inputs
                datepicker.inputs.checkin.val('');
                datepicker.inputs.checkout.val('');
                datepicker.inputs.dates.val('');

                // set current datepicker state
                datepicker.status = 'checkin-selected';

                // create mouseover for table cell
                $('#datepicker').delegate('.ui-datepicker td', 'mouseover', function(){

                    // if it doesn't have year data (old month or unselectable date)
                    if ($(this).data('year') == undefined) return;

                    // datepicker state is not in date range select, depart date wasn't chosen, or return date already chosen then exit
                    if (datepicker.status != 'checkin-selected') return;

                    // get date from hovered cell
                    var hoverDate = $(this).data('year')+'-'+($(this).data('month')+1)+'-'+$('a',this).html();

                    // parse hovered date into milliseconds
                    hoverDate = $.datepicker.parseDate('yy-mm-dd', hoverDate).getTime();

                    $('#datepicker td').each(function(){

                        // compare each table cell if it's date is in date range between selected date and hovered
                        if ($(this).data('year') == undefined) return;

                        var year = $(this).data('year'),
                            month = $(this).data('month'),
                            day = $('a', this).html();

                        var cellDate = $(this).data('year')+'-'+($(this).data('month')+1)+'-'+$('a',this).html();

                        // convert cell date into milliseconds for further comparison
                        cellDate = $.datepicker.parseDate('yy-mm-dd', cellDate).getTime();

                        if ( (cellDate >= datepicker.dates[0] && cellDate <= hoverDate) || (cellDate <= datepicker.dates[0] && cellDate >= hoverDate) ) {
                            $(this).addClass('ui-datepicker-hover');
                        } else {
                            $(this).removeClass('ui-datepicker-hover');
                        }

                    });
                });

            } else {
                // CHOOSE SECOND DATE

                // push second date into dates array
                datepicker.dates[1] = $.datepicker.parseDate(datepicker.dateFormat, dateText).getTime();

                // sort array dates
                datepicker.dates.sort();

                var checkInDate = $.datepicker.parseDate('@', datepicker.dates[0]);
                var checkOutDate = $.datepicker.parseDate('@', datepicker.dates[1]);

                datepicker.status = 'checkout-selected';

                //fill input fields

                datepicker.inputs.checkin.val($.datepicker.formatDate(datepicker.dateFormat, checkInDate));
                datepicker.inputs.checkout.val($.datepicker.formatDate(datepicker.dateFormat, checkOutDate)).change();

                datepicker.inputs.dates.val(datepicker.inputs.checkin.val() + ' - ' + datepicker.inputs.checkout.val());

            }
        }
    });
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文