正如标题所述,我在完整日历中有一个关于EventSource的问题。
目前,我可以在全日历中加载1个Google日历。并且知道如何添加多个Google日历。
但是,我想使用复选框(链接到他们自己的Google日历),我使用googleCalendarIds动态创建一个数组,所有这些都有效,但是我无法让日历从Google日历中“重新提取”所有事件在数组中。
此刻,这是我用来填充日历的代码:
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();
});
但是我得到的是一个错误:
TypeError: $(...).fullCalendar is not a function
我已经加载了所有需要的文件(并且可以看到它们已加载)。
编辑当前代码
这是我现在使用的代码,但仍然不确定如何修复资源部分(刷新):
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();
});
任何想法?
您添加和删除事件源的逻辑存在缺陷-它会删除所有以前的源,但是只会添加当前选择的源(嗯,除非您永远不清除newSource
,否则它将包含所有重复项过了一会儿)。另一个问题是,当您编写calendar.addEventSource(curSource);
时,您将添加事件源的[[array(即使它仅包含一个项目),但是将其添加为好像是单个事件源object 。因此,它不是fullCalendar期望的格式,因此它不会添加任何内容。
function getEventSources() {
var sources = [];
$(".badgebox:checked").each(function() {
sources.push({
id: $(this).val(),
'googleCalendarId' : $(this).val(),
className: $(this).data("color")
});
});
return sources;
}
然后在日历配置中,通过调用函数来设置初始事件源:
eventSources: getEventSources(),
并像这样处理复选框上的“更改”事件:
$(".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); }); });
我在此处进行了现场演示:https://codepen.io/ADyson82/pen/Jjoovym。显然,我无法使用Google日历进行演示,因此我使用事件的硬编码列表完成了此操作,但是该过程的总体逻辑是相同的。