Tuesday, February 22, 2022

[FIXED] FullCalendar - Sending view via ajax

Issue

I am trying to integrate the fullCalendar plugin into a website. The problem I am experiencing is that I am trying to send the current view, wether it be month, agendaDay etc..., as a custom parameter via the json/$.ajax method.

$('#calendar').fullCalendar({
events: {
    url: 'feed.php',
    type: 'POST',
    data: {
        view: $('#calendar').fullCalendar('getView').name
    },
    error: function() {
        alert('there was an error while fetching events!');
    }
}
});

The above is returning any errors but the custom view variable is not being passed along with the normal values.

I have tried the following:

$('#calendar').fullCalendar({
    events: {
        url: 'feed.php?view=' + $('#calendar').fullCalendar('getView').name,
       .......

But this just add ?view=undefined to the ajax call. I know that $('#calendar').fullCalendar('getView').name works as it returns the correct value so it must be something with the line view: $('#calendar').fullCalendar('getView').name but I unsure of what.


Solution

It looks like you are trying to access the fullCalendar before it has been initialized. Try breaking it into separate steps:

$('#calendar').fullCalendar();
var viewName = $('#calendar').fullCalendar('getView').name;
$('#calendar').fullCalendar({
    events: {
        url: 'feed.php',
        type: 'POST',
        data: {
            view: viewName
        },
        error: function() {
            alert('there was an error while fetching events!');
        }
    }
});


Answered By - gilly3

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.