根据周视图中的第一个和最后一个事件设置minTime和maxTime
Set the minTime and maxTime depending on the first and last event on week view
我有一个FullCalendar小部件,我使用在我的网站演示在这里: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
。问题是,如果没有回调
好的,目前还不支持。它属于那些没有动态setter的东西。为了解决这个问题,我们需要在每次更改视图时销毁并重新创建日历,这比听起来更可行。
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所示,还需要对结构进行一些更改。
另外,我注意到你在使用重复事件。根据您的需要,像我这里的答案这样的解决方案可能更简单,更容易管理。
相关文章:
- 而循环只设置php中输入字段中的第一个值
- 错误:$injector:modulerr模块错误(我的第一个SPA应用程序)
- IE11中的第二个调用取消了第一个Fetch API调用
- Javascript XMLHttpRequest——只有第一个POST请求有效
- 使用javascript或angularjs特定过滤器搜索字符串中第一个img标记的json值
- 将OnClick函数设置为<ul>,最后一个ul是擦除第一个ul-s
- RxJS油门行为;立即获取第一个值
- Angular UI网格:如何通过第一个UI网格中的按钮使第二个UI网格可见
- 选择多个实例中的第一个
- 如何在调用下一个请求之前完成第一个Ajax Get请求
- Lodash:返回对象的第一个键,该对象的值(即数组)中有一个给定的元素(即字符串)
- 看起来第一个console.log是'It’不太对
- 与杜兰达尔合作的第一个JavaScript项目.尝试从第三方 API 获取数据
- 节点发布错误对象的第一个“属性”
- Javascript:表单验证getElementById仅返回第一个id元素
- 手风琴我想更改第一个 li 边框半径和最后一个 li 边框半径,我该怎么做请帮助我.
- 当移动到最后一个li时,返回到第一个li(使用Jquery滚动到下一个)
- CSS的条件包含,但不作为最后一个(或第一个)CSS
- 为什么jQuery代码将最后一个图像替换为第一个
- 删除最后一个无效字符,而不是第一个(javascript验证)