简体   繁体   English

如何突出显示 FullCalendar.js 中有事件的一天?

[英]How to highlight a day which has an event in FullCalendar.js?

I am using FullCalendar.js in my website.我在我的网站上使用 FullCalendar.js。 I have set events from backend.我已经从后端设置了事件。 I want to change look of the day which has an event.我想更改有活动当天的外观。 In other words I need some sort of class where I have "fc-day" and an event in it.换句话说,我需要某种 class,其中包含“fc-day”和一个事件。

My code looks like and you see it on jsFiddle:我的代码看起来像你在 jsFiddle 上看到的:

$('#calendar').fullCalendar({
    events: [{
        title: 'event1',
        start: '2013-01-01'
    }, {
        title: 'event2',
        start: '2013-12-05',
        end: '2013-12-07'
    }, {
        title: 'event3',
        start: '2013-12-15'
    }]
})

You can use eventRender() property of Full Canendar. 您可以使用Full Canendar的eventRender()属性。

This works for me with fullcalendar v2: 这适用于fullcalendar v2:

$('#calendar').fullCalendar({
         events: [
        {
            title  : 'event1',
            start  : '2014-04-01'
        },
        {
            title  : 'event2',
            start  : '2014-04-05',
        },
        {
            title  : 'event3',
            start  : '2014-04-15'
        }
    ],
    eventRender: function (event, element, view) { 
        // like that
        var dateString = moment(event.start).format('YYYY-MM-DD');
        $('#calendar').find('.fc-day-number[data-date="' + dateString + '"]').css('background-color', '#FAA732');
     }

});

Note: You will need Moment library for this. 注意:为此,您将需要Moment库。

I could not find a desired property or method to use in the documentation of Fullcalendar. 我在Fullcalendar的文档中找不到所需的属性或方法。

I came up with a kind of a messy solution: adding a class by data-attributes: 我想出了一种麻烦的解决方案: 通过数据属性添加类:

function addClassByDate(date) {
    var dataAttr = getDataAttr(date);
    $("[data-date='" + dataAttr + "']").addClass("hasEvent");
}

function getDataAttr(date) {
    return date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + (date.getDate().toString().length === 2 ? date.getDate() : "0" + date.getDate());
};

It works fine for the settings, you've provided in your question. 您在问题中提供的设置对它的工作正常。

The working example could be found here: http://jsfiddle.net/6NShN/9/ 可以在以下位置找到工作示例: http : //jsfiddle.net/6NShN/9/

This is how I did it to give background color to each and every events. 这就是我为每个事件赋予背景颜色的方式。

JS FIDDLE DEMO JS FIDDLE DEMO

events: [
     {
       title: 'Test1',
       start: new Date(2014, 2, 28, 12, 0),
       end: new Date(2014, 2, 30, 12, 0), 
       backgroundColor: '#80ACED',
     },
     {
       title: 'Test2',
       start: new Date(2014, 2, 14, 3, 55),
       end: new Date(2014, 2, 21, 12, 0), 
       backgroundColor: '#80ACED',
     },
     {
       title: 'Test3',
       start: new Date(2014, 2, 2, 12, 0),
       end: new Date(2014, 2, 2, 12, 0), 
       backgroundColor: '#E82525',
     }
       ]

Hope this helps. 希望这可以帮助。 Refer : http://arshaw.com/fullcalendar/docs/event_data/Event_Object/ 请参阅: http : //arshaw.com/fullcalendar/docs/event_data/Event_Object/

Finally this is how it worked for me take a look at this 最后,这对我来说是如何工作的

jsfiddle jsfiddle

$('#calendar').fullCalendar({
         events: [
        {
            title  : 'event1',
            start  : '2014-04-01'
        },
        {
            title  : 'event2',
            start  : '2014-04-05',
        },
        {
            title  : 'event3',
            start  : '2014-04-15'
        }
    ],
    eventRender: function (event, element, view) { 
        // like that
        var dateString = $.fullCalendar.formatDate(event.start, 'yyyy-MM-dd');
        view.element.find('.fc-day[data-date="' + dateString + '"]').css('background-color', '#FAA732');
     }

});

weswesweswes is nearly correct view.element should be view.el weswesweswes几乎是正确的view.element应该是view.el

eventRender: function (event, element, view) { 

    var dateString = moment(event.start).format('YYYY-MM-DD');

    view.el.find('.fc-day[data-date="' + dateString + '"]').addClass('fc-has-event');

}

None of these will work for >=v2.0, since fullCalendar started using moment.js for date formatting. 这些都不适用于> = v2.0,因为fullCalendar开始使用moment.js进行日期格式化。

The following worked for me (passed to fullCalendar initialization function): 以下对我有用(传递给fullCalendar初始化函数):

eventRender: function (event, element, view) { 

var dateString = moment(event.start).format('YYYY-MM-DD');

view.element.find('.fc-day[data-date="' + dateString + '"]').addClass('fc-has-event');

}

You can add class on base of the fullcalender .fc-event class its not direct approach but its works. 您可以在fullcalender .fc-event类的基础上添加类,这不是直接方法,而是其工作原理。

Fiddle demo 小提琴演示

   $(document).ready(function (){ 
   $(".fc-event").addClass("myclass");
  });

Heres another Fiddle JS FIDDLE 这是另一个小提琴JS FIDDLE

Just by adding 只需添加

textColor: 'white',
backgroundColor:'purple' 

You can change the style 你可以改变风格

JS code: JS代码:

$('#calendar').fullCalendar({

    eventSources: [

        // your event source
        {
            events: [
        {
            title  : 'event1',
            start  : '2013-01-01'
        },
        {
            title  : 'event2',
            start  : '2013-12-05',
            end    : '2013-12-07'
        },
        {
            title  : 'event3',
            start  : '2013-12-15'
        }],

            textColor: 'white',
            backgroundColor:'purple'
        }

        // any other event sources...

    ]

});

Fullcalendar v2, for agendaWeek view: Fullcalendar v2,用于scheduleWeek视图:

eventRender: function(event, element, view ){
    if(view.name=='agendaWeek') {
        var weekdays = new Array('sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat');
        var eventweekday = $.fullCalendar.moment(event.start).format('d');
        $('.fc-'+weekdays[eventweekday]).css('background-color', '#FFC');
    }
},

Highlights all days with events. 突出显示所有活动日。

全日历日精选示例

Had this same issue and had found a bunch of solutions for older versions of FullCalendar (which seemed a bit hacky anyway).遇到了同样的问题,并为 FullCalendar 的旧版本找到了一堆解决方案(无论如何看起来有点老套)。 However, in v5 you can use eventDidMount and traverse up the DOM to the day cell.但是,在 v5 中,您可以使用 eventDidMount 并将 DOM 向上遍历到日期单元格。

JS Code: JS代码:

eventDidMount: function(info) {
    $(info.el).closest(".fc-daygrid-day").addClass("has-event");
}

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

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