【发布时间】:2019-07-11 08:47:41
【问题描述】:
我有一个完整的日历,我可以在其中根据选择输入(多个)过滤事件结果:
<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>
默认视图已选择选项 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 新事件。想一想 - rerenderEvents 只是重新显示已经存在的内容。新的事件数据位于
data...但您没有使用它。根据documentation,您必须使用提供的回调函数来告诉 fullCalendar 有关新事件的信息,这不是魔术发生的。success: function (data) { callback(data); }在这种情况下可以工作 -
...或者如果 jQuery 尚未检测到 PHP 的输出是 JSON,您可能需要
success: function (data) { callback(JSON.parse(data)); }。但无论如何,您不需要这种复杂性,因为您可以使用下面的答案更简单地解决它。
标签: javascript jquery callback fullcalendar fullcalendar-3