bootstrap-year-calendar icon indicating copy to clipboard operation
bootstrap-year-calendar copied to clipboard

Load AJAX not work on first load

Open gandhyonly opened this issue 6 years ago • 1 comments

I create year calendar but it's not working on first load. If I change year and data source will load completely.

<script>
        $(document).ready(function() {
            var currentYear = new Date().getFullYear();

            var calendar = $('#calendar').calendar({
                weekStart: 1,
                minDate: new Date(currentYear-1, 0, 1),
                maxDate: new Date(currentYear+1, 11, 31),
                style: 'border',
                dataSource: getMyData()
            });
            function getMyData() {
                var myData=[];
                $.ajax({
                    url: 'load_events.php',
                    type: 'POST',
                    dataType: 'json',
                    data: {'action':'getDataCalendarYear'},
                    success: function (response) {
                        for (var i = 0; i < response.length; i++) {
                            myData.push({
                                id: response[i].id,
                                title: response[i].title,
                                startDate: new Date(response[i].startDate),
                                endDate: new Date(response[i].endDate),
                                color: response[i].color
                            });
                        }
                    }
                });
                return myData;
            }
        });
    </script>

if I console.log(calendar) dataSource variable already set. and If I write dataSource manually (without ajax) it will work perfectly did you know what's wrong about it?

gandhyonly avatar Aug 11 '17 06:08 gandhyonly

Hi, refer to #103 there's an example, use it with the event yearChanged instead of dataSource Why? Well it's somehow better because the event yearChange will be activated in the first run, other way to do it is instead of dataSource use its respective event like:

$('#calendar').data('calendar').setDataSource(getMyData());

Note: the way of the example load data per year, and you should do it that way of filtering, because that way you won't burden all the possible event of past and future years, you'll probably think it's not much but believe after a few months client will be go crazy creating events.

Note2: yearChanged it's avalaible at this repo and not at the release.

William-H-M avatar Aug 11 '17 12:08 William-H-M