简体   繁体   English

如何在fullCalendar中编辑事件?

[英]How do I edit an event in fullCalendar?

I have this sample: 我有这个样本:

link 链接

CODE HTML: 代码HTML:

<div id='calendar'></div>
    <div class="cont" style="display:none">
    <button type="button" class="btn btn-primary btn-save" id="edit">Save</button>
​​    <input id="required-input" type="text" name="firstname" id="firstname" placeholder="John">

CODE JS: 代码JS:

   $(function() { // document ready

  var calendar=$('#calendar').fullCalendar({
    header: {
      left: 'prev,next today',
      center: 'title',
      right: 'month,agendaWeek,agendaDay'
    },
    defaultDate: '2014-11-12',
    editable: true,
    eventLimit: true, // allow "more" link when too many events
    defaultView: 'agendaDay',
    selectable: true,   //permite sa selectezi mai multe zile
    selectHelper: true,  //coloreaza selctia ta


    eventClick:  function(event, jsEvent, view) {
                $(".cont").show();
                $( "#edit" ).click(function(e) {
                     e.preventDefault();
                     alert(event._id);
                     event.title = $("#required-input").val();
                     $('#calendar').fullCalendar('updateEvent', event);
                     $(".cont").hide();
                });

        //  event.title = "CLICKED!";


    },
    select: function(start, end, allDay)  
            {

                    var title = "test"
                    if(title)
                    {
                            calendar.fullCalendar('renderEvent',
                                {
                                    title: title,
                                    start: start,
                                    end: end,
                                    //allDay: allDay
                                },

                        true // make the event "stick"
                        );
                    }   


                calendar.fullCalendar('unselect');  
            },

     events: [
        {

            title  : 'titleEvent',
            start  : '2014-11-12',
             allDay : false // will make the time show
        },

    ]        

  });

});

Unfortunately at this time to edit all events, I just want the current event. 不幸的是,此时要编辑所有事件,我只需要当前事件。

The reason this happens? 发生这种情况的原因? Because button click? 因为单击按钮?

I put an alert and run twice, you can test the link above and see more clearly. 我发出警报并运行了两次,您可以测试上面的链接并更清楚地看到。

Thanks in advance! 提前致谢!

The problem is every time you click on an event a new click handler is getting added to the button, one possible solution is to use .off() to remove the current handler and add a new handler. 问题是,每次单击事件时,都会向按钮添加新的单击处理程序,一种可能的解决方案是使用.off()删除当前处理程序并添加新的处理程序。

Another is to can use .data() api to pass the event instance to the click handler 另一个方法是可以使用.data() API将事件实例传递给点击处理程序

 $(function() { // document ready var calendar = $('#calendar').fullCalendar({ header: { left: 'prev,next today', center: 'title', right: 'month,agendaWeek,agendaDay' }, defaultDate: '2014-11-12', editable: true, eventLimit: true, // allow "more" link when too many events defaultView: 'agendaDay', selectable: true, //permite sa selectezi mai multe zile selectHelper: true, //coloreaza selctia ta eventClick: function(event, jsEvent, view) { $(".cont").show().data('event', event); }, select: function(start, end, allDay) { var title = "test" if (title) { calendar.fullCalendar('renderEvent', { title: title, start: start, end: end, //allDay: allDay }, true // make the event "stick" ); } calendar.fullCalendar('unselect'); }, events: [{ title: 'titleEvent', start: '2014-11-12', allDay: false // will make the time show }, ] }); $("#edit").click(function(e) { e.preventDefault(); var title = $("#required-input").val().trim(); if (!title) { return; } var event = $(".cont").data('event'), isAdd = !event; if (isAdd) { event = {}; event.start = '2014-11-12'; } event.title = title; if (isAdd) { $('#calendar').fullCalendar('renderEvent', event, true); } else { $('#calendar').fullCalendar('updateEvent', event); } $(".cont").hide().removeData('event'); }); $("#add").click(function(e) { $(".cont").show(); }); }); 
 <script type="text/javascript" src="//code.jquery.com/jquery-1.9.1.js" ie.="" data-type=""></script> <script type="text/javascript" src="//code.jquery.com/ui/1.9.2/jquery-ui.js"></script> <link rel="stylesheet" type="text/css" href="//code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css"> <link rel="stylesheet" type="text/css" href="//cdnjs.cloudflare.com/ajax/libs/fullcalendar/2.3.0/fullcalendar.min.css"> <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.9.0/moment.min.js"></script> <script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/fullcalendar/2.3.0/fullcalendar.min.js "></script> <script type="text/javascript" src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script> <link rel="stylesheet" type="text/css" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css"> <div id='calendar'></div> <div class="cont" style="display:none"> <button type="button" class="btn btn-primary btn-save" id="edit">Save</button> <input id="required-input" type="text" name="firstname" id="firstname" placeholder="John" /> </div> <button type="button" class="btn btn-primary btn-save" id="add">Add</button> 

you are getting a memory leak, by binding the edit click event everytime you click on an event 通过在每次单击事件时绑定编辑单击事件,您将发生内存泄漏

   $( "#edit" ).off('click').on('click', function(e) {
     ...
   )}

will solve it, by unbinding and rebinding every time 通过每次解除绑定和重新绑定来解决该问题

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

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