FullCalendar中的动态事件模板

时间:2014-09-29 05:25:46

标签: fullcalendar

有没有办法在FullCalendar中动态更改事件模板?

更新。我想要的是指定新的事件html(例如在eventRender回调中)并使FullCalendar用于我的事件呈现。像这样:

eventRender: function(event, element, view) {
  var template = '<div class="customEvent"></div>';
  return $.parse(template);
}

尝试上面的示例 - 没有用。

5 个答案:

答案 0 :(得分:16)

您可以相应地添加关于事件的元信息,例如类和样式:

events: [{
    title: 'Blue Event',
    start: '2014-06-01',
    description: 'Lorem ipsum lorem ipsum',
    class: 'blue main'
}]

CSS:

.fc-event.blue {}
.fc-event.main {}

eventRender上插入带有

的类
eventRender: function (event, element) {
    element.addClass(event.class)
}

并添加更多内容,例如:

if (event.description) {
    element.find('.fc-event-inner')
        .append("<div class='desc'>" + event.description + "</div>");
}

$(document).ready(function () {
    $('#calendar').fullCalendar({
        header: { left: '', center: '', right: '' },
        defaultDate: '2014-06-12',
        eventRender: function (event, element) {
            if (event.description) {
                element.find('.fc-event-inner')
                    .append("<div class='desc'>" + event.description) + "</div>";
            }               
            element.addClass(event.class)
        },
        events: [{
            title: 'Blue Event',
            start: '2014-06-01',
            description: 'Lorem ipsum lorem ipsum',
            class: 'blue main'
        }, {
            title: 'More Blue',
            start: '2014-06-01',
            description: 'More lorem ipsum',
            class: 'blue main'
        }, {
            title: 'Long Event',
            start: '2014-06-07',
            end: '2014-06-10',
            class: 'red main'
        }, {
            title: 'Meeting',
            start: '2014-06-12T10:30:00',
            end: '2014-06-12T12:30:00',
            class: 'blue main'
        }, {
            title: 'Lunch',
            start: '2014-06-12T12:00:00',
            class: 'blue main'
        }, {
            title: 'Birthday Party',
            start: '2014-06-13T07:00:00',
            class: 'red main'
        }, ],
    });
});
body {
    background-color: #eaefb5;
    font-family: sans-serif;
}
.fc-event-time, .fc-event-title {
    padding: 0 1px;
    float: left;
    clear: none;
    margin-right: 10px;
}
.fc-event.main {
    border: 5px solid #bbb;
    margin: 5px;
    padding: 3px
}
.fc-event.red {
    background-color: #f85032;
}
.fc-event.red .fc-event-title {
    font-family: Tahoma;
    font-size: 1.2em
}
.fc-event.blue {
    background: #87e0fd;
    background: -moz-linear-gradient(top, #87e0fd 0%, #53cbf1 40%, #05abe0 100%);
    background: -webkit-gradient(linear, left top, left bottom, color-stop(0%, #87e0fd), color-stop(40%, #53cbf1), color-stop(100%, #05abe0));
    background: -webkit-linear-gradient(top, #87e0fd 0%, #53cbf1 40%, #05abe0 100%);
    background: -o-linear-gradient(top, #87e0fd 0%, #53cbf1 40%, #05abe0 100%);
    background: -ms-linear-gradient(top, #87e0fd 0%, #53cbf1 40%, #05abe0 100%);
    background: linear-gradient(to bottom, #87e0fd 0%, #53cbf1 40%, #05abe0 100%);
    filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#87e0fd', endColorstr='#05abe0', GradientType=0);
}
.fc-event.blue .fc-event-title {
    font-size: 2em;
    color: #EEE;
    text-shadow: 2px 2px 4px #300000;
}
.fc-event.blue .desc {
    font-size:.8em;
    float:left;
    clear:both;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" type="text/css" href="//cdnjs.cloudflare.com/ajax/libs/fullcalendar/2.0.3/fullcalendar.css">
<script type='text/javascript' src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.7.0/moment.min.js"></script>
<script type='text/javascript' src="//cdnjs.cloudflare.com/ajax/libs/fullcalendar/2.0.3/fullcalendar.min.js"></script>

<div id="calendar"></div>

答案 1 :(得分:9)

eventRender回调函数可以修改元素,返回将用于渲染的全新DOM元素,或者它可以返回false,这将阻止事件被渲染。< / p>

http://fullcalendar.io/docs/event_rendering/eventRender/

此处示例:http://jsfiddle.net/3E8nk/506/

$('#calendar').fullCalendar({
    header: {
        left: 'prev,next today',
        center: 'title',
        right: 'month,agendaWeek,agendaDay'
    },
    defaultDate: '2014-06-12',
    editable: true,
    eventRender: function(event, element, view) {
        return $('<div>' + event.title + '</div>');
    },

    events: [
        {
            title: 'All Day Event',
            start: '2014-06-01'
        },
        {
            title: 'Long Event',
            start: '2014-06-07',
            end: '2014-06-10'
        },
        {
            id: 999,
            title: 'Repeating Event',
            start: '2014-06-09T16:00:00'
        },
        {
            id: 999,
            title: 'Repeating Event',
            start: '2014-06-16T16:00:00'
        },
        {
            title: 'Meeting',
            start: '2014-06-12T10:30:00',
            end: '2014-06-12T12:30:00'
        },
        {
            title: 'Lunch',
            start: '2014-06-12T12:00:00'
        },
        {
            title: 'Birthday Party',
            start: '2014-06-13T07:00:00'
        },
        {
            title: 'Click for Google',
            url: 'http://google.com/',
            start: '2014-06-28'
        },
    ]
});

答案 2 :(得分:1)

使用eventRender(回调)函数更改事件模板。以下是如何为活动添加提示:

eventRender: function(event, element, view) { 
   element.qtip({
      content: event.description
   });
}

使用此功能,您可以完全自定义显示的事件的任何细节,包括添加完整的新事件模板。有关更多详细信息,请查看文档: http://fullcalendar.io/docs/event_rendering/eventRender/

答案 3 :(得分:0)

您可以附加此类新信息

eventRender: function( event, element, view ) {
       element.find('.fc-title').append('<span class="yourCSS"></span> '); 
}

答案 4 :(得分:0)

在FullCalendar版本v4中,参数已更改(https://fullcalendar.io/docs/eventRender

eventRender: function(info) {
    var node = document.createElement('div');
    node.append(document.createTextNode(info.event.title));
    return node;
}

如果您想防止事件仅呈现 return false

eventRender: function(info) {
    return false;
}