简体   繁体   English

如何将数组放入事件中:[..]

[英]How to put array in events: [..]

I am trying to put my variable array in the events: [..] part.我正在尝试将我的变量数组放入 events: [..] 部分。 However, this does not work.但是,这不起作用。 In the var ar is the selfmade array with the date loop.在 var ar 中是带有日期循环的自制数组。 The var needs to be set in the events: [..] option. var 需要在 events: [..] 选项中设置。

    <script>
    "use strict";

    /*let date = new Date();
    let day = date.getDay();
    let month = date.getMonth() + 1;
    let year = date.getFullYear();*/

    document.addEventListener('DOMContentLoaded', function () {

        var getDates = function (startDate, endDate) {
            var dates = [],
                currentDate = startDate,
                addDays = function (days) {
                    var date = new Date(this.valueOf());
                    date.setDate(date.getDate() + days);
                    return date;
                };
            while (currentDate <= endDate) {
                dates.push(currentDate);
                currentDate = addDays.call(currentDate, 1);
            }
            return dates;
        };

        var dates = getDates(new Date('2019-11-01'), new Date('2019-11-02'));

        dates.forEach(function (date) {
            var day = ("0" + date.getDate()).slice(-2);
            var month = date.getMonth() + 1;
            var year = date.getFullYear();

            var str = "title: \'Max\'| start: \'" + year + "-" + month + "-" + day + "\'| color: \'#ff7d7d\'";
            let ar = str.split('|');
        });
            events: [
                {title: 'Frans', start: '2019-11-08', color: '#ff7d7d'},
            ]
</script>

It's a bit unclear what is going on here.有点不清楚这里发生了什么。 In the code you've shown, events: [... ] will do nothing.在您显示的代码中, events: [... ]不会做任何事情。 It is supposed to be an option you give to fullCalendar when you create the calendar.它应该是您在创建日历时提供给 fullCalendar 的选项。 Where is your code to create the calendar?创建日历的代码在哪里?

And the line var str =... is some sort of horror show. var str =...这一行是某种恐怖表演。 It looks like a mangled attempt to create some JSON.它看起来像是创建一些 JSON 的失败尝试。 But a) that's a terrible way to create JSON - JSON.stringify exists for a good reason.但是 a) 这是创建 JSON 的糟糕方法 - JSON.stringify的存在是有充分理由的。 And b) fullCalendar doesn't want JSON anyway, it wants an actual array of JS objects, which you haven't created. b) fullCalendar 无论如何都不想要 JSON ,它想要一个实际的 JS 对象数组,而您还没有创建它。

(NB You can connect it to an API which returns a JSON string, and fullCalendar will download the JSON and then parse it into an array , but if you're supplying the data through code, then you should supply an array directly.) (NB You can connect it to an API which returns a JSON string, and fullCalendar will download the JSON and then parse it into an array , but if you're supplying the data through code, then you should supply an array directly.)

What I think you're probably trying to do is something like this:认为您可能正在尝试做的是这样的事情:

document.addEventListener("DOMContentLoaded", function() {
  var getDates = function(startDate, endDate) {
    var dates = [],
      currentDate = startDate,
      addDays = function(days) {
        var date = new Date(this.valueOf());
        date.setDate(date.getDate() + days);
        return date;
      };
    while (currentDate <= endDate) {
      dates.push(currentDate);
      currentDate = addDays.call(currentDate, 1);
    }
    return dates;
  };

  var dates = getDates(new Date("2019-11-01"), new Date("2019-11-02"));
  var eventList = [];

  dates.forEach(function(date) {
    eventList.push({
      title: "Max",
      start: date,
      color: "#ff7d7d"
    });
  });

  var calendarEl = document.getElementById("calendar");
  var calendar = new FullCalendar.Calendar(calendarEl, {
    plugins: ["interaction", "dayGrid", "timeGrid"],
    header: {
      left: "prev,next today",
      center: "title",
      right: "dayGridMonth,timeGridWeek,timeGridDay"
    },
    events: eventList
  });

  calendar.render();
});

This creates an empty array called eventsList and then within the dates.ForEach loop creates an object for each date, and adds it to the event list.这将创建一个名为eventsList的空数组,然后在dates.ForEach循环中为每个日期创建一个 object,并将其添加到事件列表中。 That list is then assigned as the "events" property in the fullCalendar options.然后将该列表分配为 fullCalendar 选项中的“事件”属性。

Live demo: https://codepen.io/ADyson82/pen/vYYrLva现场演示: https://codepen.io/ADyson82/pen/vYYrLva


PS Having said all that, it looks like you are trying to create a sequence of identical events for a series of dates. PS 说了这么多,看起来您正在尝试为一系列日期创建一系列相同的事件。 It's perhaps worth pointing out that since version 4, fullCalendar now supports recurring events , which can make your looping redundant.或许值得指出的是,从第 4 版开始,fullCalendar 现在支持重复事件,这会使您的循环变得多余。

You could implement the same thing as your code above much more simply using recurring events as follows:您可以更简单地使用重复事件来实现与上面的代码相同的东西,如下所示:

document.addEventListener("DOMContentLoaded", function() {
  var calendarEl = document.getElementById("calendar");
  var calendar = new FullCalendar.Calendar(calendarEl, {
    plugins: ["interaction", "dayGrid", "timeGrid"],
    header: {
      left: "prev,next today",
      center: "title",
      right: "dayGridMonth,timeGridWeek,timeGridDay"
    },
    events: [{
      title: "Max",
      color: "#ff7d7d",
      startRecur: new Date("2019-11-01"),
      endRecur: new Date("2019-11-03") //03 because recurrence end dates are exclusive, as per docs
    }]
  });

  calendar.render();
});

Demo: https://codepen.io/ADyson82/pen/oNNyYaV演示: https://codepen.io/ADyson82/pen/oNNyYaV

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

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