2012-10-13 45 views
1

我有一組不可用日期設置在unavailableDates和週末。日期選擇器不可用日期從/到

陣:

<script type="text/javascript">//<![CDATA[ 
var unavailableDates = []; 
{foreach $fixed.rows as $row} 
    var row = {}; 
    row['date_from'] = "{$row.fixed_date_from.value}"; 
    row['date_until'] = "{$row.fixed_date_until.value}"; 
    row['name'] = "{$row.fixed_name.value} ({$row.fixed_type.value})"; 
    unavailableDates.push(row); 
{/foreach} 
//]]></script> 

Q1)我如何添加額外的檢查了,直到日期。我已經按照以下所示的那樣開始工作了? Q2)我可以改進代碼以使用in_array而不是循環遍歷每個日曆日的每個日期嗎?

var days = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"]; 
var unavailableDays = ["Saturday","Sunday"]; 

$("#.datePicker").datepicker($.extend(true, {}, $.initDatePickers.defaults, { 
    beforeShowDay: function(date) { 
     ymd = date.getFullYear() + "-" + ("0"+(date.getMonth()+1)).slice(-2) + "-" + ("0"+date.getDate()).slice(-2); 
     day = new Date(ymd).getDay(); 
     result = null; 

     // Check if date in unavailable array or weekend 
     for(var i = 0; i < unavailableDates.length; i++){ 
      if ((ymd == unavailableDates[i].date_from) && $.inArray(days[day], unavailableDays) < 0) { 
       return [false,"unavailable",unavailableDates[i].name]; 
      } 
     } 

     if(result){ 
      return result; 
     } else { 
      return [true, "available", ""]; 
     } 
    } 
})); 

回答

0

使用befreShowDay選項來分析你的數據陣列,以確定一天爲可選或不

beforeShowDayType: Function(Date date)

函數需要一個日期作爲參數,並必須返回一個數組[ 0]等於真/假,指示該日期是否可選,1等於CSS類名或「」作爲默認演示文稿,[2]此日期的可選彈出式工具提示。在日期選擇器顯示之前每天都會調用它。

相關問題