Fullcalendar:如何删除事件



感谢StackOverflow上的另一篇文章,我在我的select:方法中添加了一些代码,防止用户在NOW之前添加事件。

不利的一面是,当他们点击空时隙,然后系统发出警报(警报消息)时,尝试的事件仍然存在。我该如何摆脱它?谢谢

更新:这是我的代码:

    select: function(start, end, jsEvent) {
        var check = start._d.toJSON().slice(0,10),
            today = new Date().toJSON().slice(0,10),
            m = moment(),
            url = "[redacted]",
            result = {};
            title = "Class",
            eventData = {
                title: title,
                start: start,
                end: start.clone().add(2, 'hour'),
                durationEditable: false,
                instructorid: 123,
                locationid: 234
            };

        if(check < today) {
            alert("Cannot create an event before today.");
            $("#calendar").fullCalendar('removeEvents', function(eventObject) {
                return true;
            });
        } else {
            $.ajax({ type: "post", url: url, data: JSON.stringify(eventData), dataType: 'JSON', contentType: "application/json", success: function(result) {
                if ( result.SUCCESS == true ) {
                    $('#calendar').fullCalendar('renderEvent', eventData, true);
                    $('#calendar').fullCalendar('unselect');
                } else {
                    alert(result.MESSAGE);
                }
            }});
        }
    }

如果使用FullCalendar V2,则需要使用removeEvents方法。

您可以通过以下方式调用它来删除具有特定ID的事件:

$("#calendar").fullCalendar('removeEvents', 123); //replace 123 with reference to a real ID

如果你想使用自己的函数来决定事件是否被删除,你可以这样称呼它:

$("#calendar").fullCalendar('removeEvents', function(eventObject) {
    //return true if the event 'eventObject' needs to be removed, return false if it doesn't
});

FullCalendar有一个removeEvent方法,在创建事件时使用id

示例完整日历v1:

var calendar = $('#calendar').fullCalendar({ ... stuff ... });
    calendar.fullCalendar( 'addEventSource', {id:123, stuff:'stuff'});
    // ... other calendar things here...
    calendar.fullCalendar( 'removeEvent', 123);

参考API v1

示例FullCalendar v2:

var calendar = $('#calendar').fullCalendar({ ... stuff ... });
    calendar.fullCalendar( 'addEventSource', {id:123, stuff:'stuff'});
    // ... other calendar things here...
    calendar.fullCalendar( 'removeEvents', [123]);

参考API v2

4.3版

        calendar = new Calendar(calendarEl, {
                plugins : [ 'interaction', 'dayGrid', 'list' ],
                header : {
                    left : 'prev,next today',
                    center : 'title',
                    right : 'dayGridMonth,timeGridWeek,timeGridDay,list'
                },
                editable : true,
                droppable : true, 
                eventReceive : function(info) {
                    alert(info.event.title);
                },
                eventDrop : function(info) {
                    alert(info.event.title + " was dropped on "
                            + info.event.start.toISOString());
                    if (!confirm("Are you sure about this change?")) {
                        info.revert();
                    }
                },
                eventClick : function(info) {
                     //delete event from calender
                    info.event.remove();
                }
            });
            calendar.render();
        });

完整日历版本4

如何从日历中删除事件:

<div id="calendar"></div>
<script>
document.addEventListener('DOMContentLoaded', function() {
    var calendarEl = document.getElementById('calendar');
    var calendar = new Calendar(calendarEl, {
      events: [
        {
          id: '505',
          title: 'My Event',
          start: '2010-01-01',
          url: 'http://google.com/'
        }
        // other events here
      ],
      eventClick: function(info) {
        info.jsEvent.preventDefault(); // don't let the browser navigate
        if (info.event.id) {
            var event = calendar.getEventById(info.event.id);
            event.remove();
        }
      }
    });
});
</script>

这对我很有效。我希望,这也会对你有所帮助。谢谢你提出这个问题。

相关内容

  • 没有找到相关文章