我想从jQuery datepicker ui执行不同类型的排除日期。那些在下面。
我的JSON数据
{ “单身”:[ “2012年2月4日” “2012/3/2” ] “recurrent_day”:[ 0 ] “recurrent_date”:[ 28 ] “期间”:[ { “from”:“2/21/2012”, “to”:“2/22/2012” } ] “recurrent_period”:[ { “from”:“2/28/2012”, “to”:“2/29/2012”, “期间”:“每月” } ]}
我怎么能这样做,有人帮助我。
感谢。
答案 0 :(得分:4)
偷偷使用beforeShowDay
回调。对不起延迟,我在recurrent_period中有一个错误,我花了一段时间才发现。我在测试套件中添加了“年度”案例。如果有什么我在这里过度看了,请告诉我。有趣的问题! jsFiddle
var invalid = { "single": [ "2/4/2012", "3/2/2012" ], "recurrent_day": [ 0 ], "recurrent_date": [ 28 ], "period": [ { "from": "2/21/2012", "to": "2/22/2012" } ], "recurrent_period": [ { "from": "2/28/2012", "to": "2/29/2012", "period": "monthly" },{ "from": "2/7/2012", "to": "2/9/2012", "period": "yearly" } ] };
function single(date){
var USdate = (date.getMonth() + 1) + "/" + date.getDate() + "/" + date.getFullYear();
return ($.inArray(USdate,invalid.single) > -1);
}
function recurrent_day(date){
return ($.inArray(date.getDay(),invalid.recurrent_day) > -1);
}
function recurrent_date(date){
return ($.inArray(date.getDate(),invalid.recurrent_date) > -1);
}
function period(date){
var i, num, period, start, startArray, end, endArray;
num = invalid.period.length;
for(i=0;i<num;i++){
period = invalid.period[i];
startArray = period.from.split('/');
start = new Date(startArray[2], (startArray[0] - 1), startArray[1]);
endArray = period.to.split('/');
end = new Date(endArray[2], (endArray[0] - 1), endArray[1]);
if(date>=start && date<=end){
return true;
}
}
return false;
}
function recurrent_period(date){
var i, num, period, recurrence, startArray, endArray, startDay, endDay, start, end;
num = invalid.recurrent_period.length;
for(i=0;i<num;i++){
period = invalid.recurrent_period[i];
recurrence = period.period;
startArray = period.from.split('/');
endArray = period.to.split('/');
if( recurrence === 'monthly' ){
startDay = parseInt( startArray[1], 10);
endDay = parseInt( endArray[1], 10);
if( date.getDate() >= startDay && date.getDate() <= endDay ){
return true;
}
}else if( recurrence === 'yearly' ){
start = new Date(date.getFullYear(), (startArray[0] - 1), startArray[1]);
end = new Date(date.getFullYear(), (endArray[0] - 1), endArray[1]);
console.log({start:start.toDateString() ,end:end.toDateString(),day:date.toDateString()})
if(date>=start && date<=end){
return true;
}
}
}
return false;
}
$('input').datepicker({
beforeShowDay: function(date){
if(single(date)){
return [false];
}else if(recurrent_day(date)){
return [false];
}else if(recurrent_date(date)){
return [false];
}else if(period(date)){
return [false];
}else if(recurrent_period(date)){
return [false];
}
return [true];
}
});