我正在尝试使用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
事件上实施,任何建议都很棒
" drop"回调中没有" revertFunc()"。它只是不存在。
我的解决方法(FullCalendar V4)是管理EventLeave
和EventReceive
:在第一个保存原始事件中,在第二个事件中,在第二个事件中删除了新事件并还原原始事件。
在这里简化版本:
// 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);
}
}
在V5中有"恢复"在EventDrop上的功能。
在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>