2012-02-06 68 views
3

我对JS很新。我对Web服务调用返回一个这样的数组:将FullCalendar事件填充为数组 - 从另一个数组填充

data {...} 
    [0]: {...} 
    [1]: {...} 
    [2]: {...} 
    [3]: {...} 
    length: 4 

    data[0] {...} 
    __type: "acme.acmeSystem.EventManagement.Event" 
    Amenity: {...} 
    DateFrom: "/Date(1326952800000)/" 
    DateTo: "/Date(1326952800000)/" 
    Description: "Friends coming over for a party." 
    Food: false 
    Id: 3 
    IsPrivate: true 
    Notes: "" 
    NumberOfPeople: "Less than 10" 
    Status: {...} 
    TimeFrom: "8:30 AM" 
    TimeTo: "11:30 AM" 
    User: {...} 

我怎么能堵住这个到FullCalendar?

我想是这样的:

GetEvents([], false, 
    { 
     successCallback: function (data) { 
      if ((data) && (data != null) && (data.length > 0)) { 

       buildingEvents = data;     

       $('#calendar').fullCalendar({ 
        theme: true, 
        header: { 
         left: 'prev,next today', 
         center: 'title', 
         right: 'month,agendaWeek,agendaDay' 
        }, 
        editable: false, 
        events: buildingEvents 
       }); 

      } 

      data = null; 
     } 
    }); 

我需要建立从建设活动阵列FullCalendar事件的另一个数组:

喜欢的东西:

$('#calendar').fullCalendar({ 
    events: [ 
     { 
      title : 'event1', 
      start : '2010-01-01' 
     }, 
     { 
      title : 'event2', 
      start : '2010-01-05', 
      end : '2010-01-07' 
     }, 
     { 
      title : 'event3', 
      start : '2010-01-09 12:30:00', 
      allDay : false // will make the time show 
     } 
    ] 
}); 

感谢。

回答

2

嗯,我想你可以使用jQuery的功能$.map()

jQuery.map(数组,回调(elementOfArray,indexInArray))返回:数组
描述:翻译成阵列或对象项的新的数组的所有项目。

基本上你将映射您从服务器接收到的数据EVENTDATA的数组,对象fullCalendar理解:

var buildingEvents = $.map(data, function(item) { 
    return { 
     id: item.Id, 
     title: item.Description, 
     start: ... // "bind" all properties as needed 
    }; 
}); 
+0

非常感谢您!这工作。因此,var buildingEvents = $ .map(data,function(item){ return { id:item.Id, title:item.Description, start:..)只需要进行更正(冒号而不是=)。 。“根据需要绑定”所有属性 }; }); – user1100221 2012-02-06 22:41:13

+0

不客气,感谢':'! – 2012-02-07 06:10:46

+0

这种类型的实现的第二个参考可以在这里找到:http://simpledotnetsolutions.wordpress.com/2012/08/14/exploring-jquery-plugin-fullcalendar/ 这个问题对我很有帮助。谢谢! – ter24 2014-07-30 00:56:58