2010-11-12 41 views
1

我有能力將外部事件拖放到日曆,其中開始時間的默認行爲是放置事件的位置。我想將默認行爲設置爲將事件的結束時間設置爲開始時間之後1小時。這似乎微不足道,但我似乎無法得到它的工作。下面是我的拖放功能(基本上是可拖放項目演示加上1行)。FullCalendar FullCalendar:在放置功能中設置事件結束

drop: function(date, allDay) { // this function is called when something is dropped 

    // retrieve the dropped element's stored Event Object 
    var originalEventObject = $(this).data('eventObject'); 
    // we need to copy it, so that multiple events don't have a 
    // reference to the same object 
    var copiedEventObject = $.extend({}, originalEventObject); 

    // assign it the date that was reported 
    copiedEventObject.start = date; 
    copiedEventObject.end = date.setHours(date.getHours()+1); // <- should be working 
    copiedEventObject.allDay = allDay; 

    // render the event on the calendar 
    // the last `true` argument determines if the event "sticks" 
    // (http://arshaw.com/fullcalendar/docs/event_rendering/renderEvent/) 
    $('#calendar').fullCalendar('renderEvent', copiedEventObject, true); 

     // is the "remove after drop" checkbox checked? 
    if ($('#drop-remove').is(':checked')) { 
    // if so, remove the element from the "Draggable Events" list 
    $(this).remove(); 
    } 
}, 

任何想法?

謝謝, 喬·奇

回答

5

在這個例子你正在試圖做的那是什麼allDay設置爲true的問題,所以它忽略開始日期指定的時間。如果你滿意說暫且午夜 - 凌晨1時爲默認值,這裏是你能做什麼:

var tempDate = new Date(date); //clone date 
copiedEventObject.start = date; 
copiedEventObject.end = new Date(tempDate.setHours(tempDate.getHours()+1)); // <-- make sure we assigned a date object 
copiedEventObject.allDay = false; //< -- only change 
.... 

編輯:好的,其實我試過這個版本。它似乎工作。

+0

嗨萊利,感謝您的反饋,雖然allday屬性可能會導致問題,我不認爲它是我特定情況的根本原因。看起來開始設置爲(Fri Nov 11 2010 09:30:00 GMT-0800(PST))格式的日期,而結束時間設置爲紀元時間。我在將時代結束從世紀轉換爲UTC的過程中。如果您有任何提示,我會欣賞他們。 – JoeChin 2010-11-13 00:00:09

+0

@JoeChin - 看看我上面的編輯,我這次實際測試過,似乎做的是正確的事情... – Ryley 2010-11-13 00:16:31

+0

這是工作完美。謝謝。 – JoeChin 2010-11-13 00:32:37

0

我無法得到解決張貼Ryley正常工作。它會將外部事件放在日曆的標題中,當我查看本週時,事件將變成細線(它看起來已崩潰)或根本不顯示。這可能是一個fullCalendar版本差異(我使用的是fullCalendar的v2版本)。在fullCalendar的v2中,我能夠在沒有任何日曆視圖中的任何事件問題的情況下工作。

drop: function (date, jsEvent, ui) { // this function is called when an external element is dropped. 

    // retrieve the dropped element's stored Event Object 
    var originalEventObject = $(this).data('eventObject'); 

    // we need to copy it, so that multiple events don't have a reference to the same object 
    var copiedEventObject = $.extend({}, originalEventObject); 

    var sdate = $.fullCalendar.moment(date.format()); // Create a clone of the dropped date. 
    sdate.stripTime();  // The time should already be stripped but lets do a sanity check. 
    sdate.time('08:00:00'); // Set a default start time. 
    copiedEventObject.start = sdate; 

    var edate = $.fullCalendar.moment(date.format()); // Create a clone. 
    edate.stripTime();  // Sanity check. 
    edate.time('12:00:00'); // Set a default end time. 
    copiedEventObject.end = edate; 

    // render the event on the calendar 
    // the last `true` argument determines if the event "sticks" (http://arshaw.com/fullcalendar/docs/event_rendering/renderEvent/) 
    $('#calendar').fullCalendar('renderEvent', copiedEventObject, true); 

},