【问题标题】:Events not displayed in full calendar未在完整日历中显示的事件
【发布时间】:2020-07-15 01:59:37
【问题描述】:

我一直在尝试使用 fullcalendar 显示一些事件,但它不起作用。 我一直在谷歌搜索并尝试不同的值,但它从未奏效。 例如我试过这个:

                                events: [
                                {
                                    title  : 'event1',
                                    start  : '2020-04-01'
                                },
                                {
                                    title  : 'event2',
                                    start  : '2020-04-05',
                                    end    : '2020-04-07'
                                },
                                {
                                    title  : 'event3',
                                    start  : '2020-04-03T12:30:00',
                                    allDay : false // will make the time show
                                }
                            ],

还有这个:

                                events: [
                                {"id":"1","start":"2020-04-14T13:00:00","end":"2020-04-14T1‌​6:00:00","title":"Pe‌​r Hour: 11.00,Per Mile: 11.00"},
                                {"id":"2","start":"2020-04-15T13:00:00","end":"2020-04-15T16‌​:00:00","title":"Per Hour: 11.00,Per Mile: 11.00"},
                                {"id":"3","start":"2020-04-16T13:00:00","end":"2020-04-16T16‌​:00:00","title":"Per Hour: 11.00,Per Mile: 11.00"}
                            ]

在其他测试中。我测试的所有价值观都是在互联网上找到的,来自人们分享他们的经验,但对我来说,它们都不起作用。

知道这些值有什么问题吗?

然后我将编写从我的数据库中检索数据的脚本,但首先我必须让它使用硬编码值。


我会尽量说清楚: 我从完整的日历演示中复制/粘贴了这段代码,它工作正常,正确显示事件:

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

var calendar = new FullCalendar.Calendar(calendarEl, {
    schedulerLicenseKey: 'GPL-My-Project-Is-Open-Source',
  plugins: [ 'resourceTimeGrid' ],
  timeZone: 'UTC',
  defaultView: 'resourceTimeGridFourDay',
  datesAboveResources: true,
  header: {
    left: 'prev,next',
    center: 'title',
    right: 'resourceTimeGridDay,resourceTimeGridFourDay'
  },
  views: {
    resourceTimeGridFourDay: {
      type: 'resourceTimeGrid',
      duration: { days: 4 },
      buttonText: '4 days'
    }
  },
  resources: [
    { id: 'a', title: 'Room A' },
    { id: 'b', title: 'Room B' }
  ],
  events: 'https://fullcalendar.io/demo-events.json?with-resources=2'
});

calendar.render();  });

然后我评论了从 fullcalendar 网站检索事件的行并添加了我自己的硬编码事件,如下所示:

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

var calendar = new FullCalendar.Calendar(calendarEl, {
    schedulerLicenseKey: 'GPL-My-Project-Is-Open-Source',
  plugins: [ 'resourceTimeGrid' ],
  timeZone: 'UTC',
  defaultView: 'resourceTimeGridFourDay',
  datesAboveResources: true,
  header: {
    left: 'prev,next',
    center: 'title',
    right: 'resourceTimeGridDay,resourceTimeGridFourDay'
  },
  views: {
    resourceTimeGridFourDay: {
      type: 'resourceTimeGrid',
      duration: { days: 4 },
      buttonText: '4 days'
    }
  },
  resources: [
    { id: 'a', title: 'Room A' },
    { id: 'b', title: 'Room B' }
  ],
  //events: 'https://fullcalendar.io/demo-events.json?with-resources=2'
  events: [
                            {
                                title  : 'event1',
                                start  : '2020-04-01'
                            },
                            {
                                title  : 'event2',
                                start  : '2020-04-05',
                                end    : '2020-04-07'
                            },
                            {
                                title  : 'event3',
                                start  : '2020-04-03T12:30:00',
                                allDay : false // will make the time show
                            }
    ]
});

calendar.render();  });

当我这样做时,事件不会显示。

【问题讨论】:

  • 这两组事件都没有问题。演示 1:codepen.io/ADyson82/pen/jOPoqjq 演示 2:codepen.io/ADyson82/pen/OJVYNde。如果您有问题,那是由您未在此处显示的内容引起的。你检查过控制台错误之类的东西吗?您的日历是否确实专注于正确的月份和年份?您是否尝试将启用资源的视图与没有资源 ID 的事件一起使用?出现问题的原因可能有多种,但我们无法根据您目前提供的信息来诊断。

标签: javascript fullcalendar fullcalendar-4 fullcalendar-scheduler


【解决方案1】:

感谢您的编辑。

现在原因更清楚了 - 您正在使用启用资源的视图,但您的事件没有任何匹配的资源 ID。那么 fullCalendar 应该如何知道将它们放置在哪个资源上,你认为呢?它无法决定,这意味着它无法显示事件。

您可以将活动分配给不同的资源,具体取决于它们所属的位置。比如这样:

events: [
  {
    title  : 'event1',
    start  : '2020-04-01',
    resourceId: 'b'
  },
  {
    title  : 'event2',
    start  : '2020-04-05',
    end    : '2020-04-07',
    resourceId: 'a'
  },
  {
    title  : 'event3',
    start  : '2020-04-03T12:30:00',
    allDay : false, // will make the time show
    resourceIds: ['a', 'b']
  }
]

演示:https://codepen.io/ADyson82/pen/LYVoZvK?editable=true&editors=001

这里是相关的完整日历文档页面:Associating Events with Resources

【讨论】:

  • 最后一条评论...我在尝试将 php 变量传递给 javascript 时遇到问题...我将值插入到 HTML 输入中,然后在 Javascript 变量中检索它,如下所示:varplanned_events = document.getElementById('planned_events').value;但是planned_events 值包含奇怪的字符并且它不起作用...
  • 我想我必须找到一种方法来获取 json 值,然后将其转换为 javascript 数组,它应该可以工作。
  • 您可以这样做,但更好的方法是使用一个完全独立的服务器端脚本来返回事件数据,然后您可以将events 定义为 URL,就像在全日历演示。因此,如果您有一个 PHP 服务器,您可以编写类似events: "http:///www.example.com/events.php" 的东西,其中 events.php 将是一个 PHP 脚本,它接受“开始”和“结束”日期参数(fullCalendar 自动提供给它),并返回一个数组JSON 格式的事件。请参阅fullcalendar.io/docs/events-json-feed 了解更多信息。
  • 如果您使用的是 Symfony 之类的框架(我似乎从之前的问题中记得您是这样的),那么您当然会定义一个路由和一个控制器中的函数将返回 JSON。相同的概念,只是不同的实现方法。
  • 实际上,我通过将数据传递给 json 格式的输入来实现它,但你的想法要好得多。我仍在努力保存数据,一旦完成,我将更改这部分,因为我的代码不是很好。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多