简体   繁体   English

根据用户偏好设置FullCalendar默认视图

[英]Set The FullCalendar Default View Based On User Preference

I'd like to add a button on the calendar that let's the user set the default view to whatever the current view is. 我想在日历上添加一个按钮,让用户将默认视图设置为当前视图。 I know I can set a default view ( defaultView: basicWeek for example), but then when you close and reopen the calendar, it resets itself. 我知道我可以设置一个默认视图(例如defaultView: basicWeek ),但是当您关闭并重新打开日历时,它将重置自身。 How can I get the current view of the calendar to save into an application variable for use next time they open the application? 如何获取日历的当前视图以保存到应用程序变量中,以供下次打开应用程序时使用?

Keep in mind this example may or may not work 100% (I can't test it on a web server and the document is sandboxed here in the snippet so it doesn't work). 请记住,此示例可能或不可能100%起作用(我无法在Web服务器上对其进行测试,并且该文档在此代码段中被沙箱化,因此它不起作用)。

Let me know if you have any errors. 让我知道您是否有任何错误。

 $(function() { var view = localStorage.getItem('calendarView') || 'month'; view = (view != undefined) ? view : 'basicWeek'; $('#calendar').fullCalendar({ 'viewRender': function(view) { localStorage.setItem('calendarView', view.type); }, 'defaultView': view }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.15.1/moment.min.js"></script> <link href="//cdnjs.cloudflare.com/ajax/libs/fullcalendar/3.0.1/fullcalendar.min.css" rel="stylesheet" /> <script src="//cdnjs.cloudflare.com/ajax/libs/fullcalendar/3.0.1/fullcalendar.min.js"></script> <div id="calendar"> </div> 

You'll want to save the view that the user has set as default to local storage. 您需要将用户设置为默认视图的视图保存到本地存储。

In the same function you use to change the default view, add the line: 在用于更改默认视图的同一函数中,添加以下行:

localStorage.setItem("fc-custom-default", viewName);

where viewName is the string name of the default view (eg "basicWeek") 其中viewName是默认视图的字符串名称(例如“ basicWeek”)

Then, in customButtons: , when defining the click function callback for your default view button, check if the key exists in storage, and if it does, use that value as the viewName in the 'changeView' function that is called on click. 然后,在customButtons: ,为默认视图按钮定义click函数回调时,检查该键是否在存储中,如果存在,则将该值用作单击时调用的“ changeView”函数中的viewName。 Otherwise, just use the standard default. 否则,只需使用标准默认值即可。 This will look something like this: 看起来像这样:

$("#calendar")
    .fullCalendar({
        customButtons: {
            defaultButton:{
                label: "Default View",
                click: function(){
                         var viewName = "month" //this is the default if nothing has been set
                         if(localStorage.getItem("fc-custom-default") !== null))
                         { 
                            viewName = localStorage("fc-custom-default");
                         }
                         $("#calendar").fullCalendar( 'changeView', viewName);
                     }
                 },

You can also store the current default in a variable outside of the click function so you aren't calling a function to retrieve the value from storage on every click. 您还可以将当前默认值存储在click函数外部的变量中,这样就不会在每次单击时都调用函数来从存储中检索值。

I found the best approach to be using viewRender. 我发现最好的方法是使用viewRender。 It gets called every time a calendar view is changed. 每次更改日历视图时都会调用它。 Here's what I did: 这是我所做的:

$('#calendar').fullCalendar({
    header: {
        left: 'prev,next today',
        center: 'title',
        right: 'month,agendaWeek,agendaDay,listWeek'
    },
    viewRender: function(view) {
        localStorage.setItem('Default_FullCalendar_View', view.name);
    },

    eventClick: function(calEvent, jsEvent, view) {
        if(calEvent.event_type == "Task"){
            LightviewOpen("/portal/Task.asp", "Update", "Task_ID", calEvent.id, "Update", 1200, 700);
        } else {
            window.open("LeadSystem/Lead.asp?New_Window=True&Open_Lead_ID=" + calEvent.id);
        }
    },
    defaultView:localStorage.getItem("AI_Default_FullCalendar_View"),
    eventLimit: true, // allow "more" link when too many events
    navLinks: true,
    height: (window.innerHeight - $("footer").height() - $("#calendar").position().top)*.8,
    windowResize: function(view) {$('#calendar').height((window.innerHeight - $("footer").height()-$("#calendar").position().top)*.9);},
    eventSources:[
        {
            url:    'ajax.asp?serverside=PopulateCalendar&GetDownline=true',
            type:   'Post'
        }
    ]

})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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