将FullCalendar事件作为数组填充 - 从另一个数组中填充

4
我是一名JS的新手。我的调用Web服务返回的数组如下:
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
        }
    ]
});

谢谢。

1个回答

3

我猜您可以使用jquery函数$.map()

jQuery.map( array, callback(elementOfArray, indexInArray) ) 返回值:Array
描述:将数组或对象中的所有项转换为新的项数组。

基本上,您将把从服务器收到的数据映射到可被fullCalendar理解的EventData对象数组中:

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

非常感谢!那个有效了。只是想纠正一下(冒号而不是等于号)。因此,变量buildingEvents = $ .map(data,function(item){ 返回{ id:item.Id, title:item.Description, start:... //根据需要“绑定”所有属性 }; }); - saz
这种实现方式的第二个参考可以在这里找到:http://simpledotnetsolutions.wordpress.com/2012/08/14/exploring-jquery-plugin-fullcalendar/ 这个问题对我非常有帮助。谢谢! - ter24

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接