1

我正在使用下面的代码来初始化一个 jQuery FullCalendar。

但是,并非所有月份都有任何事件。我希望日历的初始月份视图是有事件的第一个月。

举个例子。目前是 6 月,在 8 月之前日历中没有任何事件,所以我希望日历的初始视图默认为 8 月而不是 6 月。

同样,如果 6 月有事件,我希望它默认为 6 月。

$('#fullCalendar').fullCalendar({
            selectable: true,
            selectHelper: true,
            header: {
                left: 'prev',
                center: 'title',
                right: 'next'
            },
            events: function(start, end, callback) {

                $.getJSON(jSONCalendarURL, {
                            token:jSONAPItoken,
                            productID: $('#fullCalendar').attr("data-id"),
                            startDate: $.fullCalendar.formatDate(start, 'yyyy-MM-dd'),
                            endDate: $.fullCalendar.formatDate(end, 'yyyy-MM-dd')
                        }, function(data){
                        var calevents = [];
                        $.each(data.response, function(index,item){
                            calDate = index;
                            child = item.Variations;
                            if (child.length > 0 ){
                                if (!$.isEmptyObject(child)){
                                    calPrice = child[0].Price;
                                    calID = child[0].ID;
                                    calAvailable = child[0].Available;
                                    calevents.push({
                                        'id': calID,
                                        'title': buildEventTitle(calAvailable,calDate.substring(calDate.length,calDate.length-2),child[0].Price, calID, child[0].RRP),
                                        'start': $.fullCalendar.parseDate(calDate),
                                        'end': $.fullCalendar.parseDate(calDate),
                                        'allDay': true
                                    });

                                }
                            }
                        });
                        callback(calevents);
                        highlightExisting();

                    }
                );
            },

            eventRender: function (event, element, view) {
                if (event.start.getMonth() != view.start.getMonth())
                    return false;
            },
            weekMode:'liquid'
        });

我想做的事情可以实现吗?

4

1 回答 1

6

绝对可以实现!您需要首先对事件数组进行排序并找到下一个注册的事件对象。然后您可以使用 .fullCalendar('gotoDate') 方法跳转到该日期。

像这样的东西:

function custom_sort(a, b) {
    return new Date(a.start).getTime() - new Date(b.start).getTime();
}

events_array.sort(custom_sort);

$('#calendar').fullCalendar('gotoDate', events_array[0].start);

请参阅此FIDDLE以获取有效的静态示例。

希望这可以帮助!

于 2012-06-26T04:26:02.290 回答