完整日历-为事件对象添加额外的属性


full calendar - add extra attribute to event object

可能是由于我缺乏理解,但我使用PHP返回JSON字符串来返回事件数据。

<?php
  $json = array();
  // Query that retrieves events
  $requete = "SELECT * FROM DoctorAvailability ORDER BY id";
   try {
        $bdd = new PDO("mysql:host=$servername;dbname=$dbname",$username,$password);
      } catch(Exception $e) {
        exit('Unable to connect to database.');
      }
     // Execute the query
    $resultat = $bdd->query($requete) or die(print_r($bdd->errorInfo()));
    echo json_encode($resultat->fetchAll(PDO::FETCH_ASSOC));
 ?>

数据库中有六个字段。

Id, title, start, end, backgroundColor, name.

我想带回这个名字,尽管说例如在事件内点击我尝试

 eventClick: function (calEvent, jsEvent, view) {
           calEvent.name;

它不存在。

我知道我的理解中缺少了一些东西,但我花了很长时间在谷歌上搜索都无济于事。

您可以考虑使用eventRender,它允许您访问添加的非标准字段。

eventRender: function(event, element) {
    console.log(event.name);
}

来自文档:

除了上面的字段,您还可以包括自己的字段每个事件对象中的非标准字段。FullCalendar不会修改或删除这些字段。例如,开发人员通常包括用于回调(如eventRender)的描述字段。

更新:

这是我用来为我的事件构建数据的东西:

// construct the array
foreach ($events as $e) {
    $eventList[] = array(
        'id' => 'event-' . $e->id, // unique identifier for aligning editing window 
        'allDay' => false, // makes the times show
        'title' => $e->fullname, // shows the user's name
        'start' => $e->start_time, // their start time (start working)
        'end' => $e->end_time, // their end time (done working)
        'notes' => $e->notes ? $e->notes : '', // notes, if applicable
        'className' => 'event-' . $e->id,
        'user' => $e->user
    );
};
// echo the data, json encoded for the calendar
echo json_encode($eventList);

然后,我使用eventRender访问上面提到的特定数据。

eventRender: function(event, element) {
    element.qtip({
        content: event.notes, // This is a non-standard, custom attribute!
        position: {
            my: 'top left',
            at: 'bottom left'
        }
    });
},