Shiva Krishna M
Shiva Krishna M

Reputation: 139

Removing all the eventSources in fullCalendar

I have two types of events in my fullCalendar. Few are the fetched from the eventSources using :

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

And few are created by the user. I am using

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

Now upon clicking a button I want to remove all the events that are obtained from the eventSources and retain those that are created by the user.

I tried doing :

     $('calendar').fullCalendar('removeEventSource' , function(e){ return true ; } ) ; 

But that doesn't work. How do I achieve doing the job ?

Upvotes: 5

Views: 14015

Answers (3)

Kody_06
Kody_06

Reputation: 315

They added ways to remove both event sources and events in event calendar, as long as you're using 2.8.0.

To remove all event sources or all events from full calendar, do the following:

$('#calendar').fullCalendar( 'removeEventSources', optionalSourcesArray)

If optionalSourcesArray isn't defined, it simply removes all event sources. In my case, I needed to remove event sources, so I called:

$('#calendar').fullCalendar( ‘removeEvents’, idOrFilter )

See the documentation for both method calls here:

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

You can read more about the original removeEventSources pull request and how it's actually implemented here:

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

Upvotes: 0

QChí Nguyễn
QChí Nguyễn

Reputation: 308

You can simply call:

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

Upvotes: 6

VisualBean
VisualBean

Reputation: 5008

I did exactly what you want to do in a recent project. Fullcalendar supports nonstandard fields.

Non-standard Fields

In addition to the fields above, you may also include your own non-standard fields in each Event Object. FullCalendar will not modify or delete these fields. For example, developers often include a description field for use in callbacks such as eventRender.

Source

So you could do something like

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

then to remove events that hasn't been created by user

//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);

or if you need less control then simply (as @A1rPun suggested)

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

Upvotes: 3

Related Questions