FullCalendar -通过模态删除事件

FullCalendar - removing event via modal

本文关键字:删除 事件 模态 -通 FullCalendar      更新时间:2023-09-26

我正在使用FullCalendar,我被困在试图删除一个事件。事情是这样的:当我点击一个事件,它给我带来了一个模态的每一个信息,我有关于这个事件,在这个模态我有2个按钮:一个接受事件,一个拒绝它。我需要的是,当我点击"接受"的事件颜色必须变成绿色,当我拒绝的事件必须被删除。我该怎么做呢?下面是我的代码:

CalendarioAgenda.html(我的模态是,我减少了代码,所以更容易理解,但我的每个字段都像# modalclient):

<div class="modal fade" id="ModalVistoria" tabindex="-1" role="basic" aria-hidden="true" style="display: none">
    <div class="modal-dialog" style="width:50%">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true"></button>
                <h4 class="modal-title"></h4>
            </div>
            <div class="modal-body">
                <div class="row">
                    <div class="col-xs-4">
                        <div class="form-group form-md-line-input">
                            <div class="form-control form-control-static" id="ModalCliente">
                            </div>
                            <label for="ModalCliente">Cliente</label>
                        </div>
                    </div>      
                </div>
                <div class="modal-footer">
                    <input type="hidden" id="ModalId"/>
                    <button type="button" class="btn red-intense Recusar">Recusar</button>//button to refuse the event
                    <button type="button" class="btn blue-steel Aceitar">Aceitar</button>//button to accept the event
                </div>
            </div>
            <!-- /.modal-content -->
        </div>
        <!-- /.modal-dialog -->
    </div>
</div>

和my Calendar.js:

$('#calendar').fullCalendar({ //re-initialize the calendar
            header: h,
            defaultView: 'month', // change default view with available options from http://arshaw.com/fullcalendar/docs/views/Available_Views/ 
            slotMinutes: 15,
            editable: true,
            droppable: false, // this allows things to be dropped onto the calendar !!!
            events: [{
                id: '01',
                title: 'Vistoria Viceri',
                start: new Date(y, m, 1), //usar start no campo de data da modal
                backgroundColor: Metronic.getBrandColor('yellow'),
                cliente: 'Viceri',
                tipo: 'Empresarial Simples',
                hora: '12:00',
                endereco: 'General Osorio, 61',
                bairro: 'Centro',
                cidade: 'Jundiai',
                estado: 'SP',
                contato: 'Marcel Pratte',
                telefone: '11 3308 6999',
            }, {
                id: '02',
                title: 'Visita a cliente',
                start: new Date(y, m, d - 5),
                end: new Date(y, m, d - 3),
                backgroundColor: Metronic.getBrandColor('green'),
                cliente: 'Maxtel',
                tipo: 'Empresarial',
                hora: '15:00',
                endereco: 'Avenidade Sao Joao',
                bairro: 'Ponte Sao Joao',
                cidade: 'Jundiai',
                estado: 'SP',
                contato: 'Marcio',
                telefone: '11 45270001',
            }, {
                id: '03',
                title: 'Vistoria Envision',
                start: new Date(y, m, d - 3, 16, 0),
                allDay: false,
                backgroundColor: Metronic.getBrandColor('red'),
                cliente: 'Envision',
                tipo: 'Empresarial complexa',
                hora: '12:36',
                endereco: 'Rua da empresa',
                bairro: 'Centro',
                cidade: 'Sao Paulo',
                estado: 'SP',
                contato: 'Joaquim',
                telefone: '011 995257788',
            }],
            //opção de click no evento para redirecionar para modal
            eventClick: function (event, jsEvent, view) {
                $('.modal-title').html(event.title);
                $('#ModalCliente').html(event.cliente);
                $('#ModalTipo').html(event.tipo);
                $('#ModalDataHora').html(event.d + ' - ' + event.hora);
                $('#ModalEndereco').html(event.endereco);
                $('#ModalBairro').html(event.bairro);
                $('#ModalCidade').html(event.cidade);
                $('#ModalEstado').html(event.estado);
                $('#ModalContato').html(event.contato);
                $('#ModalTel').html(event.telefone);
                $('#ModalId').html(event.id);
                $('#ModalVistoria').modal();
                $('.Recusar').click(function (events) {
                    var id = $('#ModalId').val();
                    $('#calendar').fullCalendar('removeEvents', id);
                    $("#calendar").fullCalendar('addEventSource', events);
                });
            }
        });

谢谢

您的行$('#ModalId').html(event.id);var id = $('#ModalId').val();针对两个不同的元素,因此您无法检索调用val()的事件id。

通过像$('#ModalId').val(event.id);这样设置值,它可以正常工作。

这里是你可以删除事件和改变背景颜色的部分:

$('.Recusar').click(function (events) {
   var id = $('#ModalId').val();
   $('#calendar').fullCalendar('removeEvents', id);
   $("#calendar").fullCalendar('addEventSource', events);
   $('#ModalVistoria').modal('hide');
});
$('.Aceitar').click(function (events) {
  var id = $('#ModalId').val();
  var ev = $("#calendar").fullCalendar('clientEvents', id);
  ev[0].backgroundColor = 'green';
  $("#calendar").fullCalendar('addEventSource', events);
  $('#ModalVistoria').modal('hide');
});

这是一个工作的jsFiddle:点击这里