【发布时间】:2019-11-28 07:48:53
【问题描述】:
正如标题所说,我有一个关于完整日历中的 EventSource 的问题。
目前我可以在全日历中加载 1 个谷歌日历。并且知道如何添加多个谷歌日历。
但是,我想使用复选框(链接到他们自己的谷歌日历),我使用 googleCalendarIds 动态创建一个数组,这一切都有效,但我无法让日历从谷歌“重新获取”所有事件数组中的日历。
目前,这是我用来填充日历的代码:
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();
});
有什么想法吗?
【问题讨论】:
-
$(...).fullCalendar是来自 fullCalendar 版本 3 的基于 jQuery 的语法。看看你自己的代码......你是如何创建日历的?当然,不是通过使用 jQuery。您使用calendar = new FullCalendar.Calendar创建它...并且您将calendar变量作为返回值,表示日历对象。因此,如果您想对日历做任何事情,那么您可以从该对象调用该方法。文档中有很多这样的例子 - 以及您自己的代码(例如calendar.render()...)。 -
一旦你解决了这个问题,你会发现代码仍然没有达到你期望的效果,因为 1) 你没有使用 resources (所以 "refetchResources" 不是您需要什么),以及 2)您实际上并未更改日历中的任何事件源。当您编写
eventSources: selected时,它会传递selected的内容,就像在那个时刻一样,并将其复制到fullCalendar 中。它不维护对原始变量的引用。修改selected不会更新 fullCalendar。 -
如果你想改变加载在日历中的事件源(在第一次初始化之后),你需要使用这里列出的方法:fullcalendar.io/docs/event-source(在底部,在“方法”下)
-
@ADyson,感谢您的反馈。我已经更新了我的代码(我目前正在使用),因为我无法让刷新工作。变量“curSource”已更新(可以在 console.log 中看到),但日历没有用新源刷新(如果没有可用资源,则保持空白)。
-
我想也许你忘了考虑我说 refetchResources() 不是你需要的功能的那句话...... :-)
标签: javascript fullcalendar fullcalendar-scheduler fullcalendar-4