Reload FullCalendar V5 event after updating selection options
P粉195402292
P粉195402292 2024-03-27 11:51:32
0
1
539

I am using FullCalendar with a database that contains event information, besides the normal information in the database I have a column for doctors and another for city, so I am working my fullcalendar 5 using MVC php mysql. I made a lot of changes to the calendar and it's working.

I added two selections at the top of the calendar, outside the calendar, what I like is that when I change the selection (e.g. select a doctor) the calendar has a refetchEvents() and of course only shows the events from those doctors, When I leave it blank, choose to show all events, or if I select another doctor, show his only events.

At some point, my code works fine, just the calendar doesn't refresh, I mean calendar.refetchEvents(); does nothing.

These are my js codes:

sedegeneral.addEventListener("click", clicksede);
    function clicksede(){
    sedegeneral.addEventListener("change", clicksede2);
        function clicksede2(){
        const sedegeneral1 = document.getElementById('sedegeneral').value;
        const doctorseleccionado1 = document.getElementById('doctorseleccionado').value;
        //alert("Alert 1");
        //calendar.refetchEvents();
        //alert("Estamos a que el doc es: "+doctorseleccionado1+" Y la sede escogida es: "+sedegeneral1);
            const url = base_url + 'Home/listar';
            const http = new XMLHttpRequest();
            http.open("POST", url, true);
            http.send(new FormData(frmflt));
            http.onreadystatechange = function () {
                if (this.readyState == 4 && this.status == 200) {
                    console.log(this.responseText);
                    const res = JSON.parse(this.responseText);
                     Swal.fire(
                         'Avisos?',
                         res.msg,
                         res.tipo
                     )
                    if (res.estado) {
                        eventSource.remove()
                        //calendar.getEventSources()
                        calendar.addEventSource(res)
                        calendar.refetchEvents();
                        calendar.render();
                    }
                }
            }

        sedegeneral.removeEventListener("change", clicksede2);
        };
    };

These are my controller functions:

public function listar()
    {
        $doctor = $_POST['doctorselecionado'];
        //$doctor = '';
        //$doctor = 'guti';
        $sede = $_POST['sedegeneral'];
        //$sede =  '';
        //$sede =  'dentalsoftweb';
        //$sede =  'guti';

        $data = $this->model->getEventos($doctor,$sede);
        echo json_encode($data);
        die();
    }

These are my model functions:

public function getEventos($doctor,$sede)
    {
        $where_sql = ''; 
        //if(!empty($_GET['start']) && !empty($_GET['end'])){$where_sql .= " WHERE start BETWEEN '".$_GET['start']."' AND '".$_GET['end']."' ";}
        if(!empty($_GET['start']) && !empty($_GET['end'])){$where_sql .= " AND start BETWEEN '".$_GET['start']."' AND '".$_GET['end']."' ";}

        //$sql = "SELECT id,title, CONCAT(start,'T',startTime) as start, CONCAT(start,'T',endTime) as end, color, duracioncita, finalidad, resultado, observaciones, start as fechacita, startTime as horacita FROM evento $where_sql";
        $sql = "SELECT id,title, CONCAT(start,'T',startTime) as start, CONCAT(start,'T',endTime) as end, color, duracioncita, finalidad, resultado, observaciones, start as fechacita, startTime as horacita FROM evento WHERE lastuser LIKE '%$doctor%' AND lastusermodif LIKE '%$sede%' $where_sql";
        $array = array($doctor);
        return $this->selectAll($sql, $array);
    }

So when I try, I see that in my console it works fine (php ajax), so when I start the calendar it shows all the events and of course the selection option is blank and when I change the selection (e.g. .doctor ), shows me doctor events in the console, but does not reload new data in the calendar. So in the image as you can see in one selection it brings all the data and in the other selection (when I change the selection I select a doctor) it only brings me his events , so there are fewer events, but only if I can do it in the console, the calendar doesn't perform any refresh operations.

I added the code to the image, the code I wrote before (controller):

I added an image of the code I wrote earlier (JS):

Interestingly, if I ignore the selection and modify my code (controller) online, the calendar refreshes, so I think my problem is in my JS. I think it has something to do with:

eventSource.remove()
calendar.getEventSources()
calendar.addEventSource(res)
calendar.refetchEvents();

So, do you have any ideas or suggestions? I will be forever grateful. Thanks in advance! ! !

P粉195402292
P粉195402292

reply all(1)
P粉237125700

I fixed changing the JS code like this:

//sedegeneral.addEventListener("click", clicksede);
//function clicksede(){
sedegeneral.addEventListener("change", clicksede2);
    function clicksede2(){
    const sedegeneral1 = document.getElementById('sedegeneral').value;
    const doctorseleccionado1 = document.getElementById('doctorseleccionado').value;
        const url = base_url + 'Home/listar';
        const http = new XMLHttpRequest();
        http.open("POST", url, true);
        http.send(new FormData(frmflt));
        http.onreadystatechange = function () {
            if (this.readyState == 4 && this.status == 200) {
                console.log(this.responseText);
                const ressede = JSON.parse(this.responseText);
                 Swal.fire(
                     'Filtrando Sede '+sedegeneral1+' y el doctor '+doctorseleccionado1,
                     //ressede.msg,
                     //ressede.tipo
                 )
                    calendar.removeAllEvents();
                    calendar.addEventSource(ressede)
            }
        }
    sedegeneral.removeEventListener("change", clicksede2);
    };
//};
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template