0
votes

I'm trying to update info date event from modal Bootstrap in Fullcalendar version 4. My test:

  1. Read event json
  2. Click on event calendar, open modal, show info event and set value of a field with information to update
  3. When I click on green button "Salva" (Save), doSubmitEdit function is called. It should:
    • close modal;
    • receive text field edited;
    • update via ajax my db;
    • update calendar event if necessary

But when then button is clicked I have this error:

q TypeError: calendar.fullCalendar is not a function on this line: var event_obj_arr = calendar.fullCalendar('clientEvents', calendario_id);

(function ($) {

    'use strict';
    var calendarEl = document.getElementById('calendar');

    var calendar = new FullCalendar.Calendar(calendarEl, {
        locale: 'it',
        plugins: ['dayGrid', 'timeGrid', 'list', 'interaction', 'bootstrap'],
        themeSystem: 'bootstrap',
        header: {
            left: 'prev,next today',
            center: 'title',
            right: 'dayGridMonth,timeGridWeek,timeGridDay,listWeek'
        },
        navLinks: true, // can click day/week names to navigate views
        selectable: true,
        selectMirror: true,
        editable: true,
        eventLimit: true, // allow "more" link when too many events
        events: {
            url: '/_admin/vendor/fullcalendar/php/get-events.php',
            failure: function () {
                document.getElementById('script-warning').style.display = 'block';
            }
        },
        eventClick: function (info) {
            $('.card-body h4').html(info.event.start.toDateString());
            $('.card-body p').html(info.event.title + '<br>' + info.event.extendedProps.autista + ' / Info: ' + info.event.extendedProps.autista_description);
            $('#calendario_id').val(info.event.id);
            $('#btnModal').trigger('click');
        },
        loading: function (bool) {
            document.getElementById('loading').style.display =
                bool ? 'block' : 'none';
        }//,
    });

    calendar.render();

    $('#submitButtonEdit').on('click', function (e) {
        // We don't want this to act as a link so cancel the link action
        e.preventDefault();
        doSubmitEdit();
    });

    function doSubmitEdit() {

        // get event object
        var event_obj_arr = calendar.fullCalendar('clientEvents', calendario_id);
        var event_obj = event_obj_arr[0];

        // edit
        $("#createEventModalEdit").modal('hide');
        console.log($('#autista_description').val());
        console.log($('#calendario_id').val());

        // update event object properties
        event_obj.extendedProps.autista_description = $('#autista_description').val();

        // post to server
        $.ajax({
            url: '/_admin/vendor/fullcalendar/php/planning-aggiorna.asp',
            data: 'type=changetitle&title=' + title + '&calendario_id=' + calendario_id,
            type: 'POST',
            dataType: 'json',
            success: function (response) {
                if (response.status == 'success') {
                    // nothing to do here

                    // update calendar, you may put this line into success method
                    calendar.fullCalendar('updateEvent', event_obj);
                }
            },
            error: function (e) {
                alert('Error processing your request: ' + e.responseText);
            }
        });
    }
}).apply(this, [jQuery]);

Is it possibile to access fullcalendar class outside of it? Thank you.

1
calendar.fullCalendar('clientEvents' is the old syntax and method name from v3. As per the upgrade guide the replacement in v4 is getEvents, you'd write it as var event_obj_arr = calendar.getEvents());. You'll have a similar issue with calendar.fullCalendar('updateEvent' later on in your code. Study the upgrade guide and make sure you understand all the differences between v3 and v4!ADyson
code // get values from modal form var calendario_id = $('#calendario_id').val(); var calendario_descrizione = $('#calendario_descrizione').val(); // get event object by id var event = calendar.getEventById(calendario_id); var title = event.title; // update calendar in case of 'success' event.setProp('title', title + ' changed'); event.setExtendedProp('autista_description', calendario_descrizione);Roby72
Ok. What are you trying to tell me by showing that code? Is it the solution? Or a new problem? It's not clear. But code does not belong in the comments section anyway. If this is your solution, please add it (with an explanation) to the Answers section below. If it's a new problem, please use the "edit" button to change the main question above. Thanks.ADyson
Sorry, it's the solutions. I add it asap... thanksRoby72

1 Answers

1
votes

Here's the solution, thank to @Adison. We have elements retrieved from modal form, update of live calendar (where I appended "changed" text to event's title) and update of db.

    function doSubmitEdit() {
        // get values from modal form
        var calendario_id = $('#calendario_id').val();
        var calendario_descrizione = $('#calendario_descrizione').val();

        // get event object by id
        var event = calendar.getEventById(calendario_id);
        var title = event.title;

        // post to server and update db
        $.ajax({
            url: '/_admin/planning-aggiorna.asp',
            data: 'calendario_descrizione=' + encodeURIComponent(calendario_descrizione) + '&calendario_id=' + calendario_id,
            type: 'POST',
            dataType: 'text',
            success: function (response) {
                if (response == 'success') {
                    // update calendar
                    event.setProp('title', title + ' changed');
                    event.setExtendedProp('autista_description', calendario_descrizione);
                }
            },
            error: function (e) {
                alert('Error processing your request: ' + e.responseText);
            }
        });
    }