Paul-DS / bootstrap-year-calendar

[DEPRECATED] A fully customizable year calendar widget, for boostrap !
Apache License 2.0
293 stars 243 forks source link

Load AJAX not work on first load #163

Open gandhyonly opened 7 years ago

gandhyonly commented 7 years ago

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?

William-H-M commented 7 years ago

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.