简体   繁体   English

如何根据 FullCalendar V5 上的选择值过滤事件?

[英]How to filter events based on select value on FullCalendar V5?

I've just tried FullCalendar plugin for the first time, so i went with Version 5.我第一次尝试了 FullCalendar 插件,所以我选择了版本 5。

I just want to filter visible events by selecting value in a select dropdown.我只想通过在选择下拉列表中选择值来过滤可见事件。

I can't seem to find a simple and efficient way to do this with V5.我似乎无法找到一种简单有效的方法来使用 V5 来做到这一点。

Many ressources that i found on internet talk about using rererenderEvents or refetchEvents but it refers to previous version of plugin and this is not working here.我在互联网上找到的许多资源都在谈论使用 rererenderEvents 或 refetchEvents,但它指的是以前版本的插件,这在这里不起作用。

I found a workaround by looping through the events and changing the visibility with setProp, but it doesn't look very good.我通过循环遍历事件并使用 setProp 更改可见性找到了一种解决方法,但它看起来不太好。 I have a lot of events and the for loop takes several seconds (code in comment in snippet)我有很多事件,for 循环需要几秒钟(代码片段中的注释)

Thank you very much for your advice and help非常感谢您的建议和帮助

 document.addEventListener('DOMContentLoaded', function() { let currentDayDate = new Date().toISOString().slice(0, 10); let calendarEl = document.getElementById('calendar'); let calendar = new FullCalendar.Calendar(calendarEl, { initialView: 'timeGridWeek', firstDay: 1, hiddenDays: [ 0, 6 ], slotMinTime: '07:00', slotMaxTime: '20:00', allDaySlot: false, headerToolbar: { left: 'prev,today,next', center: 'title', right: 'timeGridDay,timeGridWeek' }, views: { timeGridWeek: { type: 'timeGrid', duration: { days: 2 }, buttonText: '2 day' } }, events: [{ "title": "Marc", "start": currentDayDate + " 07:30", "end": currentDayDate + " 08:30", "userId": "1" }, { "title": "Tom", "start": currentDayDate + " 09:30", "end": currentDayDate + " 10:30", "userId": "2" }, { "title": "David", "start": currentDayDate + " 11:30", "end": currentDayDate + " 12:30", "userId": "3" }] }); calendar.render(); // $( document ).ready(function() { // $('#selector').on('change',function(){ // var events = calendar.getEvents(); // for (var i = events.length - 1; i >= 0; i--) { // if ($("#selector").val() == events[i].extendedProps.userId || $("#selector").val() == "all") { // events[i].setProp("display", "block"); // } // else { // events[i].setProp("display", "none"); // } // } // }); // }); });
 <link href="https://cdn.jsdelivr.net/npm/fullcalendar@5.10.1/main.min.css" rel="stylesheet"/> <script src="https://cdn.jsdelivr.net/npm/fullcalendar@5.10.1/main.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <select id="selector"> <option value="all">All</option> <option value="1">Marc</option> <option value="2">Tom</option> <option value="3">David</option> </select> <div id="calendar"> </div>

There are a couple of ways to approach filtering with fullCalendar:有几种方法可以使用 fullCalendar 进行过滤:

  1. Filter events out as they are rendered在渲染事件时过滤掉事件

  2. Filter the data at source, before it arrives in fullCalendar.在数据到达 fullCalendar 之前从源过滤数据。

The approach you've seen for previous versions of fullCalendar filtering the events are they are rendered is still applicable - and that's what I'll demonstrate here.您在之前版本的 fullCalendar 中看到的过滤事件的方法仍然适用 - 这就是我将在此处演示的内容。 However because there's no rerenderEvents equivalent in v5, you need to use refetchEvents to make fullCalendar fetch and draw the events again.但是,因为 v5 中没有等效的rerenderEvents ,所以您需要使用refetchEvents来获取 fullCalendar 并再次绘制事件。 The render function also won't achieve it. render功能也不会实现它。

However, refetchEvents won't work unless you have a dynamic event source.但是,除非您有动态事件源,否则refetchEvents将不起作用。 Currently you have a static list of events.目前您有一个静态的事件列表。 Consequently there's因此有

Therefore I've written a version with a nominal function-based event source - it returns the exact same events every time, but in practice of course you wouldn't do that - you'd write code to make it request/generate a dynamic set of events based on the dates supplied, or alternativey you'd use a JSON event source where the event data is fetched directly from a URL, and the server controls what is returned.因此,我编写了一个带有名义上的基于函数的事件源的版本——它每次都返回完全相同的事件,但在实践中你当然不会那样做——你会编写代码来让它请求/生成动态基于提供的日期的一组事件,或者您可以使用JSON 事件源,其中直接从 URL 获取事件数据,服务器控制返回的内容。

Also in v5 the eventRender callback is replaced by the event render hooks - for our purpose the eventDidMount callback will be suitable for what we want to do.同样在 v5 中, eventRender回调被事件渲染钩子取代 - 为了我们的目的, eventDidMount回调将适合我们想要做的事情。 You can filter each event out as it's rendered (as opposed to your example code where you tried to fetch all the events and update their properties.)您可以在呈现每个事件时将其过滤掉(与您尝试获取所有事件并更新其属性的示例代码相反。)

NB I haven't used jQuery at all here, but of course you're welcome to use it wherever you want to in place of the native DOM functions.注意,我在这里根本没有使用过 jQuery,但是当然欢迎您在任何地方使用它来代替本机 DOM 函数。

 document.addEventListener('DOMContentLoaded', function() { let currentDayDate = new Date().toISOString().slice(0, 10); let selector = document.querySelector("#selector"); let calendarEl = document.getElementById('calendar'); let calendar = new FullCalendar.Calendar(calendarEl, { initialView: 'timeGridWeek', firstDay: 1, hiddenDays: [0, 6], slotMinTime: '07:00', slotMaxTime: '20:00', allDaySlot: false, headerToolbar: { left: 'prev,today,next', center: 'title', right: 'timeGridDay,timeGridWeek' }, views: { timeGridWeek: { type: 'timeGrid', duration: { days: 2 }, buttonText: '2 day' } }, eventDidMount: function(arg) { let val = selector.value; if (!(val == arg.event.extendedProps.userId || val == "all")) { arg.el.style.display = "none"; } }, events: function (fetchInfo, successCallback, failureCallback) { successCallback([{ "title": "Marc", "start": currentDayDate + " 07:30", "end": currentDayDate + " 08:30", "userId": "1" }, { "title": "Tom", "start": currentDayDate + " 09:30", "end": currentDayDate + " 10:30", "userId": "2" }, { "title": "David", "start": currentDayDate + " 11:30", "end": currentDayDate + " 12:30", "userId": "3" }]); } }); calendar.render(); selector.addEventListener('change', function() { calendar.refetchEvents(); }); });
 <link href="https://cdn.jsdelivr.net/npm/fullcalendar@5.10.1/main.min.css" rel="stylesheet" /> <script src="https://cdn.jsdelivr.net/npm/fullcalendar@5.10.1/main.min.js"></script> <select id="selector"> <option value="all">All</option> <option value="1">Marc</option> <option value="2">Tom</option> <option value="3">David</option> </select> <div id="calendar"> </div>

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

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