fullCalendar finding addtional events in the same day?

醉酒当歌 提交于 2019-12-04 16:49:56
Tom

Well after probing around in the docs and a little experimenting, I arrived at a solution using the clientEvents method:

  eventDrop: function( event, dayDelta, minuteDelta, allDay, 
                       revertFunc, jsEvent, ui, view )
        {
            // see if its a concept class event
            if (event.className == 'conceptclass'){
                // create a new date object from the start of the event 
                var eventDate = new Date(event.start);
                // zero its time 
                eventDate.setHours(0);
                eventDate.setMinutes(0);
                eventDate.setSeconds(0);
                eventDate.setMilliseconds(0);

                // now find all the events currently displayed in the calendar
                var pulledEvents = $('#calendar').fullCalendar( 'clientEvents');
                var meetingDay = false; // until a meeting is found 

                for(var i = 0; i < pulledEvents.length; i++){
                    // if the pulled event is of the meeting class 
                    if(pulledEvents[i].className == 'meetingclass'){
                        // create a new date object from the start of the pulled event 
                        var testEventDate = new Date(pulledEvents[i].start);
                         // zero its time for comparison 
                        testEventDate.setHours(0);
                        testEventDate.setMinutes(0);
                        testEventDate.setSeconds(0);
                        testEventDate.setMilliseconds(0);

                        // if meeting event found in the day
                        // OK a little wierdness here, even though the dates were equal 
                        // they would not return a valid comparison.  So I get the time value.
                        if(testEventDate.getTime() == eventDate.getTime()){
                           meetingDay = true;
                           break;
                        }
                    }
            }
            if(!meetingDay){
                 alert("Tried to drop a Concept into Day without Scheduled Review Meeting!");
                 revertFunc(); // back to where it came!
            }       
      }

Another option is to solve this server side since you should be checking it's valid there too.

When the "concept" event is dragged onto the day, send the Ajax request to the server to update the "concept" event's date, if it's not valid, call revertFunc() to put the event back.

eventDrop: function(event, dayDelta, minuteDelta, allDay, 
                    revertFunc, jsEvent, ui, view) {
  $.post("/concep_update/", { id: event.id, delta: dayDelta },
         function(data) {
           if(data!="true") {
             revertFunc();
           }
         }, "text"});
}

On the server, check that there is a "meeting" event on the same day, and if so, update the "concept" event and return "true", otherwise, return "false".

@require_POST @login_required
def concept_update(request):
    concept = get_object_or_404(Concept.objects.all(), 
                              id=int(request.POST.get("id","0")))
    concept.date = concept.date + timedelta(days=int(request.POST.get("delta","0")))
    meetings = Meeting.objects.filter(date=concept.date)
    if meetings.count() > 0:
        concept.save()
        return HttpResponse("true", status=200, content_type="text/plain")
    else:
        return HttpResponse("false", status=200, content_type="text/plain")
    var e1start = new Date(startDate);
    var e1end = endDate == null ? e1start : new Date(endDate);

        for (var d = e1start; d <= e1end; d.setDate(d.getDate() + 1)) {
           var eventsInDay = getEventsInDay(d);
           // ...
        }


   function getEventsInDay(date){
        return $('#fr-calendar').fullCalendar('clientEvents', function(event){
            if(date >= event.start && date <= event.end){
                return true;
            }
        });
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!