简体   繁体   English

如何从Ajax响应重新填充jquery UI datepicker

[英]How can I repopulate the jquery UI datepicker from an Ajax response

I've been using this question as a guide to associating events with the jQuery UI Datepicker. 我一直在使用这个问题作为将事件与jQuery UI Datepicker关联的指南。

I have my dates and events highlighting for the current month which works fine. 我可以突出显示当月的日期和事件,效果很好。 My question is how can I refresh the calendar with new events ( a new set of social events on the calendar as stored in SpektrixApp.events) based on a further ajax call (see onChangeMonthYear in code below) 我的问题是如何基于进一步的ajax调用,使用新事件(存储在SpektrixApp.events中的日历上的一组新社交事件)刷新日历(请参见下面的代码中的onChangeMonthYear)

SpektrixApp = {};

(function($) {

    $.post(
        // see tip #1 for how we declare global javascript variables
        SpektrixAjax.ajaxurl,
        {
            // here we declare the parameters to send along with the request
            // this means the following action hooks will be fired:
            // wp_ajax_nopriv_spektrix_get_events and wp_ajax_spektrix_get_events
            action : 'spektrix_get_events',

            // other parameters can be added along with "action"
            monthId : 9
        },
        function( response ) {
            SpektrixApp.events = response;
            //console.log(events[1]);
            $("#spektrix-event-calendar" ).datepicker({

                beforeShowDay: function(date) {

                    var result = [true, '', null];
                    var matching = $.grep(SpektrixApp.events, function(event) {
                        //console.log(new Date(event.Date).valueOf() );
                        dateToHighlight = new Date(event.Date).valueOf();
                        return dateToHighlight === date.valueOf();
                    });

                    if (matching.length) {
                        result = [true, 'highlight', null];
                    }

                    return result;
                },

                onSelect: function(dateText) {

                    $('#spektrix-dialog').empty(); //empty the target div
                    var date,
                        selectedDate = new Date(dateText),
                        i = 0,
                        event = null;
                        daysEvents = [];

                    /* Determine if the user clicked an event: */
                    while (i < events.length && !event) {
                        //console.log(events[i].Date);
                        date = new Date(SpektrixApp.events[i].Date);

                        if (selectedDate.valueOf() === date.valueOf()) {
                            event = SpektrixApp.events[i];
                            daysEvents.push(event);
                        }
                        i++;
                    }
                    if (daysEvents) {
                       for(i = 0; i < daysEvents.length; i++) {
                        /* If the event is defined, perform some action here; show a tooltip, navigate to a URL, etc. */
                        var day = new Date(event.Date).getDate().toString();
                        $('#spektrix-dialog').append('<div><a href="/whats-on/'+slugify(event.Title)+'">'+event.Title+'</a></div>');

                    }

                    }
                },

                onChangeMonthYear: function(year, month,instance) {
                    jQuery.post(
                        // see tip #1 for how we declare global javascript variables
                        SpektrixAjax.ajaxurl,
                        {
                            // here we declare the parameters to send along with the request
                            // this means the following action hooks will be fired:
                            // wp_ajax_nopriv_spektrix_get_events and wp_ajax_spektrix_get_events
                            action : 'spektrix_get_events',

                            // other parameters can be added along with "action"
                            monthId : month
                        },
                        function( response ) {
                            SpektrixApp.events = response;
                            $("#spektrix-event-calendar" ).datepicker("refresh");

                        }
                    );
                }

            });

            //console.log(events);
        }
    );



})(jQuery);

function slugify(input)
{
    return input.replace(/\s+/g, '-').toLowerCase();
}

Your code does not work by reason of using ajax request in onChangeMonthYear event handler. 由于在onChangeMonthYear事件处理程序中使用了ajax请求,因此您的代码无法正常工作。 beforeShowDay called before SpektrixApp.events will change in onChangeMonthYear. 在SpektrixApp.events将在onChangeMonthYear中更改之前调用的beforeShowDay。 For solve problem you can change jQuery.post to jQuery.ajax and add option 为了解决问题,您可以将jQuery.post更改为jQuery.ajax并添加选项

async : false

to your ajax request declaration in onChangeMonthYear event handler. 到onChangeMonthYear事件处理程序中的ajax请求声明。

Please see this example jsFiddle 请看这个例子jsFiddle

What doesn't work as intended in your code ? 什么在您的代码中不起作用?

The .datepicker("refresh") method should work : here is a simple example with a delayed update .datepicker("refresh")方法应该可以正常工作:这是一个延迟更新的简单示例

var highlight = 3;
$('#date').datepicker({
    beforeShowDay: function(date){
        // highlight days matching the "highlight" weekday :
        var res = [true, "", ""];

        if (date.getDay() == highlight) {
            res = [true, "ui-state-hover", "tip"];
        }

        return res;
    },
    onChangeMonthYear: function(){
        // delayed update : change the "highlight" weekday and trigger refresh
        setTimeout(function(){
            highlight += 1;
            highlight = highlight % 7;
            $('#date').datepicker('refresh');
        }, 1000);
    }
});

fiddle 小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM