I’m trying to use FullCalendar to display my events from a JSON feed. It’s working fine with the following code:
$(document).ready(function() {
// Initialize calendar
$('#calendar').fullCalendar({
header: {
left: 'prev',
center: 'title',
right: 'next'
},
buttonText: {
prev: 'Previous month',
next: 'Next month'
},
columnFormat: {
month: 'dddd'
},
editable: false,
events: "events.json",
disableDragging: true,
});
I’m trying to create a link that’ll filter the events (ideally, it would be a select menu) using the removeEvents method. When I use the method and pass in an ID, the event gets removed. The documentation states:
idOrFilter may also be a filter function that accepts one Event Object argument and returns true if it should be removed.
I read that the filter function should operate just like jQuery’s grep method, but I don’t understand how to implement it. I started writing the below, but I’m uncertain how to continue. Any suggestions or examples would be greatly appreciated!
...
$('#filter').click(function() {
$('#calendar').fullCalendar ( 'removeEvents', filter(events) );
}
...
function filter (events) {
...
}
If you’re using a
selectto accomplish this, you’ll need some way of associating theoptions in theselectand the events in the calendar. I’ve made theoptions have ids that match those of the events in the calendar:JavaScript (using the
eventsproperty of fullCalendar):Your source of events does not have to be an array, this is just for the example I created.
HTML:
Now, the
removeEventfunction, as you described, takes one event object and returns a boolean value indicating whether or not the event should be removed:What the
filterfunction does is return true if the option selected under the select with idfilterhas an id that matches the id of the event that is passed to it. This function can really be anything, as long as it returns true or false.The way it works under the hood is that when you call
removeEventand pass your function, each of the events living on the calendar gets passed to that function and if true is returned, the event is removed.I’ve made a working example (http://jsfiddle.net/andrewwhitaker/QMyFu/) that also includes another example of a filter function and select element.
Hope that helps!