如何动态地将1小时添加到事件的结束日期?

时间:2017-01-09 07:49:50

标签: jquery fullcalendar momentjs

我想将1小时添加到活动的开始日期并将其设置为结束日期。每当我尝试使用时刻添加功能时,我会得到:

  

未捕获的TypeError:无法读取属性' _calendar'未定义的       在D(moment.min.js:28)       在e(jquery.min.js:4)       在xb(jquery.min.js:4)       在xb(jquery.min.js:4)       在xb(jquery.min.js:4)       在Function.r.param(jquery.min.js:4)       在Function.ajax(jquery.min.js:4)       在HTMLButtonElement。 ((指数):385)       在HTMLButtonElement.dispatch(jquery.min.js:3)       在HTMLButtonElement.q.handle(jquery.min.js:3)

以下是我的代码:

function loadCalendar(batch) {
    calendar.fullCalendar({
        defaultDate: moment("2016-12-11"),
        utc: true,
        header: false,
        columnFormat: 'dddd',
        allDaySlot: false,
        hiddenDays: [0],
        defaultView: 'agendaWeek',
        minTime: '07:00:00',
        maxTime: '21:00:00',
        editable: true,
        droppable: true,
        lazyFetching: true,
        eventOverlap: false,
        events: {
            editable: true
        },
        drop: function(event, ui) {
            classname = $(this).attr("id");  
        },
        eventReceive: function(event) {
//                                    classname = $(this).attr("id");
            console.log(classname);
            $("#new-subj").modal("show");

            $("#save-subj").click(function() {
//                                        var batch = $(".course").text();
                var subjcode = $("#subjcode").val();
                var start = event.start.format("YYYY-MM-DD[T]HH:mm:SS+08:00");
                var end = moment(start).add(1, "hour");
                var prof = $("#prof").val();
                var profname = $("#prof option:selected").text();
                var room = $("#room").val();
                var roomname = $("#room option:selected").text();
                var course = $("#course-hidden").val();
                var yrlvl = $("#yrlvl-hidden").val();
//                                        var classname = $(this).css("background-color");

                $.ajax({
                    url: eventurl,
                    type: "POST",
                    data: {
                        batch: batch,
                        subjcode: subjcode,
                        prof: prof,
                        start: start,
                        end: end,
                        room: room,
                        course: course,
                        yrlvl: yrlvl,
                        classname: classname,
                        action: "add"
                    },
                    dataType: "json",
                    success: function(data) {
                        event.id = data.eventid;
                        console.log(data.eventid);
                        event.title = subjcode;
                        event.description = profname + "<br/>" + roomname;
                        event.className = classname;
                        calendar.fullCalendar('updateEvent', event);

                        $("select").prop("selectedIndex", 0);
                    },
                    error: function(e){
                        console.log("adding event: "+e.responseText);
                    }
                });
                calendar.fullCalendar('updateEvent', event);
                $("#new-subj").modal("hide");
            });

            $("#cancel-subj").click(function() {
                calendar.fullCalendar('removeEvents', event.id);
                getEvents();
            });
        },
        eventDragStop: function (event, jsEvent, ui, view) {
            if (isElemOverDiv(jsEvent)) {
                swal({
                    title: "Are you sure you want to delete this subject?",
                    type: "warning",
                    showCancelButton: true,
                    confirmButtonColor: "",
                    confirmButtonText: "Delete",
                    closeOnConfirm: false
                },
                function(isConfirm) {
                    if (isConfirm) {
                        $.ajax({
                            url: eventurl,
                            type: "POST",
                            data: {
                                id: event.id,
                                action: "delete"
                            },
                            dataType: "json",
                            success: function(data) {
                                console.log(data);
                                if (data.status == "success") {
                                    console.log(event.id);
                                    setTimeout(function(){
                                        calendar.fullCalendar('removeEvents', event.id);
                                        getEvents();
                                        swal("Deleted", "The subject has been deleted.", "success");
                                    }, 2000);
                                }
                            },
                            error: function(e){ 
                                console.log('Error processing your request: '+e.responseText);
                            }
                        });
                    }
                });
            }
        }
    });
}

2 个答案:

答案 0 :(得分:1)

这应该可以解决问题。对于事件,请按如下方式设置事件对象的开始日期和结束日期:

event.end = event.start.add(moment.duration("01:00:00"));

如果您的event.start不是片刻对象,则可能必须执行event.end = moment(event.start) ...才能使添加工作。

答案 1 :(得分:0)

我使用moment(event.start).add(1, "h").format("YYYY-MM-DDTHH:mm:ss");解决了自己的问题。 Moment将日期作为无效的ISO格式读取,因此我认为我需要对其进行格式化。