如何在特定日期之后阻止Fullcalendar中的日期

我的未来日期总是比当前日期提前30天。 它存储在Date对象中。 我用它来解决这个问题:

var currentDate = new Date(); var futureBlockDate = new Date(); futureBlockDate.setDate(currentDate.getDate() + 30); 

使用FullCalendar jQuery插件,我希望在日历上以不同的背景颜色直观地阻止此日期之后的任何日期,以便用户知道他们无法点击它们或在那些日子创建活动。

使用FullCalendar执行此操作的最佳方法是什么? 也许默认情况下禁用所有日期,并且仅启用特定范围(从今天的日期到将来的30天)?

我想我可以使用以下代码将禁用的背景状态应用于所有单元格:

 $(".fc-widget-content").addClass("disabled"); .disabled .fc-day-content { background-color: #123959; color: #FFFFFF; cursor: default; } 

怎么做到呢?

使用dayRender选项将“禁用”类添加到超出范围的日期。

 $('#calendar').fullCalendar({ ... dayRender: function(date, cell){ if (date > maxDate){ $(cell).addClass('disabled'); } } ... }); 

您还可以使用viewRender事件和gotoDate方法,以防止用户在今天之后的30天内导航:

 $('#calendar').fullCalendar({ ... viewRender: function(view){ if (view.start > maxDate){ $('#calendar').fullCalendar('gotoDate', maxDate); } } ... }); 

这个解决方案怎么样?

 dayClick: function(date, allDay, jsEvent, view) { var myDate = new Date(); //How many days to add from today? var daysToAdd = 15; myDate.setDate(myDate.getDate() + daysToAdd); if (date < myDate) { //TRUE Clicked date smaller than today + daysToadd alert("You cannot book on this day!"); } else { //FLASE Clicked date larger than today + daysToadd alert("Excellent choice! We can book today.."); } } 

对于正在寻找定义min-display-datemax-display-date的解决方案的人:(对于fullcalendar v2)

 $('#calendar').fullCalendar({ defaultDate: new Date(), viewRender: function(view, element) { curdate = new Date(); viewdate = new Date(view.start); // PREV - force minimum display month to current month if (new Date(viewdate.getFullYear(), viewdate.getMonth() + 1, 1).getTime() <= new Date(curdate.getFullYear(), curdate.getMonth(), 1).getTime()){ $('.fc-prev-button').prop('disabled', true); $('.fc-prev-button').css('opacity', 0.5); } else { $('.fc-prev-button').prop('disabled', false); $('.fc-prev-button').css('opacity', 1); } // NEXT - force max display month to a year from current month if (new Date(viewdate.getFullYear(), viewdate.getMonth() + 1).getTime() >= new Date(curdate.getFullYear() + 1, curdate.getMonth() + 1).getTime()){ $('.fc-next-button').prop('disabled', true); $('.fc-next-button').css('opacity', 0.5); } else { $('.fc-next-button').prop('disabled', false); $('.fc-next-button').css('opacity', 1); } } }); 

这个选择当前月份

  $('#calendar').fullCalendar({ header: { left: 'prev,next', center: 'title', right: 'today' }, defaultDate: moment(), editable: false, //height:'auto', //weekends: false, defaultView: 'agendaWeek', eventLimit: true, events: { url: 'php/get-events.php', error: function() { $('#script-warning').show(); } }, loading: function(bool) { $('#loading').toggle(bool); }, viewRender: function(view,element) { var now = new Date(); var end = new Date(); end.setMonth(now.getMonth()); end.setDate(); now.setDate(1); if ( end < view.end) { $("#calendar .fc-next-button").hide(); return false; alert(end); } else { $("#calendar .fc-next-button").show(); } if ( view.start < now) { $("#calendar .fc-prev-button").hide(); return false; } else { $("#calendar .fc-prev-button").show(); } } }); }); 

单击禁用单元格(版本2.0):

 dayRender: function( date, cell ) { // It's an example, do your own test here if(cell.hasClass("fc-other-month")) { cell.addClass('disabled'); } }, dayClick: function(date, jsEvent, view) { if($(jsEvent.target).hasClass("disabled")){ return false; } // Your code // .... } 

只需在Fullcalendar中添加此代码:

 select: function (start, end, jsEvent, view) { if (start.isBefore(moment())) { $('#calendar').fullCalendar('unselect'); return false; } else { var currentDate = moment(start).format('YYYY/MM/DD')); alert(currentDate); } } 

简单快捷。 请享用!