How to render events in Fullcalendar by specific views?

Asked

Viewed 223 times

0

Imagine the following situation: I have 2 arrays and 2 views in my fullCalendar.

In the "list" view I need to render the first array of events, that reflects the select in a view in the database. These events come blank, and when filled in a modal are registered and stop at another table, that in turn I make a select and store the result in another array, which will be rendered in the "Month view"

my code looks like this

//array de teste que vem de uma função do php
            <?php
        $dadosDaTabela = \App\Models\AgendasMedicas::GetAllofThisDataAgenda($doctorVal);
        foreach ($dadosDaTabela as $value) {
            ?> {
//o conteudo aqui nao interessa, o importante pra mim é ver se alguma coisa é retornada
                            id: '<?php echo $value['DT_CONSULTA_AGENDADA']; ?>',
                            title: '<?php echo $value['DT_CONSULTA_AGENDADA']; ?>',
                            color: 'red',
                            start: '<?php echo $value['DT_CONSULTA_AGENDADA']; ?>',

                        },
        <?php }
        ?>
                ];

//no meu full calendar, tento isto: 
 $('#calendar').fullCalendar({
                    viewRender: function (view, element) {
                        if (view.name == 'list') {
//aqui meu array é renderizado, o problema é que ele se renderiza nas outras views também...
                            events: events_array
                        }
                        if (view.name == 'month') {
                            alert('month')
                        }
                        if (view.name == 'agendaWeek') {
                            alert('agendaWeek')
                        }
                    },
                    timezone: 'America/Sao_Paulo',
                    header: {
                        left: ' prev title next ',
                        right: 'list,agendaWeek,month'
                    },
                    buttonText: {
                        list: 'Hoje',
                        listWeek: 'Semana'
                    }
                });
  • Checks whether the attribute id of the elements is not the same, maybe posting the HTML can help solve the problem.

  • Fullcalendar is mounted only through a div with the id "Calendar"... there is no more than that in my HTML.

  • The id of the elements cannot be the same, Leonardo, because I assign them date and time, so it is impossible to be equal.

  • So you only have one calendar and want to change events according to the selected view?

  • Exactly, I have 2 javascript arrays that are fed with php foreachs, and I would like in one view my event array 1 to be loaded, and in another view, array 2 to be loaded

2 answers

1

After knocking my head and reading the documentation a lot, I finally got it. I’m using version 3 of fullCalendar, the final code was this way:

//meus arrays (virtual e scheduled)
                    var virtual_array = [

        <?php
        $virtualArrays = \App\Models\AgendasMedicas::GetAllofThisDataAgenda($doctorVal);
        foreach ($virtualArrays as $virtualArray) {
            ?> {
                            id: '<?php echo $virtualArray['DT_CONSULTA_AGENDADA']; ?>',
                            title: '<?php echo $virtualArray['DT_CONSULTA_AGENDADA']; ?>',
                            color: 'red',
                            start: '<?php echo $virtualArray['DT_CONSULTA_AGENDADA']; ?>',

                   },
        <?php }
        ?>
                ];

                var scheduled_array = [

        <?php
        $scheduledArrays = \App\Models\AgendasMedicas::GetAllofThisDataAgendada();
        foreach ($scheduledArrays as $scheduledArray) {
            ?> {
                            id: '<?php echo $scheduledArray['DT_CONSULTA_AGENDADA']; ?>',
                            title: '<?php echo $scheduledArray['DT_CONSULTA_AGENDADA']; ?>',
                            color: 'red',
                            start: '<?php echo $scheduledArray['DT_CONSULTA_AGENDADA']; ?>',

                        },
        <?php }
        ?>
                ];

// e o meu fullCalendar ficou da seguinte forma:

          $('#calendar').fullCalendar({
                    viewRender: function(view, element){

                        if(view.name == 'list'){
                          $('#calendar').fullCalendar('removeEvents');
                          $('#calendar').fullCalendar('addEventSource', virtual_array);
                          $('#calendar').fullCalendar('rerenderEvents');
                        }
                        if(view.name == 'month'){
                             $('#calendar').fullCalendar('removeEvents');
                             $('#calendar').fullCalendar('addEventSource', scheduled_array);
                             $('#calendar').fullCalendar('rerenderEvents');
                        }
                        if(view.name == 'agendaWeek'){
                            $('#calendar').fullCalendar('removeEvents');
                            $('#calendar').fullCalendar('addEventSource', scheduled_array);
                            $('#calendar').fullCalendar('rerenderEvents');
                        }
                    },
//restante do calendario........

0

To change the events you must remove the existing ones then insert the new ones

I use $('#calendar').fullCalendar('removeEvents'); to remove events and update them.

I set an example with your code

viewRender: function (view, element) {
                    if (view.name == 'list') {
                        $('#calendar').fullCalendar('removeEvents');
                        events: events_array
                    }
                    if (view.name == 'month') {
                        $('#calendar').fullCalendar('removeEvents');
                        events: novo_events_array
                    }
                    if (view.name == 'agendaWeek') {
                        $('#calendar').fullCalendar('removeEvents');
                        events: outro_events_array
                    }
                },

So each time the view is changed, the new data will be loaded.

  • Wouldn’t I need an update? I tested it here and it didn’t actually run when my second array is built - without calling it in fullCalendar, the Highlight syntax already breaks, it’s like it doesn’t recognize the existence of the second array. In the console it accuses syntax error, but everything is correct and I already checked a million times, maybe it is a bug on account of php, I do not know. But anyway, your solution seems reasonable to me, but it occurs that when I change from one view to another and go back to the previous one, everything disappears and it’s like this... do I need to update? and how to do this if you need to?

  • I update with ajax, after removing the events and make an ajax request and look for new information to popular the events. You can also separate the responsibility of creating the array from your view, passing to a controller for example and sending the mounted array to the view. Put the console error too, it might help to solve the problem. Maybe by iterating the events and using $('#calendar').fullCalendar( 'renderEvent', evento, true); can solve the problem

  • Are you using any framework?

  • I’ve already published the answer, Leonardo. Thanks!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.