简体   繁体   English

jQuery datepicker UI-使用自定义功能禁用英国银行假期

[英]jQuery datepicker UI - Disabling UK Bank Holidays with custom function

I have a piece of code which is used on a Shopify website. 我有一个Shopify网站上使用的代码。 Basically there are 3 delivery options, one of which is Saturday delivery which disables all days apart from Saturdays if that radio is selected. 基本上有3种发送选项,其中一种是“星期六发送”,如果选择了该电台,则星期六除外。

<div class="delivery-radio">
 <h3>1. Choose your delivery type:</h3>
 <input type="radio" name="attributes[delivery-type]" value="Standard" checked> Standard - Delivery by 6pm (&pound;4.95)<br>
<input type="radio" name="attributes[delivery-type]" value="Pre 12" id="pre_12"> Express - Delivery by 12pm (&pound;6.25)<br>
<input type="radio" name="attributes[delivery-type]" value="saturday" id="saturday"> Saturday - Delivery by 6pm (&pound;6.95) 
</div>


<div style="width:300px; clear:both;">
  <h3>2. Pick a delivery date:</h3>
 <p>
  <input id="date" type="text" name="attributes[date]" value="{{ cart.attributes.date }}" required readonly="readonly" /> <span id="calendar_click" class="glyphicon glyphicon-calendar" aria-hidden="true"></span>
</p>
</div>

What I need is a function that doesn't break my current code that disables UK Bank Holidays. 我需要的是一个不会破坏我当前禁用UK Bank Holidays的代码的功能。 I have seen this can be done manually by adding the dates and disabling them but everything I have tried doesn't work or breaks my current code. 我已经看到可以通过添加日期并禁用日期来手动完成此操作,但是我尝试过的所有操作均无效或破坏了当前代码。

Here is a snippet of my code, if any jquery/javascript guru's can help out that would be appreciated as I am not too advanced in this area. 这是我的代码段,如果有任何jquery / javascript大师可以帮忙的话,将不胜感激,因为我在这方面不太先进。

jQuery(function() {

$('input[name="attributes[delivery-type]"]').on('click', function(){
$("#date").datepicker("destroy");
if($(this).val() == "saturday") {      
  $("#date").datepicker({
    dateFormat: 'dd-mm-yy',
    minDate: +1, 
  maxDate: '+2M',
    beforeShowDay: function(date){ return [date.getDay() == 6 || date.getDay() == 6,""]}
});
} else {
$("#date").datepicker(
{ 
  dateFormat: 'dd-mm-yy',
  minDate: +1, 
  maxDate: '+2M',
  beforeShowDay: function(date) {
    var day = date.getDay();
    return [(day != 0 && day != 1), ''];


  } 


});
 }
  });

   $("#date").datepicker(
   { 
    dateFormat: 'dd-mm-yy',
  minDate: +1, 
  maxDate: '+2M',
  beforeShowDay: function(date) {
    var day = date.getDay();
    return [(day != 0 && day != 1), ''];

  } 


});

});

By default I have disabled saturday and sunday deliveries by keeping daysOfWeekDisabled: [0,6] and for Saturday delivery option, I have kept it like - daysOfWeekDisabled: [0,1,2,3,4,5]. 默认情况下,我通过保留daysOfWeekDisabled:[0,6]来禁用周六和周日的交付,对于星期六交付选项,我将其保留为-daysOfWeekDisabled:[0,1,2,3,4,5]。 Bank holidays list resides in datesDisabled. 银行假期列表驻留在dateDisabled中。 Also mindate and maxDate are startDate and endDate. mindate和maxDate分别是startDate和endDate。

        <div class="delivery-radio">
        <h3>1. Choose your delivery type:</h3>
        <input type="radio" name="attributes[delivery-type]" value="Standard" checked> Standard - Delivery by 6pm (&pound;4.95)<br>
       <input type="radio" name="attributes[delivery-type]" value="Pre 12" id="pre_12"> Express - Delivery by 12pm (&pound;6.25)<br>
       <input type="radio" name="attributes[delivery-type]" value="saturday" id="saturday"> Saturday - Delivery by 6pm (&pound;6.95) 
       </div>


       <div style="width:300px; clear:both;">
         <h3>2. Pick a delivery date:</h3>
        <p>
         <input id="date" type="text" name="attributes[date]" value="{{ cart.attributes.date }}" required readonly="readonly" /> <span id="calendar_click" class="glyphicon glyphicon-calendar" aria-hidden="true"></span>
       </p>
       </div>

       <script>

        $(document).ready(function(){
                var maxDate = getAdjustedDate(+60);
                var minDate = getAdjustedDate(+1);
                var tempDt = new Date();
                console.log("today: "+tempDt.getDate());
                console.log("minDate: "+minDate);
                console.log("maxDate: "+maxDate);

            $('input[name="attributes[delivery-type]"]').on('click', function(){
            $("#date").datepicker("destroy");
            if($(this).val() === "saturday") {      
              $("#date").datepicker({
                format: 'dd/mm/yyyy',
                startDate: minDate, 
                endDate: maxDate,
                daysOfWeekDisabled: [0,1,2,3,4,5],
                datesDisabled: ['30/03/2018','02/04/2018','07/05/2018','28/05/2018','27/08/2018','25/12/2018','26/12/2018']
            });
            } else {
            $("#date").datepicker(
            { 
                format: 'dd/mm/yyyy',
                startDate: minDate, 
                endDate: maxDate,
                daysOfWeekDisabled: [0,6],
                datesDisabled: ['30/03/2018','02/04/2018','07/05/2018','28/05/2018','27/08/2018','25/12/2018','26/12/2018']
            });
             }
              });

               $("#date").datepicker(
               { 
                format: 'dd/mm/yyyy',
                startDate: minDate, 
                endDate: maxDate,
                daysOfWeekDisabled: [0,6],
                datesDisabled: ['30/03/2018','02/04/2018','07/05/2018','28/05/2018','27/08/2018','25/12/2018','26/12/2018']
            });


        });

        var getAdjustedDate = function (adjustment) {
            var today = new Date();
            today.setDate(today.getDate() + adjustment);

            var dd = today.getDate()-1;
            var mm = today.getMonth()+1; //January is 0!

            var yyyy = today.getFullYear();
            if(dd<10){
                dd='0'+dd;
            };

            if(mm<10){
                mm='0'+mm;
            };

            return dd+'/'+mm+'/'+yyyy;
        };            

        </script>

datesDisabled appears to be functionality for the bootstrap datepicker. datesDisabled似乎是引导日期选择器的功能。

You can add a check to the beforeShowDay function, I would separate this from the different parts and use variables as necessary. 您可以添加一个检查到beforeShowDay函数,我将其与不同部分分开,并根据需要使用变量。

translate the Saturday deliveries from 翻译周六的交货

beforeShowDay: function(date){ return [date.getDay() == 6 || date.getDay() == 6,""]}

to beforeShowDay: saturdayOnly beforeShowDay: saturdayOnly

and your default 和您的默认

beforeShowDay: function(date) {
    var day = date.getDay();
    return [(day != 0 && day != 1), ''];

  } 

to beforeShowDay: notSunMon beforeShowDay: notSunMon

then add the handling for these two options: 然后为这两个选项添加处理:

``` ```

function saturdayOnly (date) {
    return dayStatus (date, array(0,1,2,3,4,5));
}
function notSunMon (date) {
    return dayStatus (date, array(0,1));
}

var disabledDates = ["22-03-2018", "29-03-2018", "03-04-2018"];

function dayStatus (date, disableDays) {
  //Sat = 6, Sun = 0
  var dayNumber = date.getDay();

  if (disableDays.indexOf(dayNumber) !== -1) {
    return [false, ' disabledDay'];
  }

  var compareDate = formatDate(date);

  if (disabledDates.indexOf(compareDate) !== -1) {
    return [false, ' disabledDate'];
  }

  return [true, ' availableDay'];
}

function formatDate(date) {
  var dd = date.getDate().toString();
  var mm = (date.getMonth() + 1).toString(); // getMonth() is zero-based
  var yyyy = date.getFullYear().toString();

  //format as dd-mm-yyyy - make sure Disabled Dates match this format
  return (dd[1] ? dd : "0" + dd[0]) + "-" + (mm[1] ? mm : "0" + mm[0]) + "-" + yyyy;
}

``` ```

This uses a function to format your dates, to make sure they're all correct, and adds css classes. 它使用一个函数来格式化日期,以确保它们都是正确的,并添加css类。 See the jsfiddle for how these are implemented. 有关如何实现的信息,请参见jsfiddle。

Just a note, days !=0 && day != 1 checks for "not a Sunday/Monday" 请注意, days !=0 && day != 1检查“不是星期天/星期一”

Try it out with jsfiddle: https://jsfiddle.net/vreemt/sbrtyvoz/ 使用jsfiddle进行尝试: https ://jsfiddle.net/vreemt/sbrtyvoz/

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

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