我正试图用jQuery Fullcalendar制作一个动态日历,如果事件在月、周或日视图中,我想显示不同的标题。我正在尝试使用addEventSource
和removeEventSource
,但无法获得要渲染的事件。这是我的代码:
<script type='text/javascript'>
$(document).ready(function() {
var date = new Date();
var d = date.getDate();
var m = date.getMonth();
var y = date.getFullYear();
daySource = new Object();
daySource.title = 'MONTH'; // this should be string
daySource.start = new Date(y, m, d); // this should be date object
daySource.end = new Date(y, m, d);
var day =new Array();
day[0]= daySource;
monthSource = new Object();
monthSource.title = 'MONTH'; // this should be string
monthSource.start = new Date(y, m, d); // this should be date object
monthSource.end = new Date(y, m, d);
var month =new Array();
month[0]= monthSource;
$('#calendar').fullCalendar({
header: {
left: 'prev,next today',
center: 'title',
right: 'month,agendaWeek,agendaDay'
},
columnFormat: {
month: 'ddd',
week: 'ddd d/M',
day: 'dddd d/M'
},
defaultView: 'agendaDay',
viewDisplay: function(view) {
if (lastView == undefined) { lastView = 'firstRun'; }
if (view.name != lastView ) {
if (view.name == 'agendaWeek') {
$('#calendar').fullCalendar( 'addEventSource', month );
$('#calendar').fullCalendar( 'removeEventSource', day );
$('#calendar').fullCalendar( 'removeEventSource', day );
$('#calendar').fullCalendar('renderEvents');
}
if (view.name == 'agendaDay') {
$('#calendar').fullCalendar( 'addEventSource', day );
$('#calendar').fullCalendar( 'removeEventSource', month );
$('#calendar').fullCalendar( 'removeEventSource', month );
$('#calendar').fullCalendar('renderEvents');
}
if (view.name == 'month') {
$('#calendar').fullCalendar( 'addEventSource', month );
$('#calendar').fullCalendar( 'removeEventSource', day );
$('#calendar').fullCalendar( 'removeEventSource', day );
$('#calendar').fullCalendar('renderEvents');
}
lastView = view.name;
}
},
timeFormat: { // for event elements
agendaDay: '',
agendaWeek: '',
month: '',
'': 'h(:mm)t' // default
},
}); $('#calendar').fullCalendar( 'addEventSource', month ); $('#calendar').fullCalendar('rerenderEvents');
});
首先我不知道为什么复仇者没有渲染,然后标题是否会根据视图而改变。我将感谢任何帮助。
eventSources
需要是数组,而不仅仅是事件对象。将对象封装在数组中,看看这是否有效。
检查这个小提琴的演示-jsfiddle.net/100thGear/CBPW8/1