Stuck trying to get datepicker to play nice with custom holdays, mindate, and no weekends

时间:2015-07-24 12:48:23

标签: javascript jquery ruby-on-rails datepicker

I have a ruby on rails application I am using jquery datepicker. What I am having trouble doing is getting my custom holiday dates and mindate which is today on and no weekends to all work and play nicely together. So far the only one that is working is my custom holidays which is my var penn. Any help would be greatly appreciated!

Here is my application.js

$(document).ready(function(){

  var penn = ["2015-01-01","2015-04-03", "2015-05-25", "2015-07-03", "2015-09-07", "2015-11-26", "2015-12-25", "2016-01-01"];
  var ends = $('#leave_start').datepicker( "option", "beforeShowDay", $.datepicker.noWeekends );
  var today = $('#leave_start').datepicker( "option", "minDate", 0 );


   $('#leave_start').datepicker({
     beforeShowDay: function(date){ 
      var holidays = jQuery.datepicker.formatDate('yy-mm-dd', date); return [ penn.indexOf(holidays) == -1];
      var weekends = jQuery.datepicker.formatDate('yy-mm-dd', date); return [ ends.indexOf(weekends) == -1];
      var after = jQuery.datepicker.formatDate('yy-mm-dd', date); return [ today.indexOf(after) == -1];

    }



   });
});

I tried this and mindate works but, It seems like I cant have multiple beforeShowDays options.

$(document).ready(function(){

 var penn = ["2015-01-01","2015-04-03", "2015-05-25", "2015-07-03", "2015-09-07", "2015-11-26", "2015-12-25", "2016-01-01"];


  $('#leave_start').datepicker({
    beforeShowDay: $.datepicker.noWeekends,
    minDate: 0,
    beforeShowDay: function(date){
      var holidays = jQuery.datepicker.formatDate('yy-mm-dd', date); return [ penn.indexOf(holidays) == -1];}

  });
});

1 个答案:

答案 0 :(得分:1)

不,您不能拥有多个beforeShowDay选项。你可以做的是在一个函数中处理这两种情况:

$('#leave_start').datepicker({
    beforeShowDay: $.datepicker.noWeekends,
    minDate: 0,
    beforeShowDay: function(date) {
        var weekend = $.datepicker.noWeekends(date);
        // If date is selectable (not a weekend day), check if date is a holiday.
        if (weekend[0]) {
            var holidays = jQuery.datepicker.formatDate('yy-mm-dd', date); 
            return [ penn.indexOf(holidays) == -1];
        } else {
            return weekend;
        }
    }
});

JSFiddle demo