最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery FullCalendar. Default to first month with event - Stack Overflow

programmeradmin1浏览0评论

I am using the below code to initialise a jQuery FullCalendar.

However, not all months have any events. I would like the initial month view of the calendar to be the first month with an event.

So as an example. It is currently June, there are no events in the Calendar until August so I would like would like the initial view of the calendar to default to August rather than June.

Likewise, if there ARE events in June, I would like it to default to June.

$('#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'
        });

Is what I am trying to do achievable?

I am using the below code to initialise a jQuery FullCalendar.

However, not all months have any events. I would like the initial month view of the calendar to be the first month with an event.

So as an example. It is currently June, there are no events in the Calendar until August so I would like would like the initial view of the calendar to default to August rather than June.

Likewise, if there ARE events in June, I would like it to default to June.

$('#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'
        });

Is what I am trying to do achievable?

Share Improve this question asked Jun 26, 2012 at 3:32 FraserFraser 14.3k22 gold badges76 silver badges119 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

Definitely achievable! You will need to first start with sorting your events array and finding the next registered event object. Then you can use the .fullCalendar('gotoDate') method to jump to that date.

Something like this:

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);

Please refer this FIDDLE for a working static example.

Hope this helps!

发布评论

评论列表(0)

  1. 暂无评论