Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fullcalendar - highlighting a day based on event selection

In fullcalendar you can select an event (which triggers) the eventClick function/callback. What I would like to do is highlight the day of the event when the event is clicked (in month view).

For example, if the event is on October 30 and I select the event I would like the background color of the day to be highlighted. This is very similar to how fullcalendar handles "today" where it highlights today in a yellowish color.

I can't seem to figure out how to tie the fc-event class or the event object itself to the actual day div in the calendar. My October 30th event appears within the following div (which is the October 30 box):

<div class="fc-sun fc-widget-content fc-day35 fc-first">

How can I find this div (so that I can highlight it) based on the event object?

like image 265
Arthur Frankel Avatar asked Oct 19 '11 14:10

Arthur Frankel


People also ask

How do I highlight the date in Fullcalendar?

fullCalendar('getDate'); if (moment. get('date') == date. get('date')) { $(cell). addClass('fc-state-highlight'); } else { $(cell).

How do I create a dynamic event in Fullcalendar?

although it is not specified on the fullcalender site, it is necessary to assign a value to the "allday" parameter to be able to add new events dynamically. If you set this value to "false", it will not add the event to the AllDay row. If you do "true" it will add to the AllDay row. Save this answer.

How do I change the color of an event in Fullcalendar?

You can change the color of all events on the calendar like so: var calendar = new Calendar(calendarEl, { events: [ // my event data ], eventColor: '#378006' }); You can use any of the CSS color formats such #f00 , #ff0000 , rgb(255,0,0) , or red .

How do I turn off past dates in Fullcalendar?

You can try the following thread's solution(dayClick or select function to disable the dayclick for the past dates in fullcalendar). You can visit the fullcalendar support center and post your fullcalendar question on the StackOverflow fullcalendar tag.


2 Answers

You can simply use the select method in full calendar.

For example:

$('#calendar').fullCalendar('select', date);

Where date comes from event.start:

eventClick: function( event, jsEvent, view ) {
    //pass event.start to select method
}
like image 115
Fitz Agard Avatar answered Sep 21 '22 12:09

Fitz Agard


Sorry this is a rough solution, mostly from memory, as I'm not set up with any dev tools at this computer.

Hopefully this is what you're looking for!

//create fullCalendar:
$("#calendar").fullCalendar({
    /* options */

    eventClick: function(event, jsEvent){
        //use the passed-in javascript event to get a jQuery-wrapped reference
        //to the DOM element we clicked on.
        //i can never remember if this is .target, .currentTarget, or .originalTarget
        //... jquery has spoiled me
        var $clickedEvent = $(jsEvent.target);

        //tell the "selectionManager" to find the day this event belongs to,
        //and add the "selected" css class to it
        selectionManager.select($clickedEvent);
    }
});

//define an object that handles the adding-removing of the 'selectedDay' css class
var selectionManager = (function(){
    //i like making private variables :-)
    var $curSelectedDay = null

    //define a "select" method for switching 'selected' state
    return {
        select: function($newEvent) {
            if ($curSelectedDay){
                //if we already had a day chosen, let's get rid of its CSS 'selectedDay' class
                $curSelectedDay.removeClass("selectedDay");
            }
            //find the parent div that has a class matching the pattern 'fc-day', and add the "selectedDay" class to it
            $curSelectedDay = $thisEvent.closest('div[class~="fc-day"]').addClass("selectedDay");
        }       
    };
})();
like image 39
Matt H. Avatar answered Sep 19 '22 12:09

Matt H.