Unable to load the fullcalendar_view page with ajax

Created on 18 June 2023, over 1 year ago

Problem/Motivation

I'm unable to load the fullcalendar_view page through ajax.

Steps to reproduce

I have created a views block using the fullcalendar style.
I want to load the calendar through ajax on another page on onclick event. But I'm getting the following error in the console.

Uncaught TypeError: Cannot read properties of undefined (reading '0')
    at HTMLDivElement.<anonymous> (fullcalendar_view.js?rwft16:286:57)
    at Function.each (jquery.min.js?v=3.6.0:2:3003)
    at S.fn.init.each (jquery.min.js?v=3.6.0:2:1481)
    at buildCalendars (fullcalendar_view.js?rwft16:282:6)
    at fullcalendar_view.js?rwft16:387:7

And it is this line.
let viewSettings = drupalSettings.fullCalendarView[viewIndex];
I think the process function of FullcalendarViewPreprocess is not executed when you try to load it through ajax.

I have created a route, its controller function is:

public function loadCalendar() {
    $view = Views::getView('event');
    $view->setDisplay('block_1');
    // contextual relationship filter
    $view->execute();
    $rendered = $view->render();
    $output = \Drupal::service('renderer')->render($rendered);
    $response['calendar'] = $output;

    return new JsonResponse($response);
  }

The anchor link is:
<a href="/events-calendar" title="View monthly calendar" class="popup-monthly">Monthly calenar</a>

and my JS code is:

$('a.popup-monthly').on('click', function (e) {
      e.preventDefault();
        
      $.ajax({
        url: Drupal.url('events-calendar'),
        type: 'POST',
        dataType: 'json',
        success: function (response) {
          console.log(response);
          if (response.hasOwnProperty('calendar')) {
            $('#event-calendar-modal').modal('show').find('.modal-body').html(response.calendar);
          }
        }
      });
    });

Am I doing something wrong?

🐛 Bug report
Status

Closed: duplicate

Version

5.1

Component

Code

Created by

🇵🇰Pakistan hmdnawaz

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.71.5 2024