30

Is there a way to prevent overlapping events in jQuery FullCalendar?

Ricardo Altamirano
  • 14,650
  • 21
  • 72
  • 105
user267637
  • 523
  • 1
  • 5
  • 6

12 Answers12

19

I made a function that checks whether the given event is overlapping other or not. Returns true if the event is overlapping other and false otherwise.

function isOverlapping(event){
    var array = calendar.fullCalendar('clientEvents');
    for(i in array){
        if(array[i].id != event.id){
            if(!(Date(array[i].start) >= Date(event.end) || Date(array[i].end) <= Date(event.start))){
                return true;
            }
        }
    }
    return false;
}

You can use it when dropping or resizing and event and if the event overlaps other use the revertFunc that is received in the eventDrop and eventResize callbacks or cancel the creation of an event in the select callback. In order to use it in the select callback create a dummie event:

var event = new Object();
event.start = start;
event.end = end;
Community
  • 1
  • 1
ecruz
  • 743
  • 1
  • 5
  • 17
  • It's really great! It would be even faster it the array is `filter`ed with the `view.start` and `view.end` – Thiru Dec 19 '13 at 12:00
  • This could also help, special case, but still shows how to find a solution: http://stackoverflow.com/a/29832834/1066234 – Avatar Apr 24 '15 at 06:18
  • This does not seem to work with the latest Fullcalendar, I used this instead: http://stackoverflow.com/a/15804896/1066234 – Avatar Dec 16 '15 at 13:10
  • Works great in the latest FullCalendar!! Thanks :) – olefrank Nov 17 '16 at 10:23
  • nice function, to update: fullCalendar array gives you dates as Date, so you don't have to Date(array[i].start), just with the value is good enough array[i].start – absolutkarlos Aug 05 '20 at 20:44
14

As of version 2.20 this change has been incorporated by default...

use

eventOverlap: false

https://fullcalendar.io/docs/eventOverlap

inN0Cent
  • 363
  • 2
  • 18
9

Same as ecruz's answer but with logic that worked better for me.

function isOverlapping(event){
    // "calendar" on line below should ref the element on which fc has been called 
    var array = calendar.fullCalendar('clientEvents');
    for(i in array){
        if (event.end >= array[i].start && event.start <= array[i].end){
           return true;
        }
    }
    return false;
}
Matthew Webb
  • 400
  • 3
  • 7
3

use this

  eventOverlap: false, // when load event
  selectOverlap: false,  // when create event from calender

for reference :

https://fullcalendar.io/docs/eventOverlap

https://fullcalendar.io/docs/selectOverlap

userSLK
  • 91
  • 5
2

Just add

eventOverlap: false

as one of your options outside of the events element.

You can also add the option

overlap

to a single event, which will override the eventOverlap on that single event.

events: [
                        {
                            title  : 'event1',
                            start  : '2017-05-27'
                        },
                        {
                            title  : 'event2',
                            start  : '2017-05-28',
                            end    : '2017-05-29'
                        },
                        {
                            title  : 'event3',
                            start  : '2017-05-30T12:30:00',
                            allDay : false, // will make the time show
                            draggable: true,
                            editable: true,
                            overlap: true,
                        },
                        {
                            title  : 'event3',
                            start  : '2017-05-30T09:30:00',
                            allDay : false, // will make the time show
                            draggable: true,
                            editable: true,
                        }
                    ],
                    eventOverlap: false
Community
  • 1
  • 1
PrestonDocks
  • 4,851
  • 9
  • 47
  • 82
1

Same as Matthew Webb but Following worked for me since sometimes my end date was null when i was dragging the event from allDay to some time slot

function isOverlapping(event) {
    var arrCalEvents = $('#' + g_str_FullCalenderID).fullCalendar('clientEvents');
    for (i in arrCalEvents) {
        if (arrCalEvents[i].id != event.id) {
            if ((event.end >= arrCalEvents[i].start && event.start <= arrCalEvents[i].end) || (event.end == null && (event.start >= arrCalEvents[i].start && event.start <= arrCalEvents[i].end))) {//!(Date(arrCalEvents[i].start) >= Date(event.end) || Date(arrCalEvents[i].end) <= Date(event.start))
                return true;
            }
        }
    }
    return false;
}
Mantra
  • 316
  • 3
  • 16
  • And how do I use this? I read ecruz details, I used it in eventDrop, but didn't worked. – machineaddict Jun 02 '14 at 12:42
  • @machineaddict: You could use it in eventDrop as well as in eventResize. It would be more easy to help if you post your code. – Mantra Jun 27 '14 at 09:56
  • this is my code http://pastebin.com/8jyQwpbH. As it is right now, is working very good, except I can have events that are overlapping. – machineaddict Jun 30 '14 at 07:07
  • I finally made it work, if someone might need it. I'm using Fullcalendar 2.0.0. [Here is the code](http://pastebin.com/DRXDwKNP). – machineaddict Oct 23 '14 at 10:30
0
    eventResize: function (event, dayDelta, minuteDelta, revertFunc, jsEvent, ui, view) {

        var start = new Date(event.start);
        var end = new Date(event.end);

        var events = event.source.events;
        for (var i = 0; i < events.length; i++) {
            var someEvent = events[i];

            if (someEvent._id == event._id)
            {
                continue;
            }

            var seStart = new Date(someEvent.start);
            var seEnd = new Date(someEvent.end);

            if ((start < seEnd) && (seStart < end)) {// dates overlap
                revertFunc();
            }
        }
    },
SuperUser
  • 41
  • 8
0

I'm using version 2.11 of Fullcalendar and i made some change to code posted by ecruz:

function isOverlapping(event){
   var array = calendar.fullCalendar('clientEvents');
   for(i in array){
       if(array[i]._id != event._id){
           if(!(array[i].start.format() >= event.end.format() || array[i].end.format() <= event.start.format())){
               return true;
           }
       }
    }
        return false;
}

and this is how i use to prevent the overlap:

    $('#calendar').fullCalendar({
        ...
        eventDrop: function(event, delta, revertFunc) {
                        if (isOverlapping(event)) {
                            revertFunc();
                        }
        },
        ...
    });
Beachhouse
  • 4,972
  • 3
  • 25
  • 39
Luís Ponciano
  • 157
  • 1
  • 5
0

just try this, works fine for me.... https://fullcalendar.io/docs/event_ui/eventOverlap/

eventOverlap: function(stillEvent, movingEvent) {
    return stillEvent.allDay && movingEvent.allDay;
    }
Leo Barbas
  • 109
  • 1
  • 4
-1

Please write like this.

$('#calendar').fullCalendar({
    selectOverlap: false,
    eventOverlap:false,
})

SelectOverlap prevent overlap when we create an event overlapping.

EventOverlap prevent overlap when we drag drop an event overlapping.

MilanBogic
  • 162
  • 3
  • 9
-2

Try this:

$('#calendar').fullCalendar({slotEventOverlap : false});

As per the documentation.

Jack Scott
  • 442
  • 4
  • 10
RedaZZ
  • 25
  • 4
-3

allowCalEventOverlap: [boolean | default: false] – Whether the calendar will allow events to overlap. Events will be moved or resized if necessary if they are dragged or resized to a location that overlaps another calendar event.

is that what you were looking for?

Eggie
  • 127
  • 1
  • 10
  • 3
    I think this is for jQuery Week Calendar ;) It doesn't work with jQuery FullCalendar. – user267637 Mar 03 '10 at 08:37
  • haha yeah just figured the same thing, sorry. maybe your answer is in here; http://code.google.com/p/fullcalendar/issues/detail?id=218 – Eggie Mar 03 '10 at 08:45