清除fullCalendar中所有的事件源

4

我是一名有用的助手,可以为您翻译文本。

我有两种类型的事件在我的fullCalendar中。其中一些是使用eventSources获取的:

    $('calendar').fullCalendar('addEventSource' , 'source') 

很少由用户创建。我正在使用

    $('calendar').fullCalendar('renderEvent', eventData, true)

现在,当点击按钮时,我希望删除从事件源获取的所有事件,并保留用户创建的事件。
我尝试过以下操作:
     $('calendar').fullCalendar('removeEventSource' , function(e){ return true ; } ) ; 

但那样做行不通。我该如何完成工作?

1
你可以用两种方式来实现。一种是在addEventSource中保留对传入源的引用,这个引用可以用于使用removeEventSource函数来删除它们。另一种方法是为用户创建的所有事件添加一个属性,并调用.fullCalendar('removeEvents', function(e){ return !e.customProperty }) - A1rPun
3个回答

5

您可以直接调用:

$('#calendar').fullCalendar('removeEventSources');

这个答案太棒了!!非常简单。其他解决方案非常复杂。谢谢。 - Burak Odabaş

3

我最近在一个项目中做了与你想要做的完全相同的事情。 Fullcalendar支持非标准字段。

非标准字段

除了上面的字段外,您还可以在每个事件对象中包含自己的非标准字段。 FullCalendar不会修改或删除这些字段。例如,开发人员经常包括一个描述字段,用于回调,例如eventRender。

源代码

因此,您可以像这样做

//Save user created event
$('#calendar').fullCalendar('renderEvent', {
    title: title,
    end: end,
    start: start,               
    editable : true,
    //nonstandard field
    isUserCreated: true,
    description: description,               
});

然后移除未由用户创建的事件。
//Get all client events
var allEvents = $('#calendar').fullCalendar('clientEvents');

var userEventIds= [];

//Find ever non usercreated event and push the id to an array
$.each(allEvents,function(index, value){
    if(value.isUserCreated !== true){
    userEventIds.push(value._id);
    }
});

//Remove events with ids of non usercreated events
$('#calendar').fullCalendar( 'removeEvents', userEventIds);

如果您需要更少的控制,那么就像 @A1rPun 建议的那样简单地操作即可。

 $('#calendar').fullCalendar( 'removeEvents', function(e){ return !e.isUserCreated});

0
他们增加了在事件日历中删除事件源和事件的方法,只要您使用2.8.0版本即可。
要从完整日历中删除所有事件源或所有事件,请执行以下操作: $('#calendar').fullCalendar( 'removeEventSources', optionalSourcesArray) 如果未定义optionalSourcesArray,则仅删除所有事件源。在我的情况下,我需要删除事件源,因此我调用了: $('#calendar').fullCalendar( ‘removeEvents’, idOrFilter ) 请参阅此处的两种方法调用的文档:

https://fullcalendar.io/docs/removeEventSources https://fullcalendar.io/docs/removeEvents

您可以在此处阅读有关原始removeEventSources拉取请求及其实际实现的更多信息:

https://github.com/fullcalendar/fullcalendar/issues/948


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接