Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 436
Shiva Krishna M Avatar asked Feb 09 '23 23:02

Shiva Krishna M


2 Answers

You can simply call:

$('#calendar').fullCalendar('removeEventSources');
like image 81
QChí Nguyễn Avatar answered Feb 23 '23 14:02

QChí Nguyễn


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});
like image 39
VisualBean Avatar answered Feb 23 '23 13:02

VisualBean