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

标签 javascript jquery fullcalendar

我对 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
        }
    ]
});

谢谢。

最佳答案

嗯,我猜你可以使用 jquery 函数 $.map() .

jQuery.map( array, callback(elementOfArray, indexInArray) ) Returns: Array
Description: Translate all items in an array or object to new array of items.

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

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

关于javascript - 将 FullCalendar 事件填充为数组 - 从另一个数组填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9168147/

相关文章:

javascript - 控制台日志返回null

javascript - 使用 JS 切换禁用类

javascript - fullcalendar:在 clientevent 中获取一周中的几天

即使删除所有事件,FullCalendar 也会插入重复事件

javascript - 学校每年的自定义周数

javascript - 在移动设备中播放音频——Angular ng-click,DOM 异常 35 NotAllowedError

javascript - 无法在 redux 操作中重命名响应

javascript - 单击按钮时设置元素的高度

javascript - WooCommerce:根据所选的运输方式显示或隐藏结账字段和部分

javascript - 增加弹出背景的高度