我有一个FullCalendar小部件,我在我的网站上使用演示文稿' d:http://jsfiddle.net/46tnzj72/7/
我想将开始时间设置为当天的第一个事件,将结束时间设置为当天的最后一个事件。
目前,我对minTime
进行了硬编码:
$('#calendar').fullCalendar({
editable: false,
handleWindowResize: true,
weekends: false, // Hide weekends
defaultView: 'agendaWeek', // Only show week view
header: false, // Hide buttons/titles
minTime: '07:00:00', // Start time for the calendar
columnFormat: {
week: 'dddd' // Only show day of the week names
},
allDayText: 'Online/TBD'
});
我想正确的方法是找到所有日期的最小值和最大值,然后设置minTime
和maxTime
。问题是,我不知道如何在没有回调的情况下做到这一点
答案 0 :(得分:0)
好的,这还不支持。它属于没有动态设置器的事物列表。要解决这个问题,我们需要在每次更改视图时销毁并重新创建日历,这比听起来更可行。
var prevDate = moment("1000-01-01"); //arbitrary "not now" date
var options = { //Store the FC options in a variable
editable: false,
weekMode: 'liquid',
handleWindowResize: true,
weekends: false,
defaultView: 'agendaWeek',
viewRender: function (view, element) {
var newDate = $('#calendar').fullCalendar("getDate");
if (!newDate.isSame(prevDate, 'day')) { //if the date changed
prevDate = moment(newDate);
var events = $('#calendar').fullCalendar("clientEvents"); //Get all current events
$('#calendar').fullCalendar("destroy"); //Remove current calendar
$('#calendar').fullCalendar( //Rebuild the calendar
$.extend({}, options,
getEventLimits(events), //new limits
{defaultDate: newDate}) //preserve the date
);
}
},
events: eventSourceFunction,
};
$('#calendar').fullCalendar(options); //First build
// The following is needed or the first render won't have proper minTime/maxTime
// because events haven't been rendered yet. It just forces a date change.
window.setTimeout(function(){
console.log("timeout date:",prevDate);
var date = moment(prevDate);
$('#calendar').fullCalendar( 'incrementDate', moment.duration("7.00:00:00") );
$('#calendar').fullCalendar("gotoDate",date);
},1);
并获得限制:
var getEventLimits = function(events){
if(events.length > 0){
var max = events[0].end.format("HH:mm:ss"); // we will only be comparing the timestamps, not the dates
var min = events[0].start.format("HH:mm:ss"); // and they will be compared as strings for simplicity
for(var i = 1; i < events.length; i++){
if(max < events[i].end.format("HH:mm:ss")){
max = events[i].end.format("HH:mm:ss");
}
if(min > events[i].start.format("HH:mm:ss")){
min = events[i].start.format("HH:mm:ss");
}
}
}
return {maxTime:max,minTime:min};
}
此 JSFiddle 演示时,还需要进行一些结构更改。
另外,我注意到你正在使用重复事件。根据您的需求,像我的回答here这样的解决方案可能更简单,更易于管理。