【问题标题】:Insert an event with a different date other than the current date on mysql with Fullcalendar使用 Fullcalendar 在 mysql 上插入与当前日期不同日期的事件
【发布时间】:2015-03-20 22:51:07
【问题描述】:

我实现了 Fullcalendar,它显示了 mysql 表中的事件(标题+日期时间),还通过单击日历中的一天插入新事件。

问题在于它只在当天插入事件,即使用户点击了未来的日期或月份。我希望用户能够像现在一样插入未来的事件。

显示事件的完整日历脚本 (events.php) 并通过单击日期(标题)将此信息发送到“add_event.php”

$(document).ready(function() {

    var calendar = $('#calendar').fullCalendar({
        editable: true,
        events: "http://localhost/test-fullcalendar/php/eventos.php",
        lang: "es",
        selectable: true,
        selectHelper: true,

        select: function(start, end, allDay) {
            var title = prompt('Evento a insertar:');
            if (title) {

                start = moment(event.start).format('YYYY-MM-DD HH:mm:ss');
                end = moment(event.end).format('YYYY-MM-DD HH:mm:ss');

                 $.ajax({
                     url: 'php/add_evento.php',
                     type: 'POST',
                     data: {title: title, start: start, end: end},
                     success: function(json) {
                        alert("Evento insertado");
                        console.log("Enviando: ", title, start, end);
                     }
                 });
                 calendar.fullCalendar('renderEvent',
                 {
                     title: title,
                     start: start,
                     end: end,
                     allDay: allDay
                 },
                 true
                 );
            }
            calendar.fullCalendar('unselect');
        }

    });

});

【问题讨论】:

    标签: javascript jquery ajax fullcalendar


    【解决方案1】:

    您遇到的问题是您使用不存在的日期 (event.start) 覆盖所选日期,因此默认为现在。当您将事件添加到日历时,它将使用覆盖的值而不是传递给 select 回调的值。

    之前:

                start = moment(event.start).format('YYYY-MM-DD HH:mm:ss');
                end = moment(event.end).format('YYYY-MM-DD HH:mm:ss');
    
                 $.ajax({
                     url: 'php/add_evento.php',
                     type: 'POST',
                     data: {title: title, start: start, end: end},
                     success: function(json) {
                        alert("Evento insertado");
                        console.log("Enviando: ", title, start, end);
                     }
                 });
    

    之后:

                start_ajax = moment(start).format('YYYY-MM-DD HH:mm:ss');
                end_ajax = moment(end).format('YYYY-MM-DD HH:mm:ss');
    
                 $.ajax({
                     url: 'php/add_evento.php',
                     type: 'POST',
                     data: {title: title, start: start_ajax, end: end_ajax},
                     success: function(json) {
                        alert("Evento insertado");
                        console.log("Enviando: ", title, start_ajax, end_ajax);
                     }
                 });
    

    【讨论】:

    • 我添加了 event´´ before (start)´´ 和 (end)´´ because without event´´ 它无法正确显示小时格式,例如在控制台中仅显示 00:00:00,单击 3march 事件:the-title 2015-03-03 00:00:00 2015-03-04 00:00:00。我阅读了 Fullcalendar 的文档,HH:mm:ss 是获得我的 mysql 表(DATETIME 列)所需的小时格式的唯一方法。感谢您的回复
    • select 回调中没有传递event 变量,因此您所做的是尝试访问不存在的变量,因此转换为 DATETIME 是在当前日期而不是实际日历上完成的选定的日期。 event 变量传递给其他 Fullcalendar 回调,但不传递给 select
    • 我明白你在解释什么,但我看不出问题的解决方案在哪里。通过添加event 就像传递给select 函数的变量一样select: function(start, end, event, allDay) 它只返回相同的而不是确切事件/标题的日期。谢谢
    • 阅读文档fullcalendar.io/docs/selection/select_callback。它清楚地表明startend 保存选择时间。使用那些将数据发送到服务器。
    【解决方案2】:

    您可以通过

    获取当前选择的日期
    $("#calendar").fullCalendar('getDate');
    

    它会返回一个 moment.js 日期对象

    【讨论】:

      猜你喜欢
      • 2022-12-01
      • 2013-11-23
      • 2015-02-28
      • 1970-01-01
      • 1970-01-01
      • 2011-12-20
      • 1970-01-01
      • 2017-04-01
      • 1970-01-01
      相关资源
      最近更新 更多