jQuery DatePicker - 如何突出每月的某些日子?

时间:2010-03-11 10:07:23

标签: jquery jquery-ui

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

 $.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);
                }
            });
        }
    }

1 个答案:

答案 0 :(得分:1)

问题是你正在制作ajax请求onChangeMonthYear并重新初始化datepicker,当你的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);
    }
});