【问题标题】:jQuery FullCalendar. Default to first month with eventjQuery 全日历。默认为有事件的第一个月
【发布时间】:2012-06-27 08:43:46
【问题描述】:

我正在使用下面的代码来初始化一个 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'
        });

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

【问题讨论】:

    标签: javascript jquery fullcalendar


    【解决方案1】:

    绝对可以实现!您需要首先对事件数组进行排序并找到下一个注册的事件对象。然后你可以使用 .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 以获取有效的静态示例。

    希望这会有所帮助!

    【讨论】:

    • 哦,请检查下一个日期是否大于今天(或上个月) - 以确保您不会倒退。
    • 如果事件是通过url获取而不是放入数组中怎么实现?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-31
    • 2019-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多