2011-12-29 201 views
0

我在我的一個項目中使用JQuery UI日期選擇器。我需要禁用美國假期日期。我試過jQuery UI Datepicker - Disable specific daysCan the jQuery UI Datepicker be made to disable Saturdays and Sundays (and holidays)?JQuery UI:日期選擇器

但他們沒有工作。我怎樣才能讓特殊的日子變得不可選擇。

我也試過用http://tokenposts.blogspot.com/2011/05/jquery-datepicker-disable-specific.html,代碼片段正在運行appart,但不是在我的項目中。這就是:

var unavailableDates = ["31-12-2012"]; 

function unavailable(date) { 
    dmy = date.getDate() + "-" + (date.getMonth()+1) + "-" + date.getFullYear(); 
    if ($.inArray(dmy, unavailableDates) == -1) { 
    return [true, ""]; 
    } else { 
    return [false,"","Unavailable"]; 
    } 
} 


Drupal.behaviors.date_popup = function (context) { 
    for (var id in Drupal.settings.datePopup) { 
    $('#'+ id).bind('focus', Drupal.settings.datePopup[id], function(e) { 
     if (!$(this).hasClass('date-popup-init')) { 
     var datePopup = e.data; 
     // Explicitely filter the methods we accept. 
     switch (datePopup.func) { 
      case 'datepicker': 
      $(this) 
       .datepicker({ minDate: +1, maxDate: "+3Y", beforeShow: unavailable }) 
       .addClass('date-popup-init') 
      $(this).click(function(){ 
       $(this).focus(); 
      }); 
      break; 

      case 'timeEntry': 
      $(this) 
       .timeEntry(datePopup.settings) 
       .addClass('date-popup-init') 
      $(this).click(function(){ 
       $(this).focus(); 
      }); 
      break; 
     } 
     } 
    }); 
    } 
}; 
+0

如果他們工作了誰張貼問題的任擇議定書,我不明白爲什麼它不會爲你...除非你告訴我們,你的代碼試圖告訴我們什麼** **沒有按't工作 – JMax 2011-12-29 10:34:01

+0

我已經張貼 – orif 2011-12-29 11:02:12

回答

0

我發現了這個決定。首先使用其他功能中的「beforeshow」屬性。

natDays = [ 
    [1, 1, 'New Year'], 
    [1, 16, 'Martin Luther King'], 
    [1, 20, 'Inauguration Day'], 
    [2, 14, "St. Valentine's Day"], 
    [2, 20, "Washington's Day"], 
    [5, 21, 'Memorial Day'], 
    [6, 4, 'Independence Day'], 
    [9, 3, 'Labour Day'], 
    [11, 11, 'Veterans Day'], 
    [11, 22, 'Thanks Giving Day'], 
    [12, 25, 'Christmas'], 
    [1, 7, 'Custom'] 
]; 

function nationalDays(date) { 
    for (i = 0; i < natDays.length; i++) { 
     if (date.getMonth() == natDays[i][0] - 1 
       && date.getDate() == natDays[i][1]) { 
      return [false, natDays[i][2] + '_day']; 
     } 
    } 
    return [true, '']; 
} 

function noWeekendsOrHolidays(date) { 
    var noWeekend = $.datepicker.noWeekends(date); 
    if (noWeekend[0]) { 
     return nationalDays(date); 
    } else { 
     return noWeekend; 
    } 
} 


Drupal.behaviors.date_popup = function (context) { 
    for (var id in Drupal.settings.datePopup) { 
     $('#' + id).bind('focus', Drupal.settings.datePopup[id], function(e) { 
      if (!$(this).hasClass('date-popup-init')) { 
       var datePopup = e.data; 
       // Explicitely filter the methods we accept. 
       switch (datePopup.func) { 
        case 'datepicker': 
         $(this) 
           .datepicker({ 
               beforeShowDay: noWeekendsOrHolidays, 
               minDate:+1, 
               maxDate:"+3Y" 
              }) 
           .addClass('date-popup-init') 
         $(this).click(function() { 
          $(this).focus(); 
         }); 
         break; 

        case 'timeEntry': 
         $(this) 
           .timeEntry(datePopup.settings) 
           .addClass('date-popup-init') 
         $(this).click(function() { 
          $(this).focus(); 
         }); 
         break; 
       } 
      } 
     }); 
    } 
}; 
0

上究竟你已經嘗試到現在將是巨大的一些詳細信息,但無論如何:this (very simple) tutorial工作非常適合我 - 只是一個基本的函數返回真/假太beforeShowDay

+0

我試過你的鏈接,但它的工作,但如果與我的項目集成它不工作。 – orif 2011-12-29 10:51:44