2012-06-13 65 views
0

我不希望人們試圖在我的fullCalendar中創建事件。所以,一旦他們選擇日期/時間,我做一個快速檢查如下:fullCalendar gotoDate和getDate可能的錯誤

select: function(start, end, allDay) { 
     // need to check the day first. If the selected day/time < today, throw an alert 
     // otherwise allow the booking of the conference. 
      var now = calendar.fullCalendar('getDate'); 
      if (start < now) 
      { 
       alert('You cannot book a conference in the past!' +start +now); 
       calendar.fullCalendar('unselect'); 
      } 
      else 
      { 
          // other stuff here 
          } 

這個工程很好。如果我點擊比現在更早的時間或日期,我會收到警報,說我不能這麼做。完美的權利?

我正在使用jQuery DatePicker,我可以點擊小日曆視圖中的日期,並在我的fullCalendar中默認爲默認爲僅議程視圖。它工作得很好:

$("#datepicker").datepicker({ 
     changeMonth: true, 
     changeYear: true, 
     minDate: 0, 
     maxDate: "+1Y", 
     onSelect: function(dateText) { 
      // fullCalendar only accepts a date in a specific format so parse the date 
      // from the jQuery DatePicker widget and use that result to move the calendar 
      // to the correct day. 
      var pd = $.fullCalendar.parseDate(dateText); 
      $('#calendar').fullCalendar('gotoDate', pd); 
     } 

    }); 

這個例程使我在議程視圖的正確的一週。然後,我可以在那周選擇一個工作日期。但是,如果我嘗試在使用gotoDate方法前的日期之前創建一個日期事件,那麼我還會在以前創建事件時發生錯誤。看起來'gotoDate'方法實際上是SETTING日期​​。我可以在該日期或之後創建活動,但不能在該日期之前。

如果我只是使用fullCalendar的導航方法,它應該是完美的。但是,因爲fullCalendar沒有「跳轉到日期」選項或小部件,我使用DatePicker創建了自己創建的,我認爲這是一件合乎邏輯的事情。

那麼,這是一個錯誤?還是我在做一些完全錯誤的事情?

+0

如何使用var d = new Date()獲取當前日期?然後將所選日期轉換爲日期對象,然後將其與當前日期進行比較。 –

+0

這可能會更好,但這似乎仍然是一個錯誤恕我直言。將嘗試你的建議。 – PHPMachine

+0

我不認爲這是一個錯誤。 getDate不返回'今天' - 它返回日曆中當前選定的日期。對於月視圖,它是在該月的第一天和最後一天之間選擇的任何日期;對於周視圖來說,這一週是任何一天。所以你的支票(如果(開始<現在))導致問題。此外,如果您認爲這是一個錯誤,請將其與開發人員一起記錄 - 他的網站上也有相同的鏈接。 – ganeshk

回答

2

是的,回答我自己的問題,感謝Kyoka和Ganeshk的評論。碰巧,fullCalendar('getDate')函數返回當前選擇的日期而不是「今天」。這只是我對文檔的錯誤理解。然後,解決方案是使用新的Date對象,在這種情況下它可以很好地工作:

select: function(start, end, allDay) { 
    // need to check the day first. If the selected day/time < today, throw an alert 
    // otherwise allow the booking of the conference. 
     var now = new Date(); 
     if (start < now) 
     { 
      alert('You cannot book a conference in the past!'); 
      calendar.fullCalendar('unselect'); 
     } 
     else 
     { 
         // other stuff here 
         } 

希望這對其他人也有幫助。