2015-10-28 53 views
-1


我使用this answer来过滤客户端的事件。在客户端过滤FullCalendar事件

它适用于较新的事件,但我无法过滤从JSON加载的事件。

这里是我的JS代码:

$(document).ready(function() { 
    var idEspacoFisico=$('#seleciona option:selected').val(); 
    $("#calendar").fullCalendar({ 
     events: 'eventos/getEventos.json',//can't filter these 
     eventRender: function eventRender(event, element, view) { 
      //from the answer. 
      return ['0',event.espacoFisico].indexOf($('#seleciona option:selected').val()) >= 0 
     }, 
     select: function(start, end){ 
      //When created here, I can use the the eventRender to filter. 
     } 
    }); 
    $('#seleciona').change(function(){ 
     $("#calendar").fullCalendar('rerenderEvents'); 
    }); 
}); 

和HTML:

<select class="form-control" id="seleciona"> 
    <option value="0">Selecione</option> 
    <option value="1">LAB 1</option> 
    <option value="2">LAB 2</option> 
    ... 
</select> 

<div id="calendar"></div> 

我错过了什么? 感谢您的帮助。

+0

为什么你不能过滤JSON事件?你真的遇到了什么问题? –

+0

问题是我无法过滤使用此解决方案的json加载的事件,只有新事件。每次我保存一个新事件时,过滤器都能正常工作,直到我重新加载页面并通过JSON获取事件。 –

+0

还需要更多的细节 - “这个解决方案”和给你新事件的区别有什么区别?在解决问题之前,我们必须诊断问题实际上是什么。 –

回答

1

我找到了答案。我使用.change()来完成过滤器。 我也从.fullCalendar中删除了'event'和'eventRender'以避免冲突。

$(document).ready(function() { 
var idEspacoFisico=$('#seleciona option:selected').val(); 
$("#calendar").fullCalendar({ 
    select: function(start, end){ 
    ... 
    } 
}); 
$('#seleciona').change(function(){ 
    $("#calendar").fullCalendar('removeEvents');//remove the old filtered events 
    var idEspacoFisico=$('#seleciona option:selected').val();//filter based on the select value 
    $.ajax({ 
     type:"POST", 
     url:'eventos/getEventos.json', //get events 
     success: function(data){ 
      $.each(data,function(index,value){//for each event, I will compare the value with the filter, if true, render it. 
       if(value.espacoFisico==idEspacoFisico){ 
        $("#calendar").fullCalendar('renderEvent', value, true); 
       } 
      }) 
     } 
    }); 
}); 
});