【问题标题】:full calendar - add extra attribute to event object完整日历 - 向事件对象添加额外属性
【发布时间】:2015-12-01 17:11:41
【问题描述】:

可能是由于我缺乏理解,但我正在使用 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 中说我试试

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

它不存在。

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

【问题讨论】:

    标签: php jquery json fullcalendar


    【解决方案1】:

    FullCalendar 版本 4: 任何自定义值都将在 Event Object

    的“extendedProps”中找到
    events: [
    
                        {
                        title: 'My Title',
                        My_Custom_Value: 'non-standard Event Object field',
                        allDay: false,
                        start: 1501056000000,
                        end: 1501057800000
                        }
    ],
    
    eventRender: function(info){
    
                        console.log("_______ info _______\n");
                        console.log(info.event.extendedProps.My_Custom_Value);
    }
    

    "extendedProps
    一个普通对象,包含在解析期间指定的其他各种属性。接收显式给定的 extendedProps 散列中的属性以及其他非标准属性。"

    https://fullcalendar.io/docs/event-object

    【讨论】:

      【解决方案2】:

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

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

      来自docs

      除了上述字段外,您还可以包括自己的字段 每个事件对象中的非标准字段。 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'
              }
          });
      },
      

      【讨论】:

      • 谢谢,刚刚看了一下,.name 不可用
      • @barryspikebob 它应该可用,这是访问非标准字段的正确方法。 event 对象中有数据吗?
      • eventRender: function(event, element){ event.name } 这显示expression is not assignment or call
      • 尝试将event.name 更改为console.log(event)。这可能是由于数据未正确加载到 SQL 查询中的事件中。
      • 我已经更新了我的答案以包含我在项目中使用的内容。
      猜你喜欢
      • 1970-01-01
      • 2021-01-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多