【问题标题】:jQuery DatePicker - How to highlight certain days every month?jQuery DatePicker - 如何突出显示每个月的某些日子?
【发布时间】:2010-03-11 10:07:22
【问题描述】:

我有一个 jquery datepicker,它默认将当前日期呈现为一个完整的日历。在呈现之前,我通过 ajax 从服务器获取了当月需要突出显示的天数列表。代码如下:

 $.get("Note/GetActionDates/?orgID=" + orgID + "&month=" + month +"&year=" + year,
        null, function(result) {
            RenderCalendar(result);
        }, "json");


function RenderCalendar(dates) {
        $("#actionCal").datepicker({ dateFormat: 'dd/mm/yy', beforeShowDay: function(thedate) {
            var theday = thedate.getDate();
            if ($.inArray(theday, dates) == -1) {
                return [true, "", ""];
            }
            else {
                return [true, "specialDate", "Actions Today"];
            }
        }
        });
    }

这一切都很好,但我希望在用户点击不同月份时更新突出显示的日期。我可以使用以下代码修改 jquery datepicker 初始化代码:

onChangeMonthYear: function(year, month, inst) {
            //get new array of dates for that month
            $.get("Note/GetActionDates/?orgID=" + orgID + "&month=" + month + "&year=" + year,
            null, function(result) {
                RenderCalendar(result);
        }, "json");
            }

但这似乎不起作用。

谁能告诉我我做错了什么?谢谢! :)

更新 - 工作代码

感谢您的帮助!

我已经对 petersendidit 中的代码进行了如下调整,现在它可以工作了。将添加更多代码以从日期数组中删除重复的日期,但除此之外一切都很好。

$("#actionCal").datepicker({
            dateFormat: 'dd/mm/yyyy',
            beforeShowDay: function(thedate) {
                var theday = thedate.getDate() + "/" + (thedate.getMonth() + 1) + "/" + thedate.getFullYear();
                if ($.inArray(theday, actionCalDates) == -1) {
                    return [true, "", ""];
                } else {
                    return [true, "specialDate", "Actions Today"];
                }
            },
            onChangeMonthYear: function(year, month, inst) {
                dateCount = 0;
                getDates(orgID, month, year);
            }

        });

function getDates(orgID, month, year) {
        dateCount += 1;
        if (dateCount < 4) {
            $.ajax({
                url: "Note/GetActionDates/",
                data: {
                    'orgID': orgID,
                    'month': month,
                    'year': year
                },
                type: "GET",
                dataType: "json",
                success: function(result) {
                    actionCalDates = actionCalDates.concat(result);
                    getDates(orgID, month + 1, year);
                    getDates(orgID, month - 1, year);
                }
            });
        }
    }

【问题讨论】:

    标签: jquery jquery-ui


    【解决方案1】:

    问题是您正在发出 ajax 请求 onChangeMonthYear 并重新初始化日期选择器,当您的 ajax 请求完成时,新月份已经绘制完毕。

    您可以做的是加载当前月份以及上个月和下个月的“日期”。然后 onChangeMonthYear 去获取更多日期并将其添加到您的 datesArray。

    类似这样的东西(未经测试):

    var actionCalDates = array();
    function getDates(orgID, month, year) {
        $.ajax({
            url:"Note/GetActionDates/",
            data: {
                'orgID':orgID,
                'month':month,
                'year':year
            },
            type:"GET",
            dataType: "json",
            success: function(result) {
                actionCalDates.concat(result);
                getDates(orgID, month+1, year);
                getDates(orgID, month-1, year);
            }
        });
    }
    
    $("#actionCal").datepicker({ 
        dateFormat: 'dd/mm/yy',
        beforeShowDay: function(thedate) {
            var theday = thedate.getDate();
            if ($.inArray(theday, actionCalDates) == -1) {
                return [true, "", ""];
            } else {
                return [true, "specialDate", "Actions Today"];
            }
        },
        onChangeMonthYear: function(year, month, inst) {
            getDates(orgID, month, year);
        }
    });
    

    【讨论】:

    • Sergio:你的代码是怎样的?我已经实现了你所做的几乎所有事情,我只是想看看你是如何实现代码的,我正在使用 asp.net
    • 如果您有两个日历字段怎么办?如果您将第一个字段放回几个月后,第二个字段将在您单击时为空,因为它没有可用的日期。你如何解决这个问题?
    猜你喜欢
    • 1970-01-01
    • 2014-04-26
    • 1970-01-01
    • 1970-01-01
    • 2012-12-11
    • 1970-01-01
    • 2015-12-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多