简体   繁体   English

全日历重新获取事件源

[英]Fullcalendar refetch eventsource

As the title says, I have a question regarding EventSource in the fullcalendar.正如标题所说,我对完整日历中的 EventSource 有疑问。

At the moment I can load 1 google calendar in the fullcalendar.目前我可以在完整日历中加载 1 个谷歌日历。 And know how to add multiple google calendars.并且知道如何添加多个谷歌日历。

However, I want to use checkboxes (linked to their own google calendar), I dynamically create an array with the googleCalendarIds, all this works, but I can't get the calendar to "refetch" all the event from the google calendars in the array.但是,我想使用复选框(链接到他们自己的谷歌日历),我用 googleCalendarIds 动态创建一个数组,所有这些都有效,但我无法让日历从谷歌日历中“重新获取”所有事件大批。

At the moment, this is the code I use to populate the calendar:目前,这是我用来填充日历的代码:

document.addEventListener('DOMContentLoaded', function() {
var selected = [];
$('.badgebox:checked').each(function() {
    selected.push({
        'googleCalendarId' : $(this).val(),
        'className' : $(this).data('color')
    });
});
$('.badgebox').on('click', function() {
    if($(this).prop('checked')) {
        selected.push({
            'googleCalendarId' : $(this).val(),
            'className' : $(this).data('color')
        });
        $('#calendar').fullCalendar('refetchResources');
    }else{
        index = selected.findIndex(obj => obj.googleCalendarId === $(this).val());
        selected.splice(index, 1);
        $('#calendar').fullCalendar('refetchResources');
    }
});
var calendarEl = document.getElementById('calendar');
calendar = new FullCalendar.Calendar(calendarEl, {
    header: {
        center: 'dayGridMonth,timeGridWeek'
    },
    views: {
        dayGridMonth: {
            titleFormat: { year: 'numeric', month: '2-digit', day: '2-digit' }
        }
    },
    plugins: [ 'dayGrid', 'timeGrid', 'bootstrap', 'googleCalendar' ],
    googleCalendarApiKey: 'api key',
    eventSources: selected,
    eventClick: function(info) {
        info.jsEvent.preventDefault();
    },
    defaultView: 'timeGridWeek',
    weekNumbers: true,
    locale: 'nl',
    themeSystem: 'bootstrap',
    nowIndicator: true
});

calendar.render();

}); });

But what I am getting is an error:但我得到的是一个错误:

TypeError: $(...).fullCalendar is not a function

I have loaded all the files needed (and can see they are loaded).我已经加载了所有需要的文件(并且可以看到它们已加载)。

Edit current code编辑当前代码

This is the code I use now, but still not sure how to fix the resources part (refreshing):这是我现在使用的代码,但仍然不确定如何修复资源部分(刷新):

document.addEventListener('DOMContentLoaded', function() {
    var curSource = [];
    $('.badgebox:checked').each(function() {
        curSource.push({
            'googleCalendarId' : $(this).val(),
            'className' : $(this).data('color')
        });
    });
    var newSource = [];
    var calendarEl = document.getElementById('calendar');
    var calendar = new FullCalendar.Calendar(calendarEl, {
        schedulerLicenseKey: 'key',
        header: {
            center: 'dayGridMonth,timeGridWeek'
        },
        views: {
            dayGridMonth: {
                titleFormat: { year: 'numeric', month: '2-digit', day: '2-digit' }
            }
        },
        plugins: [ 'dayGrid', 'timeGrid', 'bootstrap', 'googleCalendar', 'resourceTimeGrid', 'resourceDayGrid' ],
        googleCalendarApiKey: 'apikey',
        eventSources: curSource,
        eventClick: function(info) {
            info.jsEvent.preventDefault();
        },
        defaultView: 'timeGridWeek',
        weekNumbers: true,
        locale: 'nl',
        themeSystem: 'bootstrap',
        nowIndicator: true
    });
    $('.badgebox').on('change', function() {
        if($(this).prop('checked')) {
            newSource.push({
                'googleCalendarId' : $(this).val(),
                'className' : $(this).data('color')
            });
        }else{
            index = newSource.findIndex(obj => obj.googleCalendarId === $(this).val());
            newSource.splice(index, 1);
        }
        curSource = newSource;
        calendar.getEventSources().forEach(eventSource => {
            eventSource.remove()
        });
        calendar.addEventSource(curSource);
    });
    calendar.render();
});

Any idea?任何的想法?

Your logic for adding and removing event sources is flawed - it'll remove all the previous sources, but only ever add the currently selected one (well, except that you never clear newSource so it'll contain all sorts of duplication after a while).您添加和删除事件源的逻辑是有缺陷的——它会删除所有以前的源,但只添加当前选择的源(好吧,除了你永远不会清除newSource所以它会在一段时间后包含各种重复) . The other problem is that when you write calendar.addEventSource(curSource);另一个问题是当你写calendar.addEventSource(curSource); you're adding an array of event sources (even though it only ever contains one item) but adding it as if it was a single event source object .您正在添加一事件源(即使它只包含一个项目),但将其添加为就好像它是单个事件源对象一样。 Therefore it's not in the format fullCalendar expects, so it doesn't add anything.因此它不是 fullCalendar 期望的格式,因此它不会添加任何内容。

Instead you can just use the same logic you use when you first declare the calendar, to loop through all the currently selected checkboxes and set all of them as the current sources.相反,您可以使用与首次声明日历时相同的逻辑来循环遍历所有当前选中的复选框并将它们全部设置为当前源。 This is the simplest way to do it.这是最简单的方法。 Just move that logic into a function so you can re-use it.只需将该逻辑移动到一个函数中,以便您可以重新使用它。 It also removes the necessity for global objects containing the list of sources.它还消除了包含源列表的全局对象的必要性。 Something like this:像这样:

  function getEventSources() {
    var sources = [];
    $(".badgebox:checked").each(function() {
      sources.push({
        id: $(this).val(),
        'googleCalendarId' : $(this).val(),
        className: $(this).data("color")
      });
    });
    return sources;
  }

Then in the calendar config, set the initial event sources by calling the function:然后在日历配置中,通过调用函数设置初始事件源:

eventSources: getEventSources(),

And handle the "change" event on the checkboxes like this:并像这样处理复选框上的“更改”事件:

  $(".badgebox").on("change", function() {
    //remove event sources
    calendar.getEventSources().forEach(eventSource => {
      eventSource.remove();
    });
    //get currently selected sources
    var sources = getEventSources();

    //add each new source to the calendar
    sources.forEach(eventSource => {
      calendar.addEventSource(eventSource);
    });
  });

I made a live demo here: https://codepen.io/ADyson82/pen/Jjoovym .我在这里做了一个现场演示: https ://codepen.io/ADyson82/pen/Jjoovym。 I couldn't use google calendars for the demo obviously so I've done it with hard-coded lists of events, but the overall logic of the process is identical.显然,我不能在演示中使用谷歌日历,所以我用硬编码的事件列表来完成它,但整个过程的逻辑是相同的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM