Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In the FullCalendar dayClick event, can I get the events that already exist in clicked date?

In the FullCalendar dayClick event, can I get the events that already exist in clicked date?

http://arshaw.com/fullcalendar/docs/mouse/dayClick/

Let me explain a little about my project I'm writing a simple php site that allow user to add/edit event on calendar.

  • Only single event can add to a date.
  • If user click on empty date, New event popup will show (I use dayclick event).
  • If user click date with existing event, Edit event popup will show (I use eventclick event).

The problem is when user click outside of event area (upper area or bottom area) of a date with existing event, it raise dayclick instead of eventclick. How can I trigger eventclick event and skip dayclick event if there is existing event in the date?

like image 945
zawmn83 Avatar asked Mar 25 '10 13:03

zawmn83


1 Answers

Are your events stored on the client side or on the server side? If on the client side, this should work specifically if you want the event in that clicked time:

$('#calendar').fullCalendar({
    dayClick: function(date, allDay, jsEvent, view) {
            $('#calendar').fullCalendar('clientEvents', function(event) {
                if(event.start <= date && event.end >= date) {
                    return true;
                }
                return false;
            });
    }
});

This will give events that overlap the clicked time and are stored on the client side. For the all-day slot, it will give all events overlapping that day.

If you want all events on that date itself, regardless of whether the allDay slot or a time slot was clicked.

$('#calendar').fullCalendar({
    dayClick: function(date, allDay, jsEvent, view) {
            if(!allDay) {
                // strip time information
                date = new Date(date.getFullYear(), date.getMonth(), date.getDay());
            }
            $('#calendar').fullCalendar('clientEvents', function(event) {
                if(event.start <= date && event.end >= date) {
                    return true;
                }
                return false;
            });
    }
});

If your events are stored on the server, you'll want to take the date provided in your dayClick callback and use it to construct a call to your server to get the info in whatever format you need.

EDIT If doing a round trip or trying to get the information other ways

$('#calendar').fullCalendar({
    dayClick: function(date, allDay, jsEvent, view) {
            var startDate = new Date(date.getFullYear(), date.getMonth(), date.getDay(), 0, 0, 0).getTime();
            var endDate = new Date(date.getFullYear(), date.getMonth(), date.getDay(), 23, 59, 59).getTime();
            var cache = new Date().getTime();
            $.getJSON("/json-events.php?start="+startDate+"&end="+endDate+"&_="+cache,
                function(data) {
                    // do stuff with the JSOn data
                }
    }
});

And if you don't want the round trip, you can also take a look at what happens in, say, refetchEvents in fullCalendar.js. If you don't mind diverging from the fullCalendar trunk, you can save off fetched events somewhere so that you aren't doing a bunch of DOM manipulation (depending on the number of events, as they get large that will get unwieldy).

like image 116
justkt Avatar answered Oct 25 '22 04:10

justkt