jQuery DatePicker-如何突出显示每个月的特定日期?
我有一个jquery datepicker,默认情况下它将当前日期呈现为完整日历。在呈现之前,我通过ajax从服务器获得了当前月份需要突出显示的天数列表。其代码如下:jQuery DatePicker-如何突出显示每个月的特定日期?,jquery,jquery-ui,Jquery,Jquery Ui,我有一个jquery datepicker,默认情况下它将当前日期呈现为完整日历。在呈现之前,我通过ajax从服务器获得了当前月份需要突出显示的天数列表。其代码如下: $.get("Note/GetActionDates/?orgID=" + orgID + "&month=" + month +"&year=" + year, null, function(result) { RenderCalendar(result);
$.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中的代码进行了如下调整,现在可以正常工作了。我将添加更多的代码来从dates数组中删除重复的日期,但除此之外,这一切都很好
$("#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);
}
});
}
}
$(“#actionCal”).datepicker({
日期格式:“dd/mm/yyyy”,
展示日前:功能(日期){
var theday=thedate.getDate()+“/”+(thedate.getMonth()+1)+“/”+thedate.getFullYear();
如果($.inArray(theday,actionCalDates)=-1){
返回[真,”,“”];
}否则{
return[true,“specialDate”,“Actions Today”];
}
},
onChangeMonthYear:功能(年、月、月){
日期计数=0;
获取日期(起始日期、月份、年份);
}
});
函数getDates(起始日期、月份、年份){
日期计数+=1;
如果(日期计数<4){
$.ajax({
url:“Note/GetActionDates/”,
数据:{
“orgID”:orgID,
“月”:月,
“年”:年
},
键入:“获取”,
数据类型:“json”,
成功:功能(结果){
actionCalDates=actionCalDates.concat(结果);
getDates(起始日期、月份+1、年份);
getDates(起始日期,月份-1,年份);
}
});
}
}
问题在于,在ajax请求完成的时候,新的月份已经被绘制出来了,您在ChangeMonthyear发出ajax请求并重新初始化日期选择器
您可以做的是加载当前月份以及上个月和下个月的“日期”。然后一旦换了一个月,就去拿更多的日期,并将其添加到你的日期里
类似这样(未经测试):
问题在于,当ajax请求完成新月份的绘制时,您正在onChangeMonthYear发出ajax请求并重新初始化日期选择器 您可以做的是加载当前月份以及上个月和下个月的“日期”。然后一旦换了一个月,就去拿更多的日期,并将其添加到你的日期里 类似这样(未经测试):
塞吉奥:你的代码是什么样子的?我在实现了您所做的大部分工作之后,我只想看看您是如何实现代码的,我正在使用asp.NET。如果您有两个日历字段,该怎么办?如果您将第一个字段放回几个月,那么当您单击它时,第二个字段将为空,因为它没有可用的日期。塞吉奥:你的代码是什么样子的?我在实现了您所做的大部分工作之后,我只想看看您是如何实现代码的,我正在使用asp.NET。如果您有两个日历字段,该怎么办?如果您将第一个字段放回几个月,那么当您单击它时,第二个字段将为空,因为它没有可用的日期。你怎么避开这个?
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);
}
});