Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add more events to fullcalendar when dragging starts

I need to be able to let users move some events around on my fullcalendar. And I want to go to server and fetch counterparties' busy times and display them as background events on the calendar when the dragging is going on. However, when I call .fullcalendar('renderEvents', [...]) while the dragging is happening, the dragging mechanism within fullcalendar breaks and stops.

Is there a way to either add events without breaking dragging or some other way highlight busy times that have to be retrieved with ajax call?

The example is here: https://jsbin.com/qikiganelu/1/edit?js,output. Try dragging 'Meeting with Bob'.

$(function() { // document ready
  
  $('#calendar').fullCalendar({
    header: {
      left: '',
      center: '',
      right: ''
    },
    defaultView: 'agenda',
    duration: {days: 2},
    allDaySlot: false,
    defaultDate: '2017-04-27',
    minTime: '9:00',
    maxTime: '17:00',
    events: [
      {
        title: 'Keynote',
        start: '2017-04-27T09:00',
        end: '2017-04-27T11:00',
        editable: false,
        color: "gray",
      },
      {
        title: 'Meeting with Bob',
        start: '2017-04-27T12:30',
        end: '2017-04-27T13:00',
        editable: true,
      },
    ],
    eventDragStart: function(event, jsEvent, ui, view){
      // fetch Bob's busy times and add them as background events
      var busyTimes = [
        {
          start: '2017-04-27T14:00',
          end: '2017-04-27T16:00',
          rendering: 'background',
        },
        {
          start: '2017-04-28T9:00',
          end: '2017-04-28T12:00',
          rendering: 'background',
        }
      ];
      $('#calendar').fullCalendar('renderEvents', busyTimes);
    },
  });

});
body {
  margin: 40px 10px;
  padding: 0;
  font-family: "Lucida Grande",Helvetica,Arial,Verdana,sans-serif;
  font-size: 14px;
}

#calendar {
  max-width: 900px;
  margin: 0 auto;
}
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8' />
<link href='https://fullcalendar.io/js/fullcalendar-3.4.0/fullcalendar.css' rel='stylesheet' />
<link href='https://fullcalendar.io/js/fullcalendar-3.4.0/fullcalendar.print.css' rel='stylesheet' media='print' />
<script src='//cdnjs.cloudflare.com/ajax/libs/moment.js/2.9.0/moment.min.js'></script>
<script src='//ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>
<script src='https://fullcalendar.io/js/fullcalendar-3.4.0/fullcalendar.js'></script>

<!-- the code from the JavaScript tab will go here -->


<!-- the code from the CSS tab will go here -->

</head>
<body>

	<div id='calendar'></div>

</body>
</html>
like image 528
repka Avatar asked Apr 27 '17 20:04

repka


People also ask

How do I set events in fullCalendar?

Here is an example of how to specify an array of events: var calendar = new Calendar(calendarEl, { events: [ { title : 'event1', start : '2010-01-01' }, { title : 'event2', start : '2010-01-05', end : '2010-01-07' }, { title : 'event3', start : '2010-01-09T12:30:00', allDay : false // will make the time show } ] });

How do I select multiple dates in fullCalendar?

Detect when the user clicks on dates or times. Give the user the ability to select multiple dates or time slots with their mouse or touch device. Allows a user to highlight multiple days or timeslots by clicking and dragging.

How do I resize fullCalendar?

ready(function() { $(window). resize(function() { $('#calendar'). fullCalendar('option', 'height', get_calendar_height()); }); //set fullcalendar height property $('#calendar'). fullCalendar({ //options height: get_calendar_height }); });

How do I start and end date in fullCalendar?

We can get start date by getView event with intervalStart. We can get end date by getView event with intervalEnd. var month = $('#calendar'). fullCalendar('getView').


1 Answers

As a workaround, try preloading all the background events, but hiding them via CSS until the drag operation happens. For instance:

// hide all background events by default
.fc-bgevent {
  display: none;
}

// redisplay once "show-background-events" class toggled on
.fc-bgevent.show-background-events {
  display: block;
}

Show / hide the background events by toggling the .show-background-events class like so:

eventDragStart: function(event, jsEvent, ui, view){
  $(".fc-bgevent").addClass("show-background-events");
},
eventDragStop: function(event, jsEvent, ui, view){
  $(".fc-bgevent").removeClass("show-background-events");
}

Example at: https://jsbin.com/vuvacisibu/1/edit?css,js,output

like image 90
Conan Avatar answered Oct 12 '22 22:10

Conan