简体   繁体   English

在FullCalendar中调用Ajax后的着色假期

[英]Coloring holidays after an Ajax call in FullCalendar

I'm working with http://fullcalendar.io/ . 我正在使用http://fullcalendar.io/ My purpose is to give different background color to holidays. 我的目的是为假期提供不同的背景颜色。 After a lot of searches I can say that a good solution is using dayRender hook. 经过大量搜索,我可以说一个好的解决方案是使用dayRender钩子。 But how to fetch holidays list, and what about timing? 但是如何获取假期列表,以及时序呢?

Solution 1: an Ajax call for each day. 解决方案1:每天的Ajax调用。

dayRender: function (date, cell) {
    $.ajax({
        url: '/myproject/holidays?date='+date,
        type: "POST",
        success: function(isHoliday) {
            if (isHoliday) {
                cell.css("background-color", "red");
            }
        }
    });
}

It should work but... an Ajax call for EACH days? 它应该工作但是...每天都有一个Ajax调用? It seems not a nice solution.. 这似乎不是一个好的解决方案..

Solution 2: one ajax call to fetch all the holidays 解决方案2:一次ajax调用以获取所有假期

dayRender: function (date, cell) {
    if ($.inArray(date, holidayList)) {
        cell.css("background-color", "red");
    }
}

where "holidayList" was fetched somewhere before. 其中“holidayList”之前被提取过的地方。 That's my problem. 那是我的问题。 Before when? 在什么时候? How can I be sure that holidayList has been completed fetched before each dayRender is called? 如何在每天调用dayRender之前确定是否已完成holidayList? In other words: is there a hook I can rely on to fetch the holidays list? 换句话说: 是否有一个我可以依赖的钩子来获取假期列表?

Thank you 谢谢

Two options I see here: 我在这里看到两个选项:

  1. Initialize the whole calendar only after the holiday fetching Ajax request is done (and meanwhile display a loader/spinner). 仅在假期提取Ajax请求完成后初始化整个日历(同时显示加载器/微调器)。 Simple, but not "pretty". 简单但不“漂亮”。

  2. As soon as the holiday fetching Ajax request is done, refresh the whole calendar view. 完成假期提取Ajax请求后,刷新整个日历视图。 As the internal reinitView method is sadly not being exported to be called externally, you have to do it in a few more steps: 由于遗憾地没有将内部reinitView方法导出为外部调用,因此您必须再执行几个步骤:

     var fcEl = $('#calendar'), view = fcEl.fullCalendar('getView'); view.unrenderDates(); view.renderDates(); fcEl.fullCalendar('refetchEvents'); 

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

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