简体   繁体   English

Fullcalendar:如何删除事件

[英]Fullcalendar: How to remove event

Thanks to another post here on StackOverflow, I added some code to my select: method that prevents users from adding an event on a date prior to NOW.感谢 StackOverflow 上的另一篇文章,我在 select: 方法中添加了一些代码,以防止用户在 NOW 之前的日期添加事件。

The downside is that when they click on the empty time slot, and the system then complains (an alert message), the attempted event remains.不利的一面是,当他们点击空的时间段,然后系统抱怨(警告消息)时,尝试的事件仍然存在。 How do I get rid of it?我该如何摆脱它? Thanks!谢谢!

Update: Here's my code:更新:这是我的代码:

    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);
                }

            }});
        }
    }

If you're using FullCalendar V2, you need to use the removeEvents method .如果您使用的是 FullCalendar V2,则需要使用removeEvents 方法

You can use it to delete events with a certain ID by calling it in this way:您可以通过以下方式调用它来删除具有特定 ID 的事件:

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

If you want to use your own function that decides whether or not an event get's removed, you can call it this way:如果要使用自己的函数来决定是否删除事件,可以这样调用:

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

FullCalendar has a removeEvent method that uses an id when you create the event. FullCalendar 有一个removeEvent方法,该方法在您创建事件时使用id

Example Full Calendar v1:完整日历 v1 示例:

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

Reference API v1参考 API v1

Example FullCalendar v2:示例 FullCalendar v2:

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

Reference API v2参考 API v2

Version 4.3版本 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();
        });

Full calendar version 4完整日历版本 4

How to remove event from calendar:如何从日历中删除事件:

<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>

This worked for me.这对我有用。 I hope, this will also help you.我希望,这也能帮助你。 Thanks for asking this question.感谢您提出这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM