【问题标题】:How to display dates in fullcalendar in different format?如何以不同格式显示完整日历中的日期?
【发布时间】:2017-12-19 11:52:36
【问题描述】:

我正在使用fullcalendar 包。我的目标是在日历中显示数据库中的约会。

根据他们的文档,我必须使用events() 函数。

我的收藏 (Requests),我有 title 和 start 属性。 start 属性存储如下:

start: 'December 19, 2017 at 9:43 PM'

我现在被困在这里:

Template.appointments.onRendered( () => {
  $( '#calendar' ).fullCalendar({
    events( start, end, timezone, callback ) {


      });
    }
  });
});

我应该如何检索日期和时间并将其显示在日历中?

更新:

这是我尝试过的:

Template.appointments.onRendered( () => {
  $( '#calendar' ).fullCalendar({
    events( start, end, timezone, callback ) {
        let data = Requests.find().fetch().map( ( event ) => {
            event.start = new Date(event.start.replace("at", ""));
            event.end = new Date(event.start.replace("at", ""));
            return event;
        });

        if ( data ) {
            callback( data );
        }
    }
});

  Tracker.autorun( () => {
    Requests.find().fetch();
    $( '#calendar' ).fullCalendar( 'refetchEvents' );
});

});

没用,我试着把events函数里面的代码全部去掉,改成下面这样:

Template.appointments.onRendered( () => {
  $( '#calendar' ).fullCalendar({
    events( start, end, timezone, callback ) {
        console.log('WORKS!')
    }
});
});

控制台没有打印任何内容。为什么事件里面的代码没有被执行?

注意:日历已成功加载到模板中,但为空。

【问题讨论】:

  • 附带说明,您是否使用字符串来存储日期?或者是一个日期时间对象。
  • @blueren 这是我正在使用的字符串
  • 不要将日期作为字符串存储在数据库中。这是一个可怕的想法。您甚至不能针对它编写查询。 GUI 应该负责格式化日期(通常,它应该根据用户的本地习惯来做,或者它应该像 ISO 那样明确的格式)。代码中的其他任何地方都应为您的编程语言使用适当的 DateTime 类型的对象。
  • @ADyson 如果我可以将其查询回 Date 不应该没问题吗?我正在使用教程作为参考,他们也以字符串格式将其存储在那里。这里:themeteorchef.com/tutorials/…
  • 在该示例中,当您看到示例数据时,它们以 yyyy-mm-dd 格式提供日期,该格式是明确的,并且可以被 momentJS 解析(fullcalendar 用于处理日期)。但它也是一个模拟,因此不涉及数据库。他们本可以使用 momentJS 或 Date 对象,但它的可读性可能较差。通常,当您最终以 JSON 形式从服务器端检索数据时,它会作为字符串传输(因为您无法通过网络传输代码对象),但它必须是有效的、可解析的、明确的字符串,与您的不同.

标签: meteor fullcalendar


【解决方案1】:

您当前一直将日期存储为字符串。

var start_date = new Date(start.replace("at", "")); 

以上为您提供了一个等效的日期对象。我们从字符串中删除“at”,因为它会在传递给Date 构造函数时导致错误。

您的模板及其渲染功能应类似于:

template.html

<template name ="appointments">
    ....
    ....
    <div id = "calendar"></div>
    ....
    ....
</template>

template.js

    Template.appointments.onRendered(function() {

    // each time a reactive entity changes, whatever is inside tracker.autorun is run again.
    Tracker.autorun(function() {


        var eventsArray = [];
        // events.find() is reactive. Any change to the events collection will trigger the autorun to run.
        events.find().forEach(function(event) {
            eventsArray.push({
                title: event.title,
                start: event.startDate, //should be preferably in ISO format
                end: event.endDate //should be preferably in ISO format
            });

        });

        // Each time some data changes, fullCalendar must be notified again.
    $('#calendar').fullCalendar('removeEvents');
    $('#calendar').fullCalendar(eventsArray);
    $("#calendar").fullCalendar('addEventSource', eventsArray);
    $("#calendar").fullCalendar('rerenderEvents');
    });
});

我在本地测试应用程序中测试了上述内容,并且可以正常工作。

关于fullcalendar和meteor有一个很好很详细的教程here

【讨论】:

  • 感谢您的明确答复。我有一个问题,我正在使用“Requests.find()”检索事件集合我应该使用循环吗?还是有更好的做法?
  • 在不了解更多细节的情况下很难说。通常的方法是使用 find 因为它返回一个可以迭代的游标。
  • 事件功能在我用 console.log() 测试后不起作用。我更新了问题,请问您是否知道原因
  • @sourceplaze - 我了解了 fullCalendar 的一些基础知识。我已经更新了答案。该代码有一些内联 cmets 可以帮助您进行解释。
  • 感谢您的帮助和时间,我很感激。我只是在为最后一件事而苦苦挣扎,即将日历设置为每周视图。根据我在网上找到的内容,应该这样做:$("#calendar").fullCalendar({ defaultView: 'agendaWeek' }); 但它对我不起作用。你知道为什么吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-03-05
  • 2019-02-16
  • 2011-05-11
  • 1970-01-01
  • 2021-12-31
  • 1970-01-01
  • 2017-01-29
相关资源
最近更新 更多