How to limit events per time slot in fullcalendar?

2020-07-27 02:49发布

问题:

Is it possible to limit FullCalendar-2 to add events only in slot interval and limit it to 2?

For example, users can add event only between 12:00:00 - 12:15:00, it is not possible to add longer event (12:00:00 - 12:30:00) and he can add it only if there is a 'free' slot. Orage events are correct (max 2 events in time slot), events blue are wrong (over 2 events)

Here's an example

$(document).ready(function() {

    // page is now ready, initialize the calendar...

    $('#calendar').fullCalendar({
                    header: {
                            left: 'prev,next today',
                            center: 'title',
                            right: 'agendaDay'
                    },
                    defaultView: 'agendaDay',
                    allDaySlot: false,
                    scrollTime: moment().format('HH:mm:ss'),
                    slotDuration: '00:15:00',
                    minTime: '06:45:00',
                    maxTime: '23:15:00',
                editable: true,
            events: [
                {
                    title: 'Meeting',
                    start: '2014-06-06T10:30:00',
                    end: '2014-06-06T10:45:00'
                },
                {
                    title: 'Lunch',
                    start: '2014-06-06T10:30:00',
                    end: '2014-06-06T10:45:00'
                },
                {
                    title: 'Birthday Party',
                    start: '2014-06-06T10:30:00',
                    end: '2014-06-06T10:45:00'
                },
                {
                    title: 'Meeting',
                    start: '2014-06-06T15:30:00',
                    end: '2014-06-06T15:45:00',
                    color: '#FF7F50'
                },
                {
                    title: 'Lunch',
                    start: '2014-06-06T15:30:00',
                    end: '2014-06-06T15:45:00',
                    color: '#FF7F50'
                }
            ]

            });

});

and live: http://jsbin.com/fezidowo/3/

回答1:

select: function(start, end) {
    console.log( 'Events :' + getEventsByTime( start, end ).length );
    var ev = getEventsByTime( start, end );
    console.log( ev );
    var itms = {};            

    ev.forEach(function(entry){

      var begin = moment(entry.start);
      var final = moment(entry.end);


    while( begin.diff(final) < 0 ) {
        itms[begin] =  ( itms[begin] || 0) + 1;
        if( itms[begin] >= maxEventsInInterval ) {
            console.log(' __ WARNING __ , max events exceeded! ');
        }

        begin = moment(begin).add('seconds', 900);
    }

});
    console.log( itms );
}

function getEventsByTime( start, stop ) {
    var todaysEvents = $('div[name=calendar]').fullCalendar('clientEvents', function(event) {

       return ( 
           ( event.start >= start && event.end <= stop ) || 
           ( start >= event.start && stop <= event.end)  || 
           (start <= event.start && stop >= event.start) ||
           (start >= event.start && start <= event.end)
       );
    });
    return todaysEvents;
}