Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FullCalendar, events are not refreshed

Here is my jquery code :

<script type="text/javascript">
$('#refresh').on('click', function(){
    var json_events;
    $.ajax({
        url: 'ajaxRefresh.php',
        type: 'POST',
        data: 'type=fetch',
        success: function(response){
            json_events = response;
            console.log(response);
        }
    });
    $('#calendar').fullCalendar({
        events: json_events
    });
})
</script>

All is OK in fact, except thaat the calendar is not updated when I click on the button.

I did a console.log in order to see if my JSON returned by ajax was OK. And I got :

[{"id":"10","title":"Rugby","start":"2017-05-16T00:01:00+05:30","end":"2017-05-19T00:01:00+05:30","allDay":false}]

Thanks in advance for your precious help.

PS : I included 'fr.js' because I need my calendar to be in french.

like image 804
Kuartz Avatar asked May 11 '17 16:05

Kuartz


People also ask

How do I refresh fullCalendar?

Here are the events: //remove old data $('#fullCalendar'). fullCalendar('removeEvents'); //Getting new event json data $("#fullCalendar"). fullCalendar('addEventSource', response); //Updating new events $('#fullCalendar'). fullCalendar('rerenderEvents'); //getting latest Events $('#fullCalendar').

How do I set events in fullCalendar?

Here is an example of how to specify an array of events: var calendar = new Calendar(calendarEl, { events: [ { title : 'event1', start : '2010-01-01' }, { title : 'event2', start : '2010-01-05', end : '2010-01-07' }, { title : 'event3', start : '2010-01-09T12:30:00', allDay : false // will make the time show } ] });

What is fullCalendar?

FullCalendar is a JavaScript library that seamlessly integrates with such popular JavaScript frameworks as Vue, React, Angular. Thanks to its excellent documentation, one won't have trouble incorporating the library into projects.

What is fullCalendar eventRender?

eventRender is a great way to attach effects to event elements, such as a Tooltip. js tooltip effect: var calendar = new Calendar(calendarEl, { events: [ { title: 'My Event', start: '2010-01-01', description: 'This is a cool event' } // more events here ], eventRender: function(info) { var tooltip = new Tooltip(info.


1 Answers

You've got your approach the wrong way round really. This should work better:

$('#calendar').fullCalendar({
    events: function( start, end, timezone, callback ) { 
      $.ajax({
        url: 'ajaxRefresh.php',
        type: 'POST',
        data: 'type=fetch',
        success: function(response){
            console.log(response);
            callback(response); //sends the events to fullCalendar
        }
      });
    }
});

$('#refresh').on('click', function(){
  $("#calendar").refetchEvents();
});

This defines an event source for the calendar using the built-in architecture of fullCalendar. It will automatically re-runs this whenever the calendar view and/or date range is changed. Note though that really your PHP should be accepted the "start" and "end" parameters and only returning data for the dates that are currently being displayed on the calendar. That way you don't download more data than you actually need, which will improve performance, especially if you app runs for many years and collects lots of data, most of which users won't view any more after a certain point.

And then, when you click "refresh", it runs the "refetchEvents" method manually, which just causes that same function you supplied to the "events" parameter again as an extra call.

See https://fullcalendar.io/docs/event_data/events_function/ and https://fullcalendar.io/docs/event_data/refetchEvents/ for more details.

It might also be worth looking at https://fullcalendar.io/docs/event_data/events_json_feed/ - if you can make your PHP page comply with the structure required by this, you can simplify things and remove the "ajax" call entirely from the client-side code, and just write `events: "ajaxRefresh.php" as the events property in fullCalendar.

like image 94
ADyson Avatar answered Oct 07 '22 13:10

ADyson