首页 > 解决方案 > fullcalendar 4.0 使用循环动态设置事件

问题描述

我正在尝试使用完整的日历来动态设置事件。通过使用函数 loadData()。但我无法成功工作。有人可以让我知道如何解决这个问题吗?

document.addEventListener('DOMContentLoaded', function() {
    var calendarEl = document.getElementById('calendar');

    var temp_array = loadData();
    var calendar = new FullCalendar.Calendar(calendarEl, {
        schedulerLicenseKey: 'GPL-My-Project-Is-Open-Source',
        editable: true,
        eventLimit: true, // allow "more" link when too many events
        header:{
            left: 'prev,next today',
            center: 'title',
            right: 'dayGridMonth'
        },
      plugins: [ 'interaction','dayGrid' ],
        for (let i = 0 ; i <temp_array.length ; i++){
            calendar.addEvent({
                title: temp_array[i][0],
                start:temp_array[i][1],
                end :temp_array[i][2]               
            })
        }

    });
    calendar.render();

  });

function loadData(){
    let temp_array=[];
    temp_array[0]=["test1","2019-07-16","2019-07-21"];
    temp_array[1] = ["test2","2019-07-15","2019-07-20"];
    temp_array[2] =["test1","2019-07-16",""];
    return temp_array;
}

我试过这个它抛出一个

例外:Uncaught SyntaxError: Unexpected identifier

标签: javascriptfullcalendar

解决方案


plugins: [ 'interaction','dayGrid' ],
        for (let i = 0 ; i <temp_array.length ; i++){
            calendar.addEvent({
                title: temp_array[i][0],
                start:temp_array[i][1],
                end :temp_array[i][2]               
            })
        }

您不能for在这样的对象内放置循环。您可以将事件作为数组传递给日历的配置:https ://fullcalendar.io/docs/events-array

编辑:如果你想要动态事件,你也可以传递一个函数:https ://fullcalendar.io/docs/event-source-object#options


推荐阅读