简体   繁体   English

使用 FullCalendar 显示事件数组

[英]Showing array of events with FullCalendar

I have the following script using FullCalendar:我有以下使用 FullCalendar 的脚本:

  var calendarEl = document.getElementById('calendar');

  var bookingsEl = document.getElementById('bookings');

  var bookings = bookingsEl.innerText;
  var bookings = "["+bookings+"]"

  console.log(bookings);

  if(calendarEl) {

    var calendar = new Calendar(calendarEl, {
      plugins: [ dayGridPlugin ],
      firstDay: 1,
      height: 'auto',
      events: bookings,
      header: {
        left:   'prev',
        center: 'title',
        right:  'next'
      }
    });

  calendar.render();

}

The console.log for bookings looks like this: bookingsconsole.log如下所示:

[{start:"2020-08-12",end:"2020-08-25",rendering:"background"},{start:"2020-09-11",end:"2020-09-30",rendering:"background"},{start:"2020-08-26",end:"2020-08-28",rendering:"background"}]

If I copy and paste this for the events setting it works fine, but as it is I am getting no events showing in the calendar.如果我将其复制并粘贴到events设置中,它可以正常工作,但是我没有在日历中显示任何事件。

I am at a loss what to do next to get this to work.我不知道下一步该怎么做才能让它发挥作用。 Any help greatly appreciated.非常感谢任何帮助。

Right, so the issue is that the "events" attribute expects a javascript array, and you are passing it a string value that looks like a stringyfied array when you look at the console output.是的,所以问题是“事件”属性需要一个 javascript 数组,并且当您查看控制台 output 时,您传递给它的字符串值看起来像一个字符串化数组。

So you need to convert your string into a Javascript array of objects;因此,您需要将字符串转换为 Javascript 对象数组; from what you've shown, "bookingsEl.innerText" contains a list of objects in string form.从您所展示的内容来看,“bookingsEl.innerText”包含字符串形式的对象列表。 So I would try this:所以我会试试这个:

var bookings = JSON.parse("["+bookings+"]");

This will parse a JSON array into it's javascript equivalent object.这会将 JSON 数组解析为 javascript 等效的 object。 NOTE: your JSON string is not standardized: the object attributes need to be quoted too, ie {"start":"2020-08-12","end":"2020-08-25","rendering":"background"}注意:您的 JSON 字符串未标准化:object 属性也需要引用,即 {"start":"2020-08-12","end":"2020-08-25","rendering":"background "}

BONUS An array and the string representation of an array look different in the javascript console.奖励 在javascript控制台中,数组和数组的字符串表示形式看起来不同。 As a test, try this:作为测试,试试这个:

var bookings = JSON.parse("["+bookings+"]");
console.log("This is a javascript array", bookings);
console.log("This is a string representation of an array", "[" + bookingsEl.innerText + "]");

You should see they look quite different in the console...您应该看到它们在控制台中看起来完全不同...... 在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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