2012-07-16 165 views
4

我需要重新获取不仅在日历按钮,但是对我自己的下拉改变全日历事件,因为它们定义为事件搜索的条件。我至今使用$.ajax,但现在的事件没有得到日历通过下拉菜单的值的唯一方法。我错过了一些东西,因为我完全不明白这个功能。Fullcalendar重取事件

$(function() { 
    InitializeCalendar(); 

    $("#ProjectId").change(function() { 
     $('#calendar').fullCalendar('refetchEvents'); 
    }); 

    $("#JobId").change(function() { 
     $('#calendar').fullCalendar('refetchEvents'); 
    }); 

    $("#StoreId").change(function() { 
     $('#calendar').fullCalendar('refetchEvents'); 
    }); 

    $("#OrderType").change(function() { 
     $('#calendar').fullCalendar('refetchEvents'); 
    }); 

    $("#ShippingId").change(function() { 
     $('#calendar').fullCalendar('refetchEvents'); 
    }); 
}); 

function InitializeCalendar() { 
    $('#calendar').fullCalendar({ 
     header: { 
      left: 'prev,next today', 
      center: 'title', 
      right: 'month,agendaWeek,agendaDay' 
     }, 
     events: function (start, end) { 
      $.ajax({ 
       url: '/Calendar/GetEvents/', 
       dataType: 'json', 
       data: { 
        start: Math.round(start.getTime()/1000), 
        end: Math.round(end.getTime()/1000), 
        projectId: $("#ProjectId option:selected").val(), 
        storeId: $("#StoreId option:selected").val(), 
        jobId: $("#JobId option:selected").val(), 
        orderType: $("#OrderType option:selected").val(), 
        shippingId: $("#ShippingId option:selected").val() 
       } 

       **SOMETHING MISSING HERE** 

       //this puts calendar into infinite loop 
       //$('#calendar').fullCalendar('refetchEvents'); 
      }); 
     } 
    }); 
} 

控制器:

public JsonResult GetEvents(double start, double end, int? projectId, int? storeId, string orderType, int? shippingId, int? jobId) 
{ 
    // Some code... 

    IList<OrderEvent> events = orderDTOs.Select(orderDTO => new OrderEvent { 
     id = orderDTO.OrderId, 
     title = orderDTO.OrderId.ToString(), 
     start = ToUnixTimespan(orderDTO.CreatedDate), 
     end = ToUnixTimespan(orderDTO.CreatedDate.AddHours(1)), 
     url = "/Order/Search" 
    }).ToList(); 

    return Json(events, JsonRequestBehavior.AllowGet); 
} 
+0

我想知道这是如何解决的!谢谢 – Mathieu 2013-03-01 16:42:46

+0

你解决了这个问题吗?答案不起作用。 – Chookoos 2013-09-19 02:30:36

回答

1

我不认为你应该把事件中的Ajax调用的refetchEvents电话。 refetchEvents将再次调用ajax--因此是无限循环。

另外,我觉得您从下拉列表将数据发送到日历的方式是好的。尝试从ajax调用中删除refetchEvents调用,看看它是否有效。

你也可以使用Ajax调用来处理来自控制器返回事件的成功回调 - 如果你需要。

-2

像这样的东西应该为你工作。只要有完整的日历通过你的Ajax调用成功功能重新获取的事件,因此应在每次通话后只能重新获取,而不是无限喜欢它目前的作用:

events: function (start, end) { 
    $.ajax({ 
     url: '/Calendar/GetEvents/', 
     dataType: 'json', 
     data: { 
      start: Math.round(start.getTime()/1000), 
      end: Math.round(end.getTime()/1000), 
      projectId: $("#ProjectId option:selected").val(), 
      storeId: $("#StoreId option:selected").val(), 
      jobId: $("#JobId option:selected").val(), 
      orderType: $("#OrderType option:selected").val(), 
      shippingId: $("#ShippingId option:selected").val() 
     } 

     **SOMETHING MISSING HERE** 
     success: function(data) { 
      $("#calendar").fullCalendar("refetchEvents"); 
      console.log('successfully refetched events'); 
     } 

     //this puts calendar into infinite loop 
     //$('#calendar').fullCalendar('refetchEvents'); 
    }); 
4

在未来的读者的利益,这里是正确的FullCalendar v2的解决方案。

events: function (start, end, timezone, callback) { 
    $.ajax({ 
     url: '/Calendar/GetEvents/', 
     dataType: 'json', 
     data: { 
      start: Math.round(start.getTime()/1000), 
      end: Math.round(end.getTime()/1000), 
      projectId: $("#ProjectId option:selected").val(), 
      storeId: $("#StoreId option:selected").val(), 
      jobId: $("#JobId option:selected").val(), 
      orderType: $("#OrderType option:selected").val(), 
      shippingId: $("#ShippingId option:selected").val() 
     } 
     success: function(data) { 
      callback(data) 
     } 
    });