views:

866

answers:

1

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 buttom 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.

A: 

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).

justkt
Thanks for your reply my events are come from serverside by usingevents: "json-events.php"So do I need ajax call to retrieve events?Is it possible to write a javascript function that accept date as parameter and return event list of given date without going back to server side?Because I can see the events for date on client browser. Isn't it possible to query events from calendar dom?
zawmn83
If you are trying to do work via the DOM, looking at the code in the fullcalendar.js file will help you out signficantly. You'll notice that Adam Shaw, the creator, counts tds to know what date to place various events on. So yes, it's definitely possible to do this. It's just going to be a lot more painful and require a lot of DOM manipulation - perhaps enough that a round-trip to the server would be just as fast. Also, DOM manipulation won't get you the JSON of the events, but the divs that are placed inside to describe the events, showing only the displayed info. May be enough, may not.
justkt
I can see the td that represent each date in calendar dom.But events are stored as separate divs after calendar table.I don't see this event divs and td relation. So where can I get the relation between event div and date td?Thanks
zawmn83
For what view? Month? AgendaDay? AgendaWeek? BasicWeek? BasicDay? For week and day, the headers have each date, that is true. But then in the same column (for week and day) are the events, in tds. For month view they are associated. At least, this is true in 1.4.3 and 1.4.4.
justkt
I'm using Month View. Could you tell me more detail how date and events are associated?
zawmn83
Try looking in the _renderSegs code (or renderSegs before 1.4.4). It shows you how the structure of the divs are built up.
justkt