admin管理员组文章数量:1404774
I was wondering if there was a way using Full Calendar to show/hide elements based on the className of that event.
The exact situation is I have a checkbox (let's say #daytimeEvent) and I'd like to toggle the visibility of event htat has class daytime in the array....
I tried this so far but it seems to pletely remove the events from the calendar without the possibility to get them back after....
Here is my current code:
$('#daytimeEvent').on('change', function(){
if ($(this).is(':checked'))
{
// That part doesn't work
calendar.fullCalendar('renderEvent');
}
else
{
// This seems to destroy the event without having the opportunity to toggle the visibility back after
calendar.fullCalendar('removeEvents', function(event){
return $.inArray('daytime', event.className)
});
}
}
I've crawled google and it seems like I'm not the only person with this problem but I couldn't find any solution.
I was wondering if there was a way using Full Calendar to show/hide elements based on the className of that event.
The exact situation is I have a checkbox (let's say #daytimeEvent) and I'd like to toggle the visibility of event htat has class daytime in the array....
I tried this so far but it seems to pletely remove the events from the calendar without the possibility to get them back after....
Here is my current code:
$('#daytimeEvent').on('change', function(){
if ($(this).is(':checked'))
{
// That part doesn't work
calendar.fullCalendar('renderEvent');
}
else
{
// This seems to destroy the event without having the opportunity to toggle the visibility back after
calendar.fullCalendar('removeEvents', function(event){
return $.inArray('daytime', event.className)
});
}
}
I've crawled google and it seems like I'm not the only person with this problem but I couldn't find any solution.
Share Improve this question edited Oct 26, 2016 at 18:24 Yann Chabot asked Oct 26, 2016 at 14:51 Yann ChabotYann Chabot 4,8893 gold badges43 silver badges58 bronze badges 1- 1 There are a few answers on stackoverflow stackoverflow./a/36851477/5360631, stackoverflow./a/33635079/5360631 that use checkboxes for filtering. They don't use the class names but should be easily adaptable to do so – smcd Commented Oct 26, 2016 at 21:08
3 Answers
Reset to default 5I found a solution (thanks to @smcd for the reference). The good way to do this is in the eventRender method of your calendar. I put my logic in there and then did this:
$('#myCalendar').fullCalendar({
// your init [...] then
eventRender: function(event, element) {
// Array that will store accepted classes
var eventAcceptedClasses = [];
if ($('.daytime-events-checkbox').is(':checked')){
eventAcceptedClasses.push('daytime-events');
}
if ($('.nighttime-events-checkbox').is(':checked')){
eventAcceptedClasses.push('nighttime-events');
}
displayEvent = false;
event.className.forEach(function(element){
if ($.inArray(element, eventAcceptedClasses) != -1){
displayEvent = true;
}
});
return displayEvent;
}
});
Then, the things to do is just to use the rerenderEvents method in your event listener like this below.
$('.events-filter-checkbox').on('change', function(){
calendar.fullCalendar('rerenderEvents');
});
You cannot hide and show event on demande, but you can rerender which is quite flexible and fast.
One mistake you have is how you're checking for the class existence:
$.inArray('.daytime', event.className)
There is no string .daytime
inside the className
, you should check for daytime
instead (without the .
):
$.inArray('daytime', event.className)
I think the accepted answers are a bit old so, the new approach to solve this issue, is using batchRendering
本文标签: javascriptIs there a way to toggle events visibility based on className FullCalendarStack Overflow
版权声明:本文标题:javascript - Is there a way to toggle events visibility based on className FullCalendar - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744846666a2628247.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论