javascript - 更新 fullcalendar 事件中传递的变量

标签 javascript jquery callback fullcalendar fullcalendar-3

我有一个完整日历,我可以在其中根据选择输入(多个)过滤事件结果:

<select id="ids" name="ids[]" class="form-control" multiple>
    <option value="1" selected> Option 1</option>
    <option value="2"> Option 2</option>
    <option value="3"> Option 3</option>
</select>

<button type="button" id="btn-filter">Apply</button>

默认 View 选择了选项 1。加载页面时,fullcalendar 可以正确检索我的选择。

如果我选择其他选项(例如选项 2)并单击按钮过滤器、全日历刷新,但仍然仅传递选项 1(我在调试控制台 Firefox 中看到它):

火狐控制台:

array_ids[]:    1
start:  2019-06-01T00:00:00
end:    2019-07-01T00:00:00

这是fullcalendar的一段代码

events: {
            url: './get-events.php',
            cache: false,
            type: 'POST',
            data: {
                array_ids: $("#ids").val()
            },
            error: function () {
                alert('there was an error while fetching events!');
            },
        },

这是刷新日历的按钮:

  $('#btn-filter').on('click', function(e) {

        e.preventDefault();

        $('#calendar').fullCalendar('refetchEvents');

    });

似乎 fullcalendar 在页面加载时仅存储第一个值:array_ids: $("#ids").val()

编辑:好的,我已经这样修改了:

   events: function (start, end, timezone, callback) {

           $.ajax({
             url: './get-events.php',
             data: { "array_ids": $("#ids").val(), "start": moment(start).format(), "end": moment(end).format() },
             cache: false,
             type: 'POST',
             success: function (data) {
                $('#calendar').fullCalendar('rerenderEvents');
             }
           });

        },

现在所有选择选项都已通过...但是...日历停止渲染结果...仍然为空...但是观看控制台 firefox 我看到数据已正确收集...只是没有在全日历上显示!

最佳答案

原因是您将静态数据传递给 fullCalendar。当你写的时候

data: {
  array_ids: $("#ids").val()
},

您向 fullCalendar 赋予 $("#ids").val() 当时的值 - 即您创建日历的那一刻。您只需传递该值一次即可。 fullCalendar 接收并存储该值,并在每次联系您的服务器时重新使用它。它不知道该值来自哪里,也无法更新它。这就是 JavaScript 的工作方式(实际上也是其他大多数编程语言) - 变量通常按值传递,而不是按引用传递(并且在任何情况下,这里都没有引用,因为你只是直接传递函数的输出)。

根据 documentation (在标题为“动态数据参数”的部分中),如果您希望 fullCalendar 在每次联系服务器时检查新的数据值,您需要为其提供一种方法。你这样做的方式是这样的:

不要为数据传递一个简单的值,而是为 fullCalendar 提供一个回调函数。这意味着 fullCalendar 每次都可以运行该函数,并且它将根据当前数据返回一个新值。

data: function() { // a function that returns an object
  return {
    array_ids: $("#ids").val()
  };
}

关于javascript - 更新 fullcalendar 事件中传递的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56985150/

相关文章:

javascript - 单击时定位数组中的项目?

javascript - jQuery XML 导出或简单 JavaScript 文件下载

javascript - 对通过 jQuery 附加 JSON 数据的 Jquery 表进行排序

javascript - 多个 $.each 中的 setTimeout() 回调

javascript - 等待 JavaScript 函数完成后再继续

javascript - "Random"数组中的对象在全部使用之前不会重复 - 使用本地存储

javascript - 在 IE8 中动态添加 tr 和 td 设计问题

javascript - 在引导模式弹出窗口中旋转文本

php - 在 codeigniter 中的 ajax 和 Controller 之间传递数据

javascript - 使用回调函数 Javascript 延迟/重复打印屏幕数组元素