【问题标题】:Fullcalendar Event Add/Edit and Resize.and get sucesscallback without page reload/refreshFullcalendar 事件添加/编辑和调整大小。并在不重新加载/刷新页面的情况下获得成功回调
【发布时间】:2020-07-23 16:00:51
【问题描述】:

当我添加新事件或更新事件或更改事件或拖动新时间或新用户时,每次页面刷新。但是如何在不重新加载页面并显示更新的事件时间或删除事件的情况下解决此问题。我正在为 fullcalendar 使用日历版本 5。

<script type="text/javascript">
  document.addEventListener('DOMContentLoaded', function() {
    var calendarEl = document.getElementById('calendar');
    var calendar = new FullCalendar.Calendar(calendarEl, {
      now: '<?php echo $CurrentDate; ?>',
      editable: true,
      aspectRatio: 1.8,
      scrollTime: '00:00',
      timeFormat: 'H(:mm)',
      headerToolbar: {
        left: 'today prev,next',
        center: 'title',
        right: 'resourceTimelineDay,timeGridWeek,dayGridMonth'
      },
      resources: <?php eventGroups(); ?>,
      events: function(info, successCallback, failureCallback) {
        successCallback(<?php eventGroupsDetails($fromDate,$toDate); ?>)
      },
      dateClick: function(info) {
        $('#addScheduleEntry').modal('show');
        $(document).on('click', '.modal_default_ok', function() {
            $.ajax({
                url: "calendar_event_create.php",
                type: "POST",
                data: $("#frmEvent").serialize(),
                dataType: 'json',
                success: function(response){
                    if(response.result == 'success'){
                        // how to get updated event and display in calender without page refresh
                    }
                }
            });    
        });
      },
      
      eventClick: function(info, jsEvent, view) { 
        $.ajax({
            url: "calendar_event_update.php",
            type: "POST",
            data: $("#frmEventUpdate").serialize(),
            dataType: 'json',
            success: function(response){
                // how to get updated event and display in calender without page refresh
            }
        });
      },
      editable: true,
      eventDrop: function(info, delta, revertFunc, ui) {
            $.ajax({
              url: "save_drop_event_detail.php",
              type: "POST",
              data: {EventId:EventId},
              dataType: 'json',
              success: function(response){
                // how to get updated event and display in calender without page refresh
              } 
            });
      },
      eventResize: function(info) {
        $.ajax({
          url: "save_resize_event_detail.php",
          type: "POST",
          data: {EventId:EventId},
          dataType: 'json',
          success: function(response){
            // how to get updated event and display in calender without page refresh
          } 
        });
      }
    });
    calendar.render();
  });
</script>
<div class="row"> 
    <div id="msg"></div>
    <div class="col-md-12"> 
        <div id='calendar'></div>
    </div>
</div>

【问题讨论】:

  • 您显示的任何代码都不会导致页面自行刷新。也许代码中其他地方的某些东西是造成这种情况的原因。
  • 至于您的问题“如何在不刷新页面的情况下获取更新的事件并在日历中显示”...您可以调用 calendar.refetchEvents() 告诉 fullCalendar 从服务器刷新事件数据。 如果您的事件有一个 AJAX 数据源,而不是静态数据源,这将起作用。您在上一个问题中问过这个问题,我向您提供了有关如何实现它的所有信息,但您似乎没有对此做任何事情 - 您的事件数据仍然通过 PHP 函数硬编码到您的脚本中.
  • 您能否提供如何存储事件的示例 json 数据。
  • 嗯?将事件数据存储在数据库中,并使用 PHP 从数据库中获取数据并将其编码为 JSON。然后告诉 fullCalendar 在需要获取事件时它可以用来调用该 PHP 脚本的 URL。网上有很多人在 PHP 中执行此任务(或非常相似的 json 编码任务)的示例,您可以轻松找到示例。您似乎还没有在任何基础研究上投入任何精力。
  • 您好,感谢您的支持我会成功完成此问题已经解决。

标签: php jquery ajax fullcalendar fullcalendar-5


【解决方案1】:
<script type="text/javascript">
  document.addEventListener('DOMContentLoaded', function() {
    var calendarEl = document.getElementById('calendar');
    var calendar = new FullCalendar.Calendar(calendarEl, {
      now: '<?php echo $CurrentDate; ?>',
      editable: true,
      aspectRatio: 1.8,
      headerToolbar: {
        left: 'today prev,next',
        center: 'title',
        right: 'resourceTimelineDay,timeGridWeek,dayGridMonth'
      },
      views: {
        timeGridWeek: {
          type: 'timeGrid',
          duration: { weeks: <?php if($searchWeek=="") { echo 1; } else{echo $searchWeek;} ?> }
        }
      },
      resources: <?php eventGroups(); ?>,
      events: function(fetchInfo, successCallback, failureCallback) {
        var fromDate = "<?php echo $fromDate; ?>";
        var toDate = "<?php echo $toDate; ?>";
        $.ajax({
          url: "ajaxRefresh.php",
          type: "POST",
          dataType: "json",
          success: function(response){
            var events = [];
            $.each(response, function (i, item) {
                events.push({
                  id: response[i].id,
                  start: response[i].start,
                  end: response[i].end,
                  title: response[i].title,
                });
            });
            successCallback(events);
          }
        });
      },
      dateClick: function(info) {
        $('#addScheduleEntry').modal('show');
        $.ajax({
            url: "calendar_event_create.php",
            type: "POST",
            data: $("#frmEvent").serialize(),
            dataType: 'json',
            async : false,
            success: function(response){
              if(response.result == 'success'){
                calendar.refetchEvents();
              }
            }
        });
      },
      eventClick: function(info, jsEvent, view) { 
        var text = JSON.stringify(info, function (key, value) {
        var eventId = info.event._def.publicId;
          $("#eventDetail").modal("show");  

          $(document).on('click', '.removeEvent', function() {
            var eventId = $("#event_id").val();
            var yes = confirm("Are you sure ?");
            if(yes == true){
              $("#process_loader").fadeIn();  
              $.ajax({
                url: "calendar_event_remove.php",
                type: "POST",
                data: {eventId:eventId},
                dataType: 'json',
                async : false,
                success: function(response){
                  if(response.msg == "true"){
                    calendar.refetchEvents();
                  }
                }
              });
            }
          });
      },
      editable: true,
      eventDrop: function(info, delta, revertFunc, ui) {
        $.ajax({
          url: "save_drop_event_detail.php",
          type: "POST",
          data: {EventId:EventId,oldUserId:oldUserId,newUserId:newUserId,newEventStartTime:newEventStartTime,newEventEndTime:newEventEndTime},
          dataType: 'json',
          async : false,
          success: function(response){
            if(response.msg == 'success'){
              calendar.refetchEvents();
            }
          } 
        });
      },
      eventConstraint: {
        slotMinTime: '10:00' ,
        slotMaxTime: '11:00'
      }

    });
    calendar.render();
  });
</script>

我们使用此函数“calendar.refetchEvents();”并为您的事件使用 AJAX 数据源,而不是静态数据源。

【讨论】:

  • 所有这些:` var events = []; $.each(response, function (i, item) { events.push({ id: response[i].id, start: response[i].start, end: response[i].end, title: response[i] ].title, }); });` 不是必需的。你只是在创建一个与你已经拥有的相同的数组,这是没有意义的。相反,您可以直接写successCallback(response)
  • 好的,我们已经删除并更新了 successCallback(response) 并且工作正常,谢谢。
猜你喜欢
  • 1970-01-01
  • 2015-07-05
  • 1970-01-01
  • 1970-01-01
  • 2020-11-14
  • 1970-01-01
  • 2016-09-04
  • 1970-01-01
  • 2013-08-20
相关资源
最近更新 更多