Reputation: 3837
I have two tabs from bootstrap. Fullcalendar is inside the second tab which is by default hidden. So I use the onclick event to trigger the calendar_view_session
function. But every time I visit that tab the new calendar appends in the same tabs. Its actually duplicating the calendar.
calendar_view_session: function(){
var calendarEl = document.getElementById('calendar');
var calendar = new FullCalendar.Calendar(calendarEl, {
plugins: [ 'dayGrid', 'timeGrid', 'list', 'interaction' , 'momentTimezone' ],
header: {
left: 'prev,next today',
center: 'title',
right: 'dayGridMonth,timeGridWeek,timeGridDay,listWeek'
},
defaultDate: '2019-04-12',
navLinks: true, // can click day/week names to navigate views
editable: false,
eventLimit: true, // allow "more" link when too many events
events: function(info, successCallback,failureCallback) {
console.log(info);
var date1 = new Date(info.start.valueOf());
var date2 = new Date(info.end.valueOf());
var callData = {
from_date:moment.tz(date1, timeZone).format('YYYY-MM-DD'),
to_date: moment.tz(date2, timeZone).format('YYYY-MM-DD')
}
sessionCalendar = [];
console.log(callData);
$.ajax({
url:app.createApiPath(apiController_data,apiAction_data),
method:"POST",
dataType: "json",
data: {
callData: JSON.parse(JSON.stringify(callData)),
api_version: 1,
deviceType: 'web'
},
success: function(responseData){
console.log({responseData})
successCallback(app.calendarParse(responseData.data.results));
}
})
},
//sessionCalendar
});
calendar.render();
},
My HTML code
<div>
<!-- Nav tabs -->
<ul class="nav nav-tabs" role="tablist">
<li role="presentation" class="active"><a href="#list_view_session" aria-controls="list_view_session" role="tab" data-toggle="tab">List View</a></li>
<li role="presentation"><a href="#calendar_view_session" id="calendar_view_session_id" onclick="app.calendar_view_session()" aria-controls="calendar_view_session" role="tab" data-toggle="tab">Calendar View</a></li>
</ul>
<!-- Tab panes -->
<div class="tab-content">
<div role="tabpanel" class="tab-pane active" id="list_view_session">
<div class="session_list">
<div class="loaderapi hide">
<i class="fa fa-spinner fa-spin fa-4x" aria-hidden="true"></i>
</div>
</div>
</div>
<div role="tabpanel" class="tab-pane" id="calendar_view_session">
<div id='calendar'></div>
</div>
</div>
</div>
This is my function. However, I use $("#calendar").destroy()
function to destroy the calendar first. It shows error saying
sessions.js:253 Uncaught TypeError: $(...).destroy is not a function
I use this destroy function after
var calendarEl = document.getElementById('calendar');
line.
Upvotes: 1
Views: 3216
Reputation: 7004
The error is clear, the method destroy doesn't exist into fullcalendar, instead, you do:
$('#calendar').fullCalendar( 'destroy' );
Here's a fiddle
Update:
Before you instantiate fullcalendar, you probably need to empty the container element everytime you click your tab.
$('#calendar').html('');
In your case you do:
calendar_view_session: function () {
var calendarEl = document.getElementById('calendar');
$('#calendar').html('');
// your long code goes here...
calendar.render();
}
NB: You may need timeout to render your calendar.
Final update
// very global, outside of your context:
var calendar = null;
calendar_view_session: function () {
var calendarEl = document.getElementById('calendar');
if (calendar) {
calendar.destroy();
}
calendar = new FullCalendar.Calendar(calendarEl, { ... });
calendar.render();
},
Upvotes: 5
Reputation: 484
You should run the destroy method on calendar
rathern then on the element in the DOM.
Try to use window.calendar.destroy()
instead.
Have a look at the docs here: https://fullcalendar.io/docs/destroy
Upvotes: 2