javascriptjqueryfullcalendar

FullCalendar: remove events on button click


I am trying to remove a dynamically selected event from FullCalendar.

enter image description here

when this event is rendered it will be also displayed in the table with a delete button at the end of every row. enter image description here

what i want to do is when I click the delete button, it will also delete the event on the calendar. I can remove the row from the table but not in the calendar.

here is my code for selecting the event

var eventID = 0;
$('.calendar').fullCalendar({
    select: function(start, end, event, view, resource) {
            if(start.isBefore(moment())) {
                $('.calendar').fullCalendar('unselect');
                swal('Ooops!','You cannot select past date/time!','error')
            }else{
                $('#reserved_date').val(moment(start).format("YYYY-MM-DD"))
                $('#end_time').val(moment(end).format("hh:mm A"));
                $('#start_time').val(moment(start).format("hh:mm A"));
                $('#newScheduleModal').modal({
                    show : true,
                    backdrop: 'static',
                    keyboard: false
                });
                eventData = {
                    id: eventID +1,
                    title: 'Lesson Schedule',
                    start: start,
                    end: end,
                };
            }
            $(".fc-highlight").css("background", "red");

    },
    events: obj,
    eventRender:function( ev, element ) { 
        eventID++; 
        $('#sched_data').append('<tr class="tb-row">'+
            '<td>'+moment(ev.start).format('MMM. DD, YYYY')+'</td>'+
            '<td>'+moment(ev.start).format('hh:mm A')+'</td>'+
            '<td>'+moment(ev.end).format('hh:mm A')+'</td>'+
            '<td><button class="btn btn-danger btn-del btn-xs" data-id"'+ev._id+'"><i class="fa fa-times"></i></button></td></tr>'
        )
    },

})

here's the code for rendering the event to calendar

$('#btn-reserve').click(function(){
        $('.calendar').fullCalendar('renderEvent', eventData, true);

})

and here's my code for deleting an event

  $('body').on('click','.btn-del',function(){
    $(this).closest('.tb-row').remove();
    $('.calendar').fullCalendar('removeEvents', $(this).data('id'));
  })

Solution

  • I already did it,

    My code:

    $('body').on('click','.btn-del',function(){
        $(this).closest('.tb-row').remove();
        $('.calendar').fullCalendar('removeEvents', $(this).data('id'));
    })
    

    What it should be

    $('body').on('click','.btn-del',function(){
        var del_btn = $(this);
        del_btn.closest('.tb-row').remove();
    
        $(".calendar").fullCalendar('removeEvents', function(event) {
            return event.id == del_btn.data('id');
        });
    })
    

    the second parameter should be a function and not just the plain id.