简体   繁体   中英

Fullcalendar 4: update info event from modal Bootstrap

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.

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

There're many way to make calendar accessible from outside. The simplest way is changing line 4th,

var calendar = new FullCalendar.Calendar(calendarEl, {

to

$.calendar = new FullCalendar.Calendar(calendarEl, {

This means making calendar variable to be property of JQuery.

hope this help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM