Fullcalendar drop外部事件恢复功能

时间:2017-11-21 07:56:17

标签: javascript jquery fullcalendar

我正在尝试使用fullcalendar库创建一个事件日历并按照演示external-dragging,我理解这个概念只是想知道如何执行恢复功能,如果我按下取消放置事件将回到原来的位置。

我正在使用sweetalert2库替换默认的javascript警报,下面是我的代码。

$('#calendar').fullCalendar({
            header: {
                left: 'prev,next today',
                center: 'title',
                right: 'month'
            },
            editable: true,
            eventConstraint: {
            start: moment().format('YYYY-MM-DD'),
            end: '2100-01-01' // hard coded goodness unfortunately
            },
            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();

                }

                swal({
                    title: 'Are you sure?',
                    text: "You want to change this event schedule?",
                    type: 'warning',
                    showCancelButton: true,
                    confirmButtonColor: '#3085d6',
                    cancelButtonColor: '#d33',
                    confirmButtonText: 'Yes, proceed'
                  }).then(function (result) {
                    if (result.value) {


                      swal(
                        'Success!',
                        'The event schedule was successfully changed to ',
                        'success'
                      )



                    }else{

                      revertFunc();

                    }
                  })
                 //end of drop  

            },

revertFunc();仅适用于eventDrop,但我无法在drop事件中实施,任何建议都会很棒

2 个答案:

答案 0 :(得分:2)

在“放置”回调中没有“ revertFunc()”。它只是不存在。

我的解决方法(FullCalendar v4)是同时管理EventLeaveEventReceive:在第一个事件中,保存原始事件,在第二个事件中,删除新事件并恢复原始事件。

>

以下是简化版本:

// to map original events (event.id => event)
_oldEventsInfo = new Map();

eventLeaveHandler(info) {
    _oldEventsInfo.set(info.event.id, info);
}

eventReceiveHandler(info) {
    if (/* for any reason cannot be moved */) {
        this.revert(info);
    } else {
        _oldEventsInfo.delete(info.event.id);
    }
}

revert(info) {
    var oldInfo = _oldEventsInfo.get(info.event.id);
    if (oldInfo) {
        // build a new event to restore in the original calendar
        var oldCalendar = oldInfo.event._calendar;
        var restoredEvent = {
            id: oldInfo.event.id,
            // etc
        };
        oldCal.addEvent(restoredEvent);
        // delete the destination (dragged) event from dest calendar
        info.event._calendar.getEventById(info.event.id).remove();
        // remove the old event from my saved
        _oldEventsInfo.delete(info.event.id);
    }
}

答案 1 :(得分:0)

在 v5 中,eventDrop 上有一个“revert”函数。

在 React 中,您可以将此回调存储为引用变量

  const revertDropEvent = useRef<() => void>();

  const handleDropEvent = ({ event, revert }: EventDropArg) => {
      revertDropEvent.current = revert;
  }
  
  const handleCancelDrop = () => {
      if (revertDropEvent.current) revertDropEvent.current();
  }

  return (
  <div>
    <FullCalendar
       eventDrop={handleDropEvent}
       /* ... */
    />
    <ConfirmationModal onClose={handleCancelDrop}/>
  </div>