简体   繁体   English

禁用特定日期并在jQuery datepicker中启用星期几

[英]Disable specific dates and enable specific days of the week in jQuery datepicker

I'm trying to disable specific dates and enable only specific days of the week on a jQuery UI datepicker. 我正在尝试禁用特定日期,并仅在jQuery UI datepicker上启用一周中的特定日期。

This is inside a Wordpress/Woocommerce theme and I'm trying to solve some bugs but I'm here for hours trying to figure out for a solution and even tried every solution I found on StackOverflow and other websites, but nothing seems to work. 这是在Wordpress / Woocommerce主题内,我正在尝试解决一些错误,但是我在这里花了几个小时试图找出解决方案,甚至尝试了在StackOverflow和其他网站上找到的每个解决方案,但似乎没有任何效果。

My objective is to enable only chosen days of the week to be available, but when a date is chosen and bought, I need to disable it too. 我的目标是仅启用一周中选定的日期,但是选择并购买日期后,我也需要将其禁用。 I successfully disabled the days of the week that I need to be disabled, but I can't do even a hardcoded prototype about the bought dates that should be disabled. 我成功禁用了需要禁用的一周中的某几天,但我什至无法制作关于应该禁用的购买日期的硬编码原型。

The code (with some hardcoded sample): 代码(带有一些硬编码的示例):

$(document).ready(function(){
    var available_days = ["3"]; //it comes from the database
    var today = new Date();
    var tour_start_date = new Date( 1525132800000 ); //it comes from the database
    var tour_end_date = new Date( 1546214400000 ); //it comes from the database
    var available_first_date = tour_end_date;
    var lang = 'en_UK';

    lang = lang.replace( '_', '-' );

    today.setHours(0, 0, 0, 0);
    tour_start_date.setHours(0, 0, 0, 0);
    tour_end_date.setHours(0, 0, 0, 0);

    if ( today > tour_start_date ) {
        tour_start_date = today;
    }

    function DisableDays(date) {
        var day = date.getDay();

        if ( available_days.length == 0 ) {
            if ( available_first_date >= date && date >= tour_start_date) {
                available_first_date = date;
            }
            return true;
        }

        if ( $.inArray( day.toString(), available_days ) >= 0 ) {
            if ( available_first_date >= date && date >= tour_start_date) {
                available_first_date = date;
            }
            return true;
        } else {
            return false;
        }
    }
    if ( $('input.date-pick').length ) {
        if ( lang.substring( 0, 2 ) != 'fa' ) {
            $('input.date-pick').datepicker({
                startDate: tour_start_date,
                endDate: tour_end_date,
                beforeShowDay: DisableDays,
                language: lang
            });
            $('input[name="date"]').datepicker( 'setDate', available_first_date );
        } else {
            var date_format = $('input.date-pick').data('date-format');
            $('input.date-pick').persianDatepicker({
                observer: true,
                format: date_format.toUpperCase(),
            });
        }
    }
});

Please review: http://api.jqueryui.com/datepicker/#option-beforeShowDay 请查看: http : //api.jqueryui.com/datepicker/#option-beforeShowDay

A function that takes a date as a parameter and must return an array with: 一个以日期作为参数并且必须返回带有以下内容的数组的函数:

[0] : true/false indicating whether or not this date is selectable [0] :是/否,指示此日期是否可选

[1] : a CSS class name to add to the date's cell or "" for the default presentation [1] :要添加到日期的单元格的CSS类名称,或默认表示形式的""

[2] : an optional popup tooltip for this date [2] :此日期的可选弹出工具提示

The issue in your code was that you were only returning true or false when an Array was needed. 代码中的问题是,仅在需要数组时才返回truefalse For example: 例如:

 $(function() { var available_days = ["3", "5"]; //it comes from the database var today = new Date(); var tour_start_date = new Date(1525132800000); //it comes from the database var tour_end_date = new Date(1546214400000); //it comes from the database var available_first_date = tour_end_date; var lang = 'en_UK'; lang = lang.replace('_', '-'); today.setHours(0, 0, 0, 0); tour_start_date.setHours(0, 0, 0, 0); tour_end_date.setHours(0, 0, 0, 0); if (today > tour_start_date) { tour_start_date = today; } function DisableDays(date) { var day = date.getDay(); var found = false; if (available_days.length == 0) { if (available_first_date >= date && date >= tour_start_date) { available_first_date = date; } found = true; } if ($.inArray(day.toString(), available_days) >= 0) { console.log(day + " in Array"); if (available_first_date >= date && date >= tour_start_date) { available_first_date = date; } found = true; } console.log(day, found); return [found, "tour-date", "Tour Date"]; } if ($('input.date-pick').length) { if (lang.substring(0, 2) != 'fa') { $('input.date-pick').datepicker({ startDate: tour_start_date, endDate: tour_end_date, beforeShowDay: DisableDays, language: lang }); $('input[name="date"]').datepicker('setDate', available_first_date); } else { var date_format = $('input.date-pick').data('date-format'); $('input.date-pick').persianDatepicker({ observer: true, format: date_format.toUpperCase(), }); } } }); 
 <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/smoothness/jquery-ui.css"> <script src="//code.jquery.com/jquery-1.12.4.js"></script> <script src="//code.jquery.com/ui/1.12.1/jquery-ui.js"></script> Date: <input type="text" class="date-pick" name="date" /> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM