1
votes

FullCalendar provides JavaScript events for when a displayed event is clicked, and for when a date is clicked on the month view.

What I would like, though, is that if a date is clicked, that I can trigger a click on the first contained event within that date. This is in case someone clicks or taps (on a phone) and misses the event itself and hits the cell instead.

If the events were rendered inside the DOM of the cell, that would be a simple matter, but FullCalendar renders the events outside the month display and overlays them on the month grid instead.

An alternative is to do a search in cached data for the correct event to trigger, but I am hoping for a more elegant solution.

1
Have you had a chance to check out my answer to your question?Nate

1 Answers

2
votes

FullCalendar doesn't seem to do us any favors in terms of associating days or events with the DOM elements that represent them. No matter: we can do it ourselves.

Working example: http://jsfiddle.net/nNjFb/

var $calendar = $('#calendar');

$calendar.fullCalendar({
    dayClick: function (date) {
        // Create a string representing just the day/month/year of the date clicked
        dateString = date.getDate() + date.getMonth() + date.getFullYear();

        // Grab the events for that day using a custom filter function
        events = $('#calendar').fullCalendar('clientEvents', function (event) {
            eventDate = event.start;
            eventDateString = eventDate.getDate() + eventDate.getMonth() + eventDate.getFullYear();

            return dateString === eventDateString;
        });

        if (events.length) {
            // Find the first event's element by its class name
            var $event = $calendar.find('.' + events[0]._id);

            $event.trigger('click');
        }
    },
    eventClick: function (event) {
        console.log('event click!', event);
    },
    eventRender: function(event, element) {
        // Every time an event is rendered,
        // add its id as a class name so that
        // we can find it later
        $(element).addClass(event._id);
    }
});

// Add some events

var newEvent1 = {
    title: 'first event',
    start: new Date('October 31 2013 4:00 PM EST'),
    allDay: false
};

var newEvent2 = {
    title: 'second event',
    start: new Date('October 31 2013 6:00 PM EST'),
    allDay: false
};

$calendar.fullCalendar('renderEvent', newEvent1);
$calendar.fullCalendar('renderEvent', newEvent2);

There are two keys here:

  1. Use the eventRender hook to attach a useful class name to the dom elements that are associated with events.

  2. Use the clientEvents method and a custom filter to get an array of events that match the clicked day.