简体   繁体   English

日期选择者在更改日期前一年的月份

[英]Datepicker onchangemonthyear beforeshowday

I have the datepicker on a modal of twitter bootstrap. 我在Twitter引导程序的模式上有datepicker。 in order to highlight some dates, the datepicker is generated in the 'success'-part as an ajax-call. 为了突出显示某些日期,日期选择器在ajax调用的“成功”部分中生成。

I manage to highlight the dates I want to highlight in the current month, which is fine. 我设法突出显示本月要突出显示的日期,这很好。

But when I toggle to the previous or next month, I would like to make that ajax-call again and render dates to highlight. 但是,当我切换到上个月或下个月时,我想再次进行该ajax调用,并突出显示日期。 Below you can see my code: 在下面您可以看到我的代码:

function nonValidated() {           

        var date = new Date();
        date.addDays(-date.getDate() + 1);
        var startDate = [date.getDate().lpad(2), (date.getMonth() + 1).lpad(2), date.getFullYear()].join('/');
        var enddate = new Date();
        enddate.setDate(date.getDaysInMonth());
        var endDate = [enddate.getDate().lpad(2), (enddate.getMonth() + 1).lpad(2), enddate.getFullYear()].join('/');
        var depId = $('#serviceSelector').val();


        $.ajax({
            type: "POST",
            url: "/ServiceManagement/GetUnassignedSlots",
            data: { "from": startDate, "to": endDate, "depId": depId },
            success: function (data) {
                $.datepicker.setDefaults(
                  $.extend(
                    { 'dateFormat': 'dd/mm/yy' },
                    $.datepicker.regional['nl-BE']
                  )
                );
                $("#nonValidatedDatepicker").datepicker(
                    {
                        inline: true,
                        beforeShowDay: function (date) {

                            var theday = date.getDate() + '/' +
                            (date.getMonth() + 1).lpad(2) + '/' +
                            date.getFullYear();
                            return [true, $.inArray(theday, data.result) >= 0 ? "warningDate" : ''];
                        },
                        onSelect: function (dateText, inst) {
                            var dateParts = dateText.split('/');
                            if (dateParts[0][0] == '0') dateParts[0] = dateParts[0][1];
                            if (dateParts[1][0] == '0') dateParts[1] = dateParts[1][1];
                            var newdate = new Date(dateParts[2], dateParts[0]-1, dateParts[1]);

                            var dayOfWeek = newdate.getDay();
                            if (dayOfWeek == 0) dayOfWeek = 7;
                            var weekstart = new Date(newdate.getFullYear(), newdate.getMonth(), newdate.getDate());
                            weekstart.addDays(-dayOfWeek + 1);
                            var weekend = new Date(newdate.getFullYear(), newdate.getMonth(), newdate.getDate());
                            weekend.addDays(7 - dayOfWeek);

                            $('#SelectWeekDate').val([weekstart.getDate().lpad(2), (weekstart.getMonth() + 1).lpad(2), weekstart.getFullYear()].join('/') + ' - ' + [weekend.getDate().lpad(2), (weekend.getMonth() + 1).lpad(2), weekend.getFullYear()].join('/'));
                            $('#modalNonValidated').modal('hide');
                            InitFillPage();
                        },
                        onChangeMonthYear: function (year, month, widget) {


                        }
                    }
                    );

            },
            error: function (data) {
            },
            statusCode: {
                401: function (data) {
                    //ERROR 401: Unauthenticated
                    window.location.href = '/Account/Login?ReturnUrl=' + encodeURIComponent(window.location.pathname);
                }
            }
        });
    }

anyone an idea how I can combine onchangemonthyear and beforeshowday? 任何人都知道如何在onchangemonthyear和showdayday之前结合起来吗?

I would split the code that shows the datepicker and the code that makes the ajax call to get the data for the datepicker (the data that determines which days to highlight) into 2 separate functions. 我将显示日期选择器的代码和进行ajax调用以获取日期选择器的数据(确定突出显示日期的数据)的代码分成两个单独的函数。 You can call the function that makes the ajax call from the function that first shows the datepicker, and again from your onChangeMonthYear function. 您可以从首先显示日期选择器的函数,然后从onChangeMonthYear函数再次调用使ajax调用的函数。 Also, make sure that the ajax call to get the data is made synchronously (set async: false option) so that the data comes back before your beforeShowDay function runs. 另外,请确保同步获取数据的ajax调用(设置async:false选项),以便在运行beforeShowDay函数之前返回数据。

Hope that helps! 希望有帮助!

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

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