Controlling the Number of Events in a Time Slot

In many use cases you may need to limit the count of events per time slot. For example, you may need to deny creation of the 2nd event if some other event has already been defined at that time.

Activating the monitoring for collisions

To control the number of events in a time slot, use the 'collision' extension.

[1] Activating the 'collision' extension

<script src="../codebase/ext/dhtmlxscheduler_collision.js"></script>

Once you include the extension file on the page, the extension will be activated. From this moment on, the scheduler won't allow users to place 2 events in the same time slot (create or move).

Managing the allowable number of events in a time slot

By default, the allowable number of events in a time slot is 1. To regulate this number, use the collision_limit property:

[2] Denying creating more than 2 events per time slot

scheduler.config.collision_limit = 2;      //allows creating 2 events per time slot

Related sample:  Controlling the number of events in a time slot

With the 'collision' extension enabled, each time the user tries to create a new event or modify some existing one inside an already occupied time slot, the scheduler invokes the onEventCollision event which checks the value set with the collision_limit property.


But remember, the onEventCollision event isn't triggered while loading data. So, to control a number of items in a time slot while data is being loaded to the scheduler, you need to extend the previous code a bit:

[3] Denying creating/loading more than 2 events per time slot

scheduler.config.collision_limit = 2; //allows creating 2 events per time slot
scheduler.attachEvent("onEventLoading", function(ev){     return scheduler.checkCollision(ev);             });

The checkCollision method checks whether an event occurs at the time that has already been occupied by another event(s) and invokes the onEventCollision event.

Getting the number of events resided in a time slot

To get the number of events resided in a time slot, use the getEvents method:

[4] Getting the number of events in a time slot

var count = scheduler.getEvents(ev.start_date, ev.end_date).length;

Note, the getEvents method iterates over all events and compares their dates, so it may take a bit of time if you are using thousands of events.

Back to top