javascript fullCalendar:如何在 clientEvents 中按日期过滤?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20703806/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 19:03:26  来源:igfitidea点击:

fullCalendar: How to filter by date in clientEvents?

javascriptjqueryfullcalendar

提问by Thiru

To avoid overlapping events, I use this function:

为了避免重叠事件,我使用了这个函数:

    function isOverlapping(event){
    var array = $('#calendar').fullCalendar('clientEvents');
    for(i in array){
      if(array[i].id != event.id){
        if(array[i].allDay || event.allDay){
          if(array[i].start.getDate() == event.start.getDate()){
            if(array[i].start.getMonth() == event.start.getMonth()){
              return true;
            }
          }
        }
        else{
          if(event.end > array[i].start && event.start < array[i].end){ return true;}
        }
      }
    }
      return false;
  }

If my calendar has lots of events, the function is quite slow, so as to increase the speed, i thought it would be nice to compare only the events in the current view, so I wished I could use the clientEventsfilter function like this:

如果我的日历有很多事件,这个功能很慢,为了提高速度,我认为只比较当前视图中的事件会很好,所以我希望我可以clientEvents像这样使用过滤功能:

    var array = $('#calendar').fullCalendar('clientEvents', function(events){ return (event.start >= view_start && view_end > event.start)});

But this returns all the events.

但这会返回所有事件。

Note:I have declared view_start& view_endas global variables and calculated them in the ViewDisplay like this:

注意:我已将view_start&声明view_end为全局变量并在 ViewDisplay 中像这样计算它们:

 view_start = view.visStart;
 view_end = view.visEnd;     

How can I get the events which are visible in the current view.

如何获取当前视图中可见的事件。

采纳答案by Henrique C.

There are no built in filter in fullcalendar, you have to make them by yourself. The only thing you can do is to do what you are doing :) ...using your own filter functions.

fullcalendar 中没有内置过滤器,您必须自己制作它们。你唯一能做的就是做你正在做的事情:) ...使用你自己的过滤功能。

This is what i do to filter them, but this is made client side, so the filtering will be made client side... This can be a problem, but for my particular solution its enought.

这就是我为过滤它们所做的,但这是在客户端进行的,因此过滤将在客户端进行...这可能是一个问题,但对于我的特定解决方案来说已经足够了。

This is my example:

这是我的例子:

function getCalendarEvents(filter){

        var events = new Array();      
            if(filter == null)
            {
                events = calendar.fullCalendar('clientEvents');
            }
            else
            {
                events = getEventsByFilter(filter);                 
            }           
        return events;                 
    }

The filter events function:

过滤事件函数:

function getEventsByFilter(filter){        
        var allevents = new Array();
        var filterevents = new Array();
        allevents = getCalendarEvents(null);

        for(var j in allevents){ 
            if(allevents[j].eventtype === filter)
            {
                filterevents.push(allevents[j]);
            }
        }           

        return filterevents;
    }

回答by Stoinov

The problem with the oneliner you wanted to use is that the event.start is actually an object. I was able to use something similar like this:

您想要使用的 oneliner 的问题在于 event.start 实际上是一个对象。我能够使用类似的东西:

moment(calEvent.start).format('YYYY-MM-DD')

So for your case try:

因此,对于您的情况,请尝试:

var array = $('#calendar').fullCalendar('clientEvents', function(events){ return (moment(events.start).format('YYYY-MM-DD') >= view_start && view_end > moment(events.start).format('YYYY-MM-DD'))});

回答by Guest

Most functions in fullCalendar use filters.

fullCalendar 中的大多数函数都使用过滤器。

These filters may be a number and gets filtered by ID or can be functions that return true or false depending on what are you filtering.

这些过滤器可能是一个数字并按 ID 过滤,也可能是根据您过滤的内容返回 true 或 false 的函数。

var array = $('#calendar').fullCalendar('clientEvents',function(event)
    if(event.start > '1111-11-11')return true;
    else return false;
);