jquery fullcalendar事件过滤

Den*_*lov 23 jquery events filter fullcalendar

在fullcalendar中是否有任何方法可以在客户端上动态过滤事件?当我从服务器(json_encoded)获取事件时,我将自己的参数"school_id"分配给每个事件.在fullcalendar准备好之后,我想用"select"动态过滤事件.

我在页面上添加"select"元素,如下所示:

<select id='school_selector'>
      <option value='all'>All schools</option>
      <option value='1'>school 1</option>
      <option value='2'>school 2</option>
</select>
Run Code Online (Sandbox Code Playgroud)

在javascript代码中我添加:

jQuery("#school_selector").change(function(){
    filter_id = $(this).val();
    if (filter_id != 'all') {
        var events = $('#mycalendar').fullCalendar( 'clientEvents', function(event) {
        if((filter_id == 'all') ) {
            return true;
        }else{
                //what I need to write here to dynamic filter events on calendar?
        });
    }
 });
Run Code Online (Sandbox Code Playgroud)

但它不起作用.任何帮助都会很棒.谢谢.

And*_*cki 48

从fullCalendar的第2版开始,您可以使用eventRender回调来有选择地呈现事件.将此与onChange处理程序中的rerenderEvents方法调用相结合,您的事件应根据所选选项自动更新.

$('#mycalendar').fullCalendar({
    events: [
        {
            title: 'Event 1',
            start: '2015-05-01',
            school: '1'
        },
        {
            title: 'Event 2',
            start: '2015-05-02',
            school: '2'
        },
        {
            title: 'Event 3',
            start: '2015-05-03',
            school: '1'
        },
        {
            title: 'Event 4',
            start: '2015-05-04',
            school: '2'
        }
    ],
    eventRender: function eventRender( event, element, view ) {
        return ['all', event.school].indexOf($('#school_selector').val()) >= 0
    }
});

$('#school_selector').on('change',function(){
    $('#mycalendar').fullCalendar('rerenderEvents');
})
  
Run Code Online (Sandbox Code Playgroud)
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.9.0/moment.min.js"></script>
<script src="http://fullcalendar.io/js/fullcalendar-2.3.1/fullcalendar.min.js"></script>
<link rel="stylesheet" href="http://fullcalendar.io/js/fullcalendar-2.3.1/fullcalendar.min.css" />

<select id="school_selector">
  <option value="all">All</option>
  <option value="1">School 1</option>
  <option value="2">School 2</option>
</select>

<div id="mycalendar"></div>
Run Code Online (Sandbox Code Playgroud)

在上面,如果SELECT的值是'all'或者与对象的school属性匹配event,则eventRender函数将返回true并显示事件.否则在渲染过程中将跳过它.

此方法优于将过滤参数传递到事件源,因为这需要多次往返服务器.您可以一次加载所有事件,并使用eventRender 在显示时动态过滤它们.

  • 很高兴,很高兴听到@ 539.从这个例子中拿走的关键事实是eventRender()*在*渲染之前过滤*加载(和缓存)的事件*.因此,您无需加载其他事件即可更改筛选条件.当然,您可以使eventRender()中的条件尽可能复杂. (2认同)

Den*_*lov 5

有解决方案。我希望这对其他人有所帮助。

jQuery("#school_selector").change(function(){
    filter_id = $(this).val();
    if (filter_id == 'all') {
        $("#eventwrapper").fadeOut();
        $('#mycalendar').fullCalendar ('removeEvents');
        var start_source1 = {
                type:'POST',
                data: {school_id:'all',filter:'false'},
                url: '../../ajax/calendar/get_high_season_events.php',
                backgroundColor: 'red'
        };
        var start_source2 = {
                type:'POST',
                data: {school_id:'all',filter:'false'},
                url: '../../ajax/calendar/get_middle_season_events.php',
                backgroundColor: 'orange'
        };
        var start_source3 = {
                type:'POST',
                data: {school_id:'all',filter:'false'},
                url: '../../ajax/calendar/get_low_season_events.php',
                backgroundColor: 'green'
        };
        $('#mycalendar').fullCalendar('addEventSource', start_source1);
        $('#mycalendar').fullCalendar('addEventSource', start_source2);
        $('#mycalendar').fullCalendar('addEventSource', start_source3);
    }else{
        $("#eventwrapper").fadeIn();
        $('#mycalendar').fullCalendar ('removeEvents');
        var start_source1 = {
                type:'POST',
                data: {school_id:$("#school_selector").val(),filter:'true'},
                url: '../../ajax/calendar/get_high_season_events.php',
                backgroundColor: 'red'
        };
        var start_source2 = {
                type:'POST',
                data: {school_id:$("#school_selector").val(),filter:'true'},
                url: '../../ajax/calendar/get_middle_season_events.php',
                backgroundColor: 'orange'
        };
        var start_source3 = {
                type:'POST',
                data: {school_id:$("#school_selector").val(),filter:'true'},
                url: '../../ajax/calendar/get_low_season_events.php',
                backgroundColor: 'green'
        };
        $('#mycalendar').fullCalendar('addEventSource', start_source1);
        $('#mycalendar').fullCalendar('addEventSource', start_source2);
        $('#mycalendar').fullCalendar('addEventSource', start_source3);
    }//if
Run Code Online (Sandbox Code Playgroud)

  • 现在这不是客户端过滤,是吗??每次选择后,他们将是一个 ajax 调用来获取过滤的事件,或者我错过了什么? (4认同)