如何使用jQuery UI Calendar/Date PIcker一周而不是一天?

RSo*_*erg 30 javascript asp.net-mvc jquery jquery-ui

在过去的几个月里,我一直在使用jQuery UI Calendar/Date Picker取得了巨大的成功.我有一个新的要求,允许选择一周(周日 - 周六),而不是一天.

有没有人完成过这个?

  • 按周而不是白天突出显示
  • 在文本框/标签中显示开始日期和结束日期而不是单个日期

Igo*_*ich 54

使用jQuery UI DataPicker的内联周选择器(需要jQuery UI 1.8+):

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.js"></script>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.14/jquery-ui.min.js"></script>
    <link rel="stylesheet" type="text/css" media="screen" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.14/themes/base/jquery-ui.css">
<script type="text/javascript">
$(function() {
    var startDate;
    var endDate;

    var selectCurrentWeek = function() {
        window.setTimeout(function () {
            $('.week-picker').find('.ui-datepicker-current-day a').addClass('ui-state-active')
        }, 1);
    }

    $('.week-picker').datepicker( {
        showOtherMonths: true,
        selectOtherMonths: true,
        onSelect: function(dateText, inst) { 
            var date = $(this).datepicker('getDate');
            startDate = new Date(date.getFullYear(), date.getMonth(), date.getDate() - date.getDay());
            endDate = new Date(date.getFullYear(), date.getMonth(), date.getDate() - date.getDay() + 6);
            var dateFormat = inst.settings.dateFormat || $.datepicker._defaults.dateFormat;
            $('#startDate').text($.datepicker.formatDate( dateFormat, startDate, inst.settings ));
            $('#endDate').text($.datepicker.formatDate( dateFormat, endDate, inst.settings ));

            selectCurrentWeek();
        },
        beforeShowDay: function(date) {
            var cssClass = '';
            if(date >= startDate && date <= endDate)
                cssClass = 'ui-datepicker-current-day';
            return [true, cssClass];
        },
        onChangeMonthYear: function(year, month, inst) {
            selectCurrentWeek();
        }
    });

    $('.week-picker .ui-datepicker-calendar tr').live('mousemove', function() { $(this).find('td a').addClass('ui-state-hover'); });
    $('.week-picker .ui-datepicker-calendar tr').live('mouseleave', function() { $(this).find('td a').removeClass('ui-state-hover'); });
});
</script>
</head>
<body>
    <div class="week-picker"></div>
    <br /><br />
    <label>Week :</label> <span id="startDate"></span> - <span id="endDate"></span>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

在JsFiddle http://jsfiddle.net/manishma/AVZJh/light/上运行它

  • 这很好,但仅适用于日期选择器的内联显示(附加到 div 或 span)。任何建议如何在附加到常规文本字段时修改它? (2认同)
  • jQuery 1.7+ 的替换代码:替换 $('.week-picker .ui-datepicker-calendar tr').live('mousemove', function() { $(this).find('td a').addClass ('ui-state-hover'); }); $('.week-picker .ui-datepicker-calendar tr').live('mouseleave', function() { $(this).find('td a').removeClass('ui-state-hover') ; }); 与: $(document).on('mousemove','.week-picker .ui-datepicker-calendar tr',function() {$(this).find('td a').addClass('ui-state -悬停'); }); $(document).on('mouseleave','.week-picker .ui-datepicker-calendar tr',function() {$(this).find('td a').removeClass('ui-state-hover '); }); (2认同)

小智 6

这是另一种方式. - 用showWeek显示一周. - 使用live()定义beforeShow以附加事件处理程序,以便突出显示周行(包括周数). - 使用die()onclose分离事件处理程序.当您在代码中的其他位置使用普通的日期选择器时,这尤其方便.

$( ".week-picker" ).datepicker({
    dateFormat: "yy-mm-dd",
    showOtherMonths: true,
    selectOtherMonths: true,
    changeMonth: true,
    changeYear: true,
    showWeek: true,
    beforeShow: function(dateText, inst) { 

        // for week highighting
        $(".ui-datepicker-calendar tr").live("mousemove", function() { 
            $(this).find("td a").addClass("ui-state-hover"); 
            $(this).find(".ui-datepicker-week-col").addClass("ui-state-hover");
        });
        $(".ui-datepicker-calendar tr").live("mouseleave", function() { 
            $(this).find("td a").removeClass("ui-state-hover");
            $(this).find(".ui-datepicker-week-col").removeClass("ui-state-hover");      
        });
    },
    onClose: function(dateText, inst) { 
        var wk = $.datepicker.iso8601Week(new Date(dateText));
        if (parseInt(wk) < 10) {
            wk = "0" + wk;
        }           
        var year = $("#ui-datepicker-div .ui-datepicker-year :selected").val();

        if (isNaN(wk)) {
            $(this).val("");
        } else {
            $(this).val(year + ";" + wk);
        }

        // disable live listeners so they dont impact other instances
        $(".ui-datepicker-calendar tr").die("mousemove");
        $(".ui-datepicker-calendar tr").die("mouseleave");

    }
});
Run Code Online (Sandbox Code Playgroud)

  • 这是一个很好的解决方案,也可以内联工作.只是,我会添加一个小修改; 将`$(".ui-datepicker-calendar tr")`更改为`$(".ui-datepicker-calendar tbody tr")`(在两个地方添加`tbody`).这样,表格标题中的"Wk"在突出显示时不会跳转.@Stijn Van Loo也许会感兴趣. (3认同)

San*_*hal 5

我根据接受的答案创建了一个jQuery插件.通过https://github.com/Prezent/jquery-weekpicker或通过Bower 获取.用法示例:

$('#selector').weekpicker({
    startField: '#date-start',
    endField: '#date-end'
});
Run Code Online (Sandbox Code Playgroud)