【发布时间】:2021-11-12 04:26:06
【问题描述】:
我想创建一个日历,让用户选择多天,然后根据所选日期创建事件并清除所选日期。
到目前为止,我可以选择多天(感谢code from DanielST)并获得选定的日期。现在我想实现一个“刷新”按钮来清除当前选定的日期。但是,我还不能实现它。我已经尝试过 ('refetchEvents') 和 ('rerenderEvents'),但它不起作用。
如何实现?
JS (JSFiddle):
window.selectedDates = [];
window.batchEvents = [];
window.batchIDs = [];
$('#calendar').fullCalendar({
defaultDate: '2014-11-10',
defaultView: 'month',
events: [{
start: '2014-11-12T13:00:00',
end: '2014-11-12T16:00:00',
}, ],
selectable: true,
select: function(start, end, jsEvent, view) {
let startDate = start.format();
console.log('start: ' + startDate);
let newID = randomIntFromInterval(0,100);
window.batchIDs.push({id:newID});
let newEventSource = {
id: newID,
start: start,
end: end,
rendering: 'background',
block: true,
};
window.batchEvents.push(newEventSource);
$("#calendar").fullCalendar('addEventSource', [newEventSource]);
$("#calendar").fullCalendar("unselect");
window.selectedDates.push(startDate);
},
selectOverlap: function(event) {
return !event.block;
},
unselectAuto: true,
unselect: function(jsEvent,view) {
//console.log(jsEvent);
},
customButtons: {
refreshbutton: {
text: 'refresh',
click: function () {
console.log('refresh clicked');
console.log(window.batchEvents);
console.log(window.batchIDs);
$("#calendar").fullCalendar('removeEventSources',window.batchEvents);
window.selectedDates = [];
window.batchIds = [];
}
},
selectedButton: {
text: 'Check Days',
click: function () {
console.log(window.selectedDates);
console.log(window.batchEvents);
}
}
},
header: {
left: 'prev,next today refreshbutton selectedButton',
center: 'title',
right: 'month,agendaWeek,agendaDay'
}
});
function randomIntFromInterval(min, max) { // min and max included
return Math.floor(Math.random() * (max - min + 1) + min)
}
【问题讨论】:
-
仅仅刷新事件不会改变它们或删除任何东西。您需要删除在
select回调期间创建的所有事件源。为此,您需要提供这些事件源 ID 并保留它们的列表。然后您可以使用fullcalendar.io/docs/v3/removeEventSources 删除它们。 -
@ADyson 您好,感谢您的回复,但我说的是清除选定的日期(例如,当我单击某一天或按住并选择日历上的天数时)。在 JSFiddle 上,选定的日期用绿色标记。我希望用户能够清除/重置它们。
-
我知道你在说什么。如果您查看代码,这些“选择”实际上是通过创建包含背景事件的事件源来添加的(正如它在您提供的链接中描述的那样)。因此,要清除它们,您需要删除这些事件源。 (我个人认为在初始化日历时声明 one 事件源并将所有选择事件放入其中会更有意义,但这是另一回事。)
-
@ADyson 啊,我明白了。很抱歉对于这个误会。那么我如何从“选择”中获取 id 呢?从doc 开始,它只有 start、end、jsEvent 和 view。我正在尝试从 jsEvent 获取 id,但我无法获取它。
-
jsEvent 与它无关。您需要在运行 addEventSource 时创建 ID。目前您的代码不会生成一个。它可以作为事件源对象的一部分传递给 fullCalendar(请检查documentation 然后您需要将其记录在一个 ID 数组中。然后当您想要清除选择时重新使用该数组,因为您可以将其直接传递给 removeEventSources 函数。
标签: jquery fullcalendar fullcalendar-3