Javascript FullCalendar阻止在工作时间之外删除事件

Javascript FullCalendar阻止在工作时间之外删除事件,javascript,fullcalendar,Javascript,Fullcalendar,我正在使用FullCalendar插件,并试图使它成为一个新的事件,当它被拖入工作时间以外的内容时,你不能删除它。我有它,所以你不能拖到当前日期之前的任何日期,但无法计算如何防止,比如说周末被拖到 我不想要一个硬编码的解决方案,在那里我必须为周末做一个if-than声明,因为如果我想增加工作时间,比如某个星期的星期三,并且只允许在下午1点到4点之间,该怎么办?所以我需要一个动态解决方案,我可以传递一些JSON,比如events:handles和businessHours也可以处理 $(docum

我正在使用FullCalendar插件,并试图使它成为一个新的事件,当它被拖入工作时间以外的内容时,你不能删除它。我有它,所以你不能拖到当前日期之前的任何日期,但无法计算如何防止,比如说周末被拖到

我不想要一个硬编码的解决方案,在那里我必须为周末做一个if-than声明,因为如果我想增加工作时间,比如某个星期的星期三,并且只允许在下午1点到4点之间,该怎么办?所以我需要一个动态解决方案,我可以传递一些JSON,比如events:handles和businessHours也可以处理

$(document).ready(function() {
    /* initialize the external events
    -----------------------------------------------------------------*/
    $('#external-events .fc-event').each(function() {
        // store data so the calendar knows to render an event upon drop
        $(this).data('event', {
            title: $.trim($(this).text()), // use the element's text as the event title
            stick: true // maintain when user navigates (see docs on the renderEvent method)
        });
        // make the event draggable using jQuery UI
        $(this).draggable({
            zIndex: 999,
            revert: true,      // will cause the event to go back to its
            revertDuration: 0  //  original position after the drag
        });
    });
    /* initialize the calendar
    -----------------------------------------------------------------*/
    $('#calendar').fullCalendar({
        header: {
            left: 'prev,next today',
            center: 'title',
            right: 'month,agendaWeek,agendaDay'
        },
        editable: true,
        droppable: true, // this allows things to be dropped onto the calendar
        drop: function() {
            // is the "remove after drop" checkbox checked?
            if ($('#drop-remove').is(':checked')) {
                // if so, remove the element from the "Draggable Events" list
                $(this).remove();
            }
        },
        /* This constrains it to today or later */
        eventConstraint: {
            start: moment().format('YYYY-MM-DD'),
            end: '2100-01-01' // hard coded must have
        },
        businessHours: {
            start: moment().format('HH:mm'), /* Current Hour/Minute 24H format */
            end: '17:00' // 5pm
        }
    });
});

这里是我当前示例的一个小部分

您遇到的一个问题是,初始化的事件没有持续时间,因此fullcalendar不知道事件在删除时是否与约束和营业时间重叠。简单地设置start/end就可以解决这个问题

$(this).data('event', {
    title: $.trim($(this).text()), // use the element's text as the event title
    stick: true, // maintain when user navigates (see docs on the renderEvent method)
    start: moment(),
    end: moment(),
});
bonus:在fullcalendar初始值设定项集合
defaultTimedEventDuration:'01:00:00',
(事件的默认持续时间为2小时)-根据应用程序应用的域设置此值

关于在不同的日子里有不同的时间;BusinessHours可以是一个数组-(它可以来自返回jsonarray的函数(因为jsonarray是完全限定的js)


查看此提琴,了解您的代码分支(工作时间)

我在《赏金》中提到,希望能够将新事件拖到日期,即使它有特定的时间限制……可能会将时间默认为营业时间内列出的最早时间。你是说这不可能使用FullCalendar,只能将事件添加到每日视图中吗?@eqiz Th如果我没弄错的话,at是在拉小提琴。我想说的是在《赏金》中提到的能够在每月视图中做这件事。我的意思是,这部分是你所说的不可能的,只能在每周或每天视图中拖动事件,而不是每月?@eqiz如果你看我贴的小提琴,this已按要求工作。在月视图中,您不能删除工作时间以外的事件(周末)。@eqiz您在赏金中寻找的内容应该全部在小提琴和我的答案中得到回答-我已经更新了一点。您只能在工作时间内删除事件(包括月视图的特定日期)businessHours参数接受一个可以来自函数的数组。
businessHours: [ 
    {
        dow: [ 1, 2, 3 ], // Monday, Tuesday, Wednesday
        start: '08:00', // 8am
        end: '18:00' // 6pm
    },
    {
        dow: [ 4, 5 ], // Thursday, Friday
        start: '10:00', // 10am
        end: '16:00' // 4pm
    }
],
eventConstraint:"businessHours",