简体   繁体   中英

Fullcalendar get current view on click of month time week etc

Here whenever the events is trigger I want to know the current view of the calendar like if it is dayGridMonth , timeGridWeek , timeGridDay or listWeek .

I have an select element where on change I am trying to update events on calendar. But what is happening is, if I select list view and change select menu then the whole calendar is refresh to default view. I want to set the calendar to the same view it was before selecting select box.

    calendar_view_session: function(){
        var calendar = new FullCalendar.Calendar(calendarEl, {
        plugins: ['dayGrid', 'timeGrid', 'list', 'interaction'],
        header: {
            left: 'prev,next today',
            center: 'title',
            right: 'dayGridMonth,timeGridWeek,timeGridDay,listWeek'
        },

        defaultDate: moment().format('YYYY-MM-DD'),//'2019-04-12',
        navLinks: true, 
        defaultView: 'listWeek',
        eventSources: [{
                    events: function (info, successCallback, failureCallback) {
                        console.log(info);
                    },
                    ..........
            }
          ],
          ..........
  }
}



                <select name="select_meetingtype" id="select_meetingtype" class="form-control">
                    <option value="">All Sessions</option>
                    <option value="free_session">Free Sessions</option>
                    <option value="weekly_session">Classes</option>
                </select>

$('#select_meetingtype').on('change', function() {
  _this.calendar_view_session(this.value)
});

The console.log(info) is only printing dates.

在此处输入图片说明

To grab the current view, try this:

const views = {day: 'timeGridDay', week: 'timeGridWeek', month: 'timeGridMonth', list: 'listWeek'};
var currentView = 'listWeek';
$('#calendar_container').delegate('.fc-toolbar-chunk button', 'click', function(){
  currentView = views[$(this).text()];
  calendar_view_session();// re-initialize calendar with current selectedView
})

calendar_view_session: function(){
    var calendar = new FullCalendar.Calendar(calendarEl, {
        //
        //
        defaultView: currentView,// use global var instead of static value
        //
        //etc.

This will re-initialize your calendar with the current view instead of reverting to the default one. The delegation is needed since the whole calendar gets destroyed and re-created when updating events.

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