0

https://fullcalendar.io/docs/destroy

FullCalendar Code:

     document.addEventListener('DOMContentLoaded', function () {           
        var calendarEl = document.getElementById('calendar');
        var calendar = new FullCalendar.Calendar(calendarEl, {
            plugins: ['interaction', 'dayGrid', 'timeGrid'],                
            defaultDate: new Date(),
            navLinks: true,
            selectable: true,
            selectMirror: true,
            draggable: true,                
            editable: true,
            eventLimit: true, 
            events: [
                <?= $contents ?>
            ],                
        });

        calendar.render();
    });

FullCalendar v4.2 destroy is not working I tried the below,

 1. var calendarEl = document.getElementById('calendar');
    calendarE1.destroy();

 2. $("#calendar).destroy();

 3. calendarEl = document.getElementById('calendar');
    var calendar = new FullCalendar.Calendar(calendarEl, {
      plugins: ['interaction', 'dayGrid', 'timeGrid'],
    });
    calendar.destroy();

These things I tried but nothing helps!. Note: I am trying to destroy the calendar in ajax success.
My Objective is I need to destroy the old events and reinitialize by new events.

sridhar
  • 321
  • 1
  • 3
  • 18
  • fyi, in your example #1: `calendarEl` is not the same as `calendarE1` – brombeer Nov 25 '19 at 08:07
  • "My Objective is I need to destroy the old events and reinitialize by new events" ...in that case you do **not** need to destroy the entire calendar. You can just add and remove the events themselves. – ADyson Nov 25 '19 at 08:56

1 Answers1

3

Basically, you can initialize the main Calendar object out of DOMContentLoaded, and utilize it as global within that page like the following code:

     var calendar;

     document.addEventListener('DOMContentLoaded', function () {           
     var calendarEl = document.getElementById('calendar');
     calendar = new FullCalendar.Calendar(calendarEl, {
            plugins: ['interaction', 'dayGrid', 'timeGrid'],                
            defaultDate: new Date(),
            navLinks: true,
            selectable: true,
            selectMirror: true,
            draggable: true,                
            editable: true,
            eventLimit: true, 
            events: [
                <?= $contents ?>
            ],                
        });

        calendar.render();
    });

Now inside any other function you can call it to destroy as follow:

if (calendar) {
   calendar.destroy();
}

Another option is to use "refetchEvents" as stated in This link: how to refresh fullcalendar v4 after change events object using ajax

And this link may also help you : Destroy fullcalendar on bootstrap tabs click before new calendar is initialized

Khurram Ishaque
  • 778
  • 1
  • 9
  • 26