简体   繁体   中英

fullcalendar event info in modal popup

I am working on fullCalendar and attempting to get the event info to show in a popup modal (when the event is clicked on).

However I can only get the event title, location, start date and end date showing.

I also need the start time and end time showing. What am I doing wrong?

My code is below:

eventClick:  function(event, jsEvent, view) {
    var startdate= new Date(event.event.start);
    var enddate= new Date(event.event.end);
    var starttime= new Date(event.event.startTime);
    var endtime= new Date(event.event.endTime);
    //console.log(event);
    $('#modalID').html(event.event.id);
    $('#modalTitle').html(event.event.title);
    $('#modalLocation').html(event.event.extendedProps.location);
    $('#modalStartDate').html(moment(startdate.getTime()).format("DD-MM-YYYY"));
    $('#modalStartTime').html(moment(starttime.getTime()).format("hh:mm A"));

    //moment("01:15:00 PM", "h:mm:ss A").format("HH:mm:ss")

    $('#modalEndDate').html(moment(enddate.getTime()).format("DD-MM-YYYY"));
    $('#modalEndTime').html(moment(endtime.getTime()).format("hh:mm A"));
    $('#fullCalModal').modal();
},

My updated code is below

 <script>

 document.addEventListener('DOMContentLoaded', function() {
 var calendarEl = document.getElementById('calendar');

 var calendar = new FullCalendar.Calendar(calendarEl, {

 plugins: [ 'interaction', 'dayGrid' ],
 defaultDate: '<cms:date format='Y-m-d' />',
 editable: true,
 eventLimit: true, // allow "more" link when too many events
 displayEventTime: false,

 events: [
 <cms:pages masterpage='events.php' show_future_entries='1'>
    {
 id: <cms:escape_json><cms:show k_page_id /></cms:escape_json>, 
 title: <cms:escape_json><cms:show k_page_title /></cms:escape_json>,
 location: <cms:escape_json><cms:show location /></cms:escape_json>,
 start: <cms:escape_json><cms:show start_date /></cms:escape_json>,
 startTime: <cms:escape_json><cms:show start_time /></cms:escape_json>,
 end: <cms:escape_json><cms:show end_date /></cms:escape_json>,
 endTime: <cms:escape_json><cms:show end_time /></cms:escape_json>
}<cms:if "<cms:not k_paginated_bottom/>">,</cms:if>
</cms:pages>
],
eventClick: function(info) {

var dateSettings = { "year": "numeric", "month": "2-digit", "day": "2-digit" 
};
var timeSettings = { "hour": "2-digit", "minute": "2-digit", "hour12": false 
};

var startdate = calendar.formatDate(info.event.start,  dateSettings);
var startTime = calendar.formatDate(info.event.start,  timeSettings);

var enddate = calendar.formatDate(info.event.end || startdate, 
dateSettings);
var endTime = calendar.formatDate(info.event.end, timeSettings);

$('#modalID').html(info.event.id);
$('#modalTitle').html(info.event.title);
$('#modalLocation').html(info.event.extendedProps.location);
$('#modalStartDate').html(startdate);
$('#modalStartTime').html(startTime);
$('#modalEndDate').html(enddate);
$('#modalEndTime').html(endTime);
$('#fullCalModal').modal();
},

eventTextColor: '#FFFFFF',

});

calendar.render();
});

</script>

You are really tying yourself in knots here.

1) As per the documentation event.start and event.end are already Date objects. There is really no need to parse them into another Date or make them into MomentJS objects again. Just use them as they already are, and format them as needed using fullCalendar's provided date formatting functionality .

2) The event object (again see docs above) does not have a separate "startTime" or "endTime" property...the whole date and time is stored within start and end . I'm not sure where you got the idea that separate properties exist? This has never been the case in any version of fullCalendar.

3) You need to deal with the case where the event's end property might be null, so there's a little bit of extra logic to handle that, and just use the start date/time instead:

eventClick: function(info) {
  var dateSettings = { "year": "numeric", "month": "2-digit", "day": "2-digit" };
  var timeSettings = { "hour": "2-digit", "minute": "2-digit", "hour12": false };

  var startdate = calendar.formatDate(info.event.start,  dateSettings);
  var starttime = calendar.formatDate(info.event.start,  timeSettings);

  var enddate; 
  var endtime; 
  if (info.event.end != null) {
    enddate = calendar.formatDate(info.event.end, timeSettings );
    endtime = calendar.formatDate(info.event.end, dateSettings );
  } 
  else {
    enddate = startdate;
    endtime = starttime;
  }

  $('#modalID').html(info.event.id);
  $('#modalTitle').html(info.event.title);
  $('#modalLocation').html(info.event.extendedProps.location);
  $('#modalStartDate').html(startdate);
  $('#modalStartTime').html(starttime);
  $('#modalEndDate').html(enddate);
  $('#modalEndTime').html(endtime);
  $('#fullCalModal').modal();
},

Demo: https://codepen.io/anon/pen/eapjWN?editors=1010

NB If, as it appears, you are using fullCalendar v4, then your callback arguments for eventClick are wrong - again see the relevant documentation - yours look like the v3 arguments. jsEvent and view are no longer supplied separately, instead they are supplied within the info object which is now the first argument. And talking of that, calling the first argument event is really a misnomer, since it contains more than that. The fact you've ended up writing event.event... tells you you've got a naming problem. Better to name it more meaningfully. In the interest of code quality, my example above makes those changes in addition to the ones necessary to solve the actual problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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