FullCalendar skips back to current date rather than staying on current month

天涯浪子 提交于 2021-02-08 08:58:22

问题


I have FullCalendar installed and working great, pulling in courses from my database.

You can view different courses based on clicking a button that submits the page again but passes different criteria.

The Issue is that on reloading of the page and the new content it skips back to the current date which is rather annoying when when you are looking at courses 3 months into the future!!

Does anybody know how to make the calendar go back to the page you where on after you have refreshed the page???

I have a feeling it might be something to do with getdate as I got the following code to work but can't seem to pass the result back through the URL and into the calendar setup.

$('#my-button').click(function() {
    var d = $('#calendar').fullCalendar('getDate');
    alert("The current date of the calendar is " + d);
});

回答1:


If you use jquery.cookie you can store the currently viewed date in a cookie for the page being viewed and use that value to set the defaultDate when the page reloads. Pass these in as options when you initialise your calendar:

defaultView: Cookies.get('fullCalendarCurrentView') || 'month',
defaultDate: Cookies.get('fullCalendarCurrentDate') || null,
viewRender: function(view) {
  Cookies.set('fullCalendarCurrentView', view.name, {path: ''});
  Cookies.set('fullCalendarCurrentDate', view.intervalStart.format(), {path: ''});
}

This code also saves the current view (e.g. month, day etc...)




回答2:


I used a combination of the two above. I set the localStorage value for the start date when creating, moving, or resizing an event as well as viewRender and then assigned that value to the defaultDate.

defaultDate: localStorage.getItem('Default_FullCalendar_Date'),    
viewRender: function(view) {
        localStorage.setItem('Default_FullCalendar_View', view.name);
        ...
    },

    select: function(start, due){
        localStorage.setItem('Default_FullCalendar_View', start);
        ...
    },

    eventDrop: function(event, delta, revertFunc, jsEvent, ui, view){
        localStorage.setItem('Default_FullCalendar_View', event._start._d);
        ...
    },


    eventResize: function(event, delta, revertFunc, jsEvent, ui, view){
        localStorage.setItem('Default_FullCalendar_View', event._start._d);
        ...
    }

Works like a charm.




回答3:


You can use gotoDate method:

var d = $('#calendar').fullCalendar('getDate');
$('#calencar').fullCalendar( 'gotoDate', d.getFullYear(), d.getMonth(), d.getDate() )


来源:https://stackoverflow.com/questions/15939893/fullcalendar-skips-back-to-current-date-rather-than-staying-on-current-month

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!