簡體   English   中英

fullcalendar addEventSource不將事件數組添加到日歷

[英]fullcalendar addEventSource not adding event array to calendar

我正在創建一個通話時間表,其中用戶選擇一個具有成員列表以及輪換的長度和類型的組。 當用戶單擊“創建時間表”按鈕時,日歷上將顯示代表此輪換時間表的事件列表。 將創建事件並將其添加到事件數組,但它們不會顯示在日歷上。 當代碼到達時,

 $('#edit_calendar').fullCalendar('addEventSource', newEvents);

該應用程序只是掛起,日歷上沒有顯示任何事件。 這是為click事件調用的函數:

$("#rotation_schedule_btn").click(function () {
    //create member list order    
    var memberList = [];         
    $("#rotationList li").each(function () {
        memberList.push({
            id: $(this).attr('id'),
            name: $(this).text(),
            color: $(this).css('background-color')
        })
    });

    //start and end date and time for new schedule
    var startDate = new Date($('#schedule_start_date').val());
    var endDate = new Date($('#schedule_end_date').val());

    //remove events between startDate & endDate
    $('#edit_calendar').fullCalendar('removeEvents', function (event) {
        if (event.start.toDate() >= startDate && event.start.toDate() <= endDate
               || event.end.toDate() >= startDate && event.end.toDate() <= endDate) {
            return true;
        }
    });

    //Create events from rotation schedule selected
    var newEvents = [];
    var rotation_length = $('#rotation_type_select option:selected').val();
    var rotation_start_date = new Date(startDate);
    var rotation_end_date = new Date(rotation_start_date);
    rotation_end_date.setDate(rotation_end_date.getDate() + parseInt(rotation_length));
    rotation_end_date.setMinutes(rotation_end_date.getMinutes() - 1);
    var member_index = 0;
    while (rotation_end_date <= endDate)
    {
        var event = new Object();
        event = {
            title: memberList[member_index].name,
            start: rotation_start_date,
            end: rotation_end_date,              
            objectID: memberList[member_index].id,
            color: memberList[member_index].color,
            allDay: true,
            textColor: 'black'
        };
        newEvents.push(event);

        rotation_start_date.setDate(rotation_start_date.getDate() + parseInt(rotation_length));         
        rotation_end_date.setDate(rotation_end_date.getDate() + parseInt(rotation_length));

        if ((memberList.length - 1) == member_index)
        {
            member_index = 0;
        }
        else {
            member_index++;
        }
    }           
    //Render events on calendar
     $('#edit_calendar').fullCalendar('addEventSource', newEvents);
}); //end create schedule button click

為什么newEvents數組不顯示在日歷上? 如果您能告訴我我哪里出問題了,我將不勝感激。

謝謝。

更新在檢查newEvents數組后,似乎所有開始日期和結束日期都相同。 有3個事件,每個事件應持續1周(7天)。 但是所有事件的開始日期和結束日期都相同。

我想我知道發生了什么事。 在這種情況下,您將傳遞對事件對象的引用,而不是rotation_start_date和rotation_end_date的值:

event = {
        title: memberList[member_index].name,
        start: rotation_start_date,
        end: rotation_end_date,              
        objectID: memberList[member_index].id,
        color: memberList[member_index].color,
        allDay: true,
        textColor: 'black'
    };

當您說出這些日期的值時,所有事件都會看到相同的更新,因為它們對這些對象的引用都相同。 您想要做的是聲明新變量以保存循環中的值,或者只是使用rotation_start_date和rotation_end_date中的值將事件的開始和結束日期設置為新日期

    var event = new Object();
    event = {
        title: 'title',
        start: new Date(rotation_start_date),
        end: new Date(rotation_end_date),
        objectID: member_index,
        color: 'blue',
        allDay: true,
        textColor: 'black'
    };

您沒有提供任何代碼,但是我用一些示例代碼創建了一個jsfiddle來測試這個完整的日歷示例

最終答案:

 while (rotation_end_date <= endDate)
    {
        var event = new Object();
        event = {
            title: memberList[member_index].name,
            start: new Date(rotation_start_date),
            end: new Date(rotation_end_date),
            objectID: memberList[member_index].id,
            color: memberList[member_index].color,
            allDay: true,
            textColor: 'black'
        };
        newEvents.push(event);

        rotation_start_date = new Date(rotation_start_date.setDate(rotation_start_date.getDate() + parseInt(rotation_length)));
        rotation_end_date = new Date(rotation_end_date.setDate(rotation_end_date.getDate() + parseInt(rotation_length)));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM