Fullcalendar two events per day

旧巷老猫 提交于 2019-11-28 11:50:53

The eventReceive callback runs when an external event is dropped on the calendar and has been rendered. When this happens we can use the "clientEvents" method to check how many events are already present on the day the external event was dropped on, and decide on that basis whether to remove it from the calendar.

eventReceive: function(event) {
  var newEventDay = event.start.startOf('day');
  var existingEvents = $("#calendar").fullCalendar("clientEvents", function(evt) {
    //this callback will run once for each existing event on the current calendar view
    //if the event has the same start date as the new event, include it in the returned list (to be counted)
    if (evt.start.startOf('day').isSame(newEventDay)) {
      return true;
    } else {
      return false;
    }
  });

  //if this new event means there are now more than 2 events on that day, remove this new event again (N.B. we must do it like this because by this point the event has already been rendered on the calendar)
  if (existingEvents.length > 2) $("#calendar").fullCalendar("removeEvents", function(evt) {
    if (evt == event) return true;
  });
}

N.B. This assumes that the events you're dragging don't span more than one day. If they can, you'll need to alter this code a bit and bring the end date into the equation as well.

You can see a working example here: http://jsfiddle.net/Lfm1odm1/2/

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!