Managing multiple calendars on single page - javascript

I went through a couple of similar questions on stackoverflow. But, I couldn't get much help from them. Here is my problem :
I need to have multiple calendar on single page. But, currently only the first rendered calendar works properly. For example, if I have three calendars with different ids :
#calendarug1, #calendarug2, #calendarug3
Assume that order of declaring them in html page is
#calendarug3
#calendarug2
#calendarug1
Then only on #calendarug3 events like drag, drop and select work properly. On other calendars I am unable to create new events or drop new events.
Here is my code
HTML
<div class="container-fluid">
<div class="row">
<div class="col-lg-12 calendarbox">
<div class="row well calendar">
<h3 >UG3 Timetable</h3>
<div id='calendarug3'>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-lg-12 calendarbox">
<div class="row well calendar">
<h3 >UG2 Timetable</h3>
<div id='calendarug2'>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-lg-12 calendarbox">
<div class="row well calendar">
<h3 >UG1 Timetable</h3>
<div id='calendarug1'>
</div>
</div>
</div>
</div>
</div>
JS
$('#calendarug3').fullCalendar({
// put your options and callbacks here
header: { // Display nothing at the top
left: '',
center: '',
right: ''
},
theme: true,
//aspectRatio: 2.44,
//eventSources: ['events.php'],
height: 520, // Fix height
columnFormat: 'dddd', // Display just full length of weekday, without dates
defaultView: 'agendaWeek', // display week view
hiddenDays: [0,], // hide Saturday and Sunday
weekNumbers: false, // don't show week numbers
minTime: '8:30:00', // display from 16 to
maxTime: '19:00:00', // 23
slotDuration: '00:30:00', // 15 minutes for each row
allDaySlot: false, // don't show "all day" at the top
selectable: true,
editable: true,
defaultDate: '2016-10-10',
droppable: true, // this allows things to be dropped onto the calendar
selectHelper: true,
select: function(start, end, jsEvent) {
var title = prompt('Event Title:');
var eventData;
if (title) {
eventData = {
title: title,
start: start,
end: end
};
$('#calendarug3').fullCalendar('renderEvent', eventData, true); // stick? = true
}
$('#calendarug3').fullCalendar('unselect');
},
//select: function(start, end, allDay) {
// Code for creating new events.
// alert("Create new event at " + start);
//},
eventResize: function( event, delta, revertFunc, jsEvent, ui, view ) {
// Code when you resize an event (for example make it two hours longer
alert("I just got resized!");
},
eventDrop: function( event, jsEvent, ui, view ) {
// Code when you drop an element somewhere else
alert("I'm somewhere else now");
},
drop: function() {
// is the "remove after drop" checkbox checked?
console.log('calendar 3 viewRender');
if ($('#drop-remove3').is(':checked')) {
// if so, remove the element from the "Draggable Events" list
$(this).remove();
}
}
//defaultView : 'agendaSixDay'
});
////////////////////////////////////////////////////////////////
$('#calendarug2').fullCalendar({
// put your options and callbacks here
header: { // Display nothing at the top
left: '',
center: '',
right: ''
},
theme: true,
//aspectRatio: 2.44,
//eventSources: ['events.php'],
height: 520, // Fix height
columnFormat: 'dddd', // Display just full length of weekday, without dates
defaultView: 'agendaWeek', // display week view
hiddenDays: [0,], // hide Saturday and Sunday
weekNumbers: false, // don't show week numbers
minTime: '8:30:00', // display from 16 to
maxTime: '19:00:00', // 23
slotDuration: '00:30:00', // 15 minutes for each row
allDaySlot: false, // don't show "all day" at the top
selectable: true,
editable: true,
defaultDate: '2016-10-10',
droppable: true, // this allows things to be dropped onto the calendar
selectHelper: true,
select: function(start, end, jsEvent) {
var title = prompt('Event Title:');
var eventData;
if (title) {
eventData = {
title: title,
start: start,
end: end
};
$('#calendarug2').fullCalendar('renderEvent', eventData, true); // stick? = true
}
$('#calendarug2').fullCalendar('unselect');
},
//select: function(start, end, allDay) {
// Code for creating new events.
// alert("Create new event at " + start);
//},
eventResize: function( event, delta, revertFunc, jsEvent, ui, view ) {
// Code when you resize an event (for example make it two hours longer
alert("I just got resized!");
},
eventDrop: function( event, jsEvent, ui, view ) {
// Code when you drop an element somewhere else
alert("I'm somewhere else now");
},
drop: function() {
// is the "remove after drop" checkbox checked?
console.log('calendar 2 viewRender');
if ($('#drop-remove2').is(':checked')) {
// if so, remove the element from the "Draggable Events" list
$(this).remove();
}
}
//defaultView : 'agendaSixDay'
});
$('#calendarug1').fullCalendar({
// put your options and callbacks here
header: { // Display nothing at the top
left: '',
center: '',
right: ''
},
theme: true,
//aspectRatio: 2.44,
//eventSources: ['events.php'],
height: 520, // Fix height
columnFormat: 'dddd', // Display just full length of weekday, without dates
defaultView: 'agendaWeek', // display week view
hiddenDays: [0,], // hide Saturday and Sunday
weekNumbers: false, // don't show week numbers
minTime: '8:30:00', // display from 16 to
maxTime: '19:00:00', // 23
slotDuration: '00:30:00', // 15 minutes for each row
allDaySlot: false, // don't show "all day" at the top
selectable: true,
editable: true,
defaultDate: '2016-10-10',
droppable: true, // this allows things to be dropped onto the calendar
selectHelper: true,
select: function(start, end, jsEvent) {
var title = prompt('Event Title:');
var eventData;
if (title) {
eventData = {
title: title,
start: start,
end: end
};
$('#calendarug1').fullCalendar('renderEvent', eventData, true); // stick? = true
}
$('#calendarug1').fullCalendar('unselect');
},
//select: function(start, end, allDay) {
// Code for creating new events.
// alert("Create new event at " + start);
//},
eventResize: function( event, delta, revertFunc, jsEvent, ui, view ) {
// Code when you resize an event (for example make it two hours longer
alert("I just got resized!");
},
eventDrop: function( event, jsEvent, ui, view ) {
// Code when you drop an element somewhere else
alert("I'm somewhere else now");
},
drop: function() {
// is the "remove after drop" checkbox checked?
console.log('calendar 1 viewRender');
if ($('#drop-remove1').is(':checked')) {
// if so, remove the element from the "Draggable Events" list`enter code here`
$(this).remove();
}
}
//defaultView : 'agendaSixDay'
});
///////////////////////////////////////////////////////////////////////

Related

FullCalendar - drag between 2 calendars

I am trying to use this example -- https://fullcalendar.io/docs/other-calendar-dragging -- to create a page that shows 2 FullCalendar calendars and allows the user to drag events from one calendar to the other. I am able to render both calendars, and pull events for each via ajax from a php page. However, the events only display on the first calendar. If I comment out this line:
events: 'ajax/calendar.php?action=get_monthly_patrol_calendar'
from calendar1, then the events will display on calendar2 only. I'd really appreciate help to enable events to display on BOTH calendars.
FYI, here is my code:
HTML:
<div id="kt_calendar"></div>
<div id="kt_calendar2" ></div>
JS:
var todayDate = moment().startOf('day');
var show_now_indicator=true;
var YM = todayDate.format('YYYY-MM');
var YEAR = todayDate.format('YYYY');
var MONTH = todayDate.format('MM');
var YESTERDAY = todayDate.clone().subtract(1, 'day').format('YYYY-MM-DD');
var TODAY = todayDate.format('YYYY-MM-DD');
var TOMORROW = todayDate.clone().add(1, 'day').format('YYYY-MM-DD');
var NEXT_MONTH = todayDate.clone().add(1, 'month').format('YYYY-MM-DD');
var cal = document.getElementById('kt_calendar');
var calendar = new FullCalendar.Calendar(cal, {
plugins: [ 'bootstrap', 'interaction', 'dayGrid', 'dayGridPlugin', 'timeGrid', 'list' ],
themeSystem: 'bootstrap',
events: 'ajax/calendar.php?action=get_monthly_patrol_calendar',
showNonCurrentDates:false,
isRTL: KTUtil.isRTL(),
contentHeight: 'auto',
//aspectRatio: 3, // see: https://fullcalendar.io/docs/aspectRatio
nowIndicator: show_now_indicator,
now: TODAY,
defaultDate: TODAY,
initialDate: TODAY,
defaultView: 'dayGridMonth',
eventOrder: 'order_by',
eventLimit: false, // true to allow "more" link when too many events
navLinks: true,
eventResizableFromStart: false, //Whether the user can resize an event from its starting edge.
eventDurationEditable: false, //Allow events’ durations to be editable through resizing.
eventResourceEditable: reschedule,//Determines whether the user can drag events between resources.
droppable:reschedule,//Determines if external draggable elements or events from other calendars can be dropped onto the calendar.
eventStartEditable: reschedule,//Allow events’ start times to be editable through dragging.
editable: reschedule, //Determines whether the events on the calendar can be modified.
eventDrop: function(info) {
reschedule_event(info);
},
eventClick: function(info) {
do_event_click(info);
},
eventRender: function(info) {
var element = $(info.el);
},
header: {
left: 'prev,next today',
center: 'title',
right: 'dayGridMonth,dayGridWeek,dayGridDay'
},
views: {
dayGridMonth: { buttonText: 'monthly' },
dayGridWeek: { buttonText: 'weekly' },
dayGridDay: { buttonText: 'daily' }
},
});
calendar.render();
var cal2 = document.getElementById('kt_calendar2');
var calendar2 = new FullCalendar.Calendar(cal2, {
plugins: [ 'bootstrap', 'interaction', 'dayGrid', 'dayGridPlugin', 'timeGrid', 'list' ],
themeSystem: 'bootstrap',
events: 'ajax/calendar.php?action=get_monthly_patrol_calendar',
showNonCurrentDates:false,
isRTL: KTUtil.isRTL(),
contentHeight: 'auto',
//aspectRatio: 3, // see: https://fullcalendar.io/docs/aspectRatio
nowIndicator: show_now_indicator,
now: NEXT_MONTH,
defaultDate: NEXT_MONTH,
initialDate: NEXT_MONTH,
defaultView: 'dayGridMonth',
eventOrder: 'order_by',
eventLimit: false, // true to allow "more" link when too many events
navLinks: true,
eventResizableFromStart: false, //Whether the user can resize an event from its starting edge.
eventDurationEditable: false, //Allow events’ durations to be editable through resizing.
eventResourceEditable: reschedule,//Determines whether the user can drag events between resources.
droppable:reschedule,//Determines if external draggable elements or events from other calendars can be dropped onto the calendar.
eventStartEditable: reschedule,//Allow events’ start times to be editable through dragging.
editable: reschedule, //Determines whether the events on the calendar can be modified.
eventDrop: function(info) {
reschedule_event(info);
},
eventClick: function(info) {
do_event_click(info);
},
eventRender: function(info) {
var element = $(info.el);
},
header: {
left: 'prev,next today',
center: 'title',
right: 'dayGridMonth,dayGridWeek,dayGridDay'
},
views: {
dayGridMonth: { buttonText: 'monthly' },
dayGridWeek: { buttonText: 'weekly' },
dayGridDay: { buttonText: 'daily' }
},
});
calendar2.render();
Figured it out! The second call was failing bec it was running before the first call completed. I added a delay before loading the 2nd calendar, and now both months load fine:
setTimeout(function() { loadCal2(); }, 3000);

How to unselect one and multiple selected date in Fullcalendar?

I am using full calendar js in my code. I am able to select multiple dates in the calendar but I am not able to unselect the selected dates. Where do I need to changes in my code?
IN HTML FILE
<div class ="main-container fullcalendar-booking-course unselectCancel">
<div id="calendar"></div>
</div>
IN MY JS FILE
<script type="text/javascript">
$(document).ready(function() {
getFullCalendar();
</script>
<script type="text/javascript">
function getFullCalendar(){
$('#calendar').fullCalendar({
header: {
left: 'prev,next',
center: 'title',
right: 'month,agendaWeek'
},
views: {
month: {
titleFormat: 'YYYY, MM, DD'
}
},
validRange: function(nowDate) {
return {
start: nowDate,
end: nowDate.clone().add(1, 'months')
};
},
navLinks: true,
selectable: true,
selectHelper: true,
select: function(start, end) {
startDate = moment(new Date(start)).format("MM-DD-YYYY");
endDate = moment(new Date(end)).format("MM-DD-YYYY");
$("#calendar").fullCalendar('addEventSource', [{
start: start,
end: end,
rendering: 'background',
block: true,
}]);
$("#calendar").fullCalendar("unselect");
},
selectOverlap: function(event) {
return ! event.block;
},
editable: true,
eventLimit: true,
events: function(start, end, timezone, callback){
},
eventClick: function(event, jsEvent, view) {
},
loading: function(bool) {
$('#loading').toggle(bool);
},
eventRender: function(event, element){
},
eventAfterAllRender: function (view) {
var quantity = $('.fc-bgevent').length;
$("#quantity").val(quantity);
},
});
}
</script>
NOTE:
I am using fullcalendar js file and CSS file. I have initialized the function in the document ready function. I need to unselect the selected dates. I can select multiple dates but unable to unselect the dates.

Fullcalendar doesn't work

I like the fullcalendar JQuery-Plugin. My calendar doesn't work. At the moment i am looking for a solution to add an event but i can't... Why ?
This is my code:
$(document).ready(function() {
$('#calendar').fullCalendar({
defaultDate: '2017-05-11',
editable: true,
selectable: true,
selectHelper: true,
eventLimit: true, // allow "more" link when too many events
select: function(start, end, allDay) {
var title = prompt("Event here: ", "New Event:");
if (title != null) {
calendar.fullCalendar('renderEvent',
{
title: title,
start: start,
end: end,
allDay: allDay
},
true // make the event "stick"
);
}
$calendar.fullCalendar("unselect");
},
events: [
{
title: 'All Day Event',
start: '2017-05-01'
}
]
});
});
You need to define variable calendar like below
var calendar = $('#calendar').fullCalendar({
Then it will work.

only allow to save an event to a specific month in fullcalendar

i´ve got another question to fullcalendar.
I´want to allow the stuff to save events only to a fix month. The other months should be readonly. Is there a way to realize this?
This is my code:
$j('#calendar').fullCalendar({
header: {
left: 'prev,next today',
center: 'title',
right: 'month,agendaWeek,agendaDay'
},
defaultDate: '2017-05-12',
selectable: true,
selectOverlap: false,
weekends: false,
eventOverlap: false,
select: function(start, end) {
var title = prompt('Kostenstelle:');
var eventData;
id = Date.now();
if (title) {
eventData = {
id: id,
title: title,
start: start,
end: end
};
$j('#calendar').fullCalendar('renderEvent', eventData, true); // stick? = true
}
saveDay({'id': id,'title': title, 'start': start, 'end': end});
$j('#calendar').fullCalendar('unselect');
},
eventResize: function (event, dayDelta, minuteDelta) {
saveRange(event);
},
eventDrop: function (event, delta, revertFunc, jsEvent, ui, view ){
saveDrop(event, view);
},
eventReceive: function (event, dayDelta, minuteDelta){
saveDay(event);
},
eventRender: function(event, element) {
if(event.className != 'disableDelete'){ element.append( "<span class='closeon'>x</span>" );}
element.find(".closeon").click(function() {
$j('#calendar').fullCalendar('removeEvents',event._id);
deleteDay(event);
});
},
droppable: true, // this allows things to be dropped onto the calendar
drop: function() {
// is the "remove after drop" checkbox checked?
if ($j('#drop-remove').is(':checked')) {
// if so, remove the element from the "Draggable Events" list
$j(this).remove();
}
},
editable: true
});
The user should be able to view the other months but not be able to set events on it. So i want to set the other days readonly. Is it possible?
Thanks!
You can do this as follows
var monthStartDate = new Date('1/1/2017');
var monthEndDate = new Date('1/31/2017');
// monthStartDate and monthEndDate is month range in which you want to save event
select: function(start, end) {
if(start >= monthStartDate && start <= monthEndDate && end >= monthStartDate && end <= monthEndDate) {
var title = prompt('Kostenstelle:');
var eventData;
id = Date.now();
if (title) {
eventData = {
id: id,
title: title,
start: start,
end: end
};
$j('#calendar').fullCalendar('renderEvent', eventData, true); // stick? = true
}
saveDay({'id': id,'title': title, 'start': start, 'end': end});
$j('#calendar').fullCalendar('unselect');
}
},
This will not allow to save events between monthStartDate and monthEndDate.

Json data not displaying correctly

I have fullcalendar with some JSON data. It shows up as "12a Medical Day Jackson Post". Otherwise the display is fine. When I change the dates to be "start":"12/2/2015" to this 2015-2-12. The 12a appen disappears.
The Code for fullcalendar used is this
$(document).ready(function() {
$('#calendar').fullCalendar({
header: {
left: 'prev,next today',
center: 'title',
right: 'month,agendaWeek'
},
defaultDate: '2015-03-11',
defaultView: 'month',
selectable: false,
selectHelper: false,
select: function(start, end) {
var title = prompt('Event Title:');
var eventData;
if (title) {
eventData = {
title: title,
start: start,
end: end
};
$('#calendar').fullCalendar('renderEvent', eventData, true); // stick? = true
}
$('#calendar').fullCalendar('unselect');
},
editable: true,
eventLimit: true, // allow "more" link when too many events
eventMouseover: function(event, jsEvent, view) {
if (view.name !== 'agendaDay') {
$(jsEvent.target).attr('title', event.title);
}
},
events:[
{
"title":"Medical Day Jackson Post",
"start":"12/2/2015",
"end":"12/3/2015",
"description":"Jackson"
},
By default it shows the time unless the event is an allDay event. If allDay isn't specified, it guesses and in your case, it guessed differently depending on the date format.
So, either make allDay explicit in you events, or stop the time from displaying on all events with timeFormat: "", as one of you fullCalendar options.

Categories

Resources