javascript - Destroy fullcalendar on bootstrap tabs click before new calendar is initialized - Stack Overflow

I have two tabs from bootstrap. Fullcalendar is inside the second tab which is by default hidden. So I

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.

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.

Share Improve this question edited Apr 29, 2019 at 11:49 Santosh asked Apr 29, 2019 at 11:14 SantoshSantosh 3,8475 gold badges45 silver badges88 bronze badges 5
  • Could you provide some of the HTML as well? – Vasil Dininski Commented Apr 29, 2019 at 11:18
  • added html also. – Santosh Commented Apr 29, 2019 at 11:21
  • 1 Would it not be better to create the calendar once outside the tab session code? Then you don't have to destroy and re-create it every time the tab is shown, that seems inefficient. You might need to re-render it each time potentially, but that's less costly than starting from scratch – ADyson Commented Apr 29, 2019 at 12:07
  • That's a good idea :) let me try... That tabs need to be visible first. – Santosh Commented Apr 29, 2019 at 12:21
  • It does work perfectly. I just simply made the calendar tab visible and after initializing of calendar I simply show the first div. – Santosh Commented Apr 29, 2019 at 12:36
Add a ment  | 

2 Answers 2

Reset to default 5

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();
},

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745375755a4624990.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信