I'm using Jquery UI datepicker to allow a user to fill a date input by selecting a date out of a displayed a calendar.
So far, everything works as expected : http://jsfiddle.net/Aut9b/374/
Then, I wanted to highlight certain dates, to help the user choose, so I looked into the beforeShowDay
option which makes that possible.
beforeShowDayType: Function( Date date )
Default: null A function that takes a date as a parameter and must return an array with: [0]: true/false indicating whether or not this date is selectable [1]: a CSS class name to add to the date's cell or "" for the default presentation [2]: an optional popup tooltip for this date The function is called for each day in the datepicker before it is displayed.
Here is the demo : http://jsfiddle.net/Aut9b/375/
The next step is not only to highlight certain dates but to do it dynamically, based on what the user had previously selected in other inputs (in the same form), so I have used ajax
in order to retrieve the dates to highlight
This is my (incomplete) code so far.
$(function() {
$('.datepicker').datepicker({
dateFormat : 'yy-mm-dd'
});
});
function fillDates() {
$('.datepicker').datepicker({
beforeShowDay: function( date ) {
var highlight = dates[date];
if( highlight ) {
return [true, 'highlight', highlight];
} else {
return [true, '', ''];
}
}
});
}
function getDates() {
$.ajax({
type : "POST",
dataType: "text",
url : "ajaxFindDates",
data : {departure:$('#departure option:selected').val(),
arrival:$('#arrival option:selected').val()},
success : function(data) {
var dateStr = JSON.parse(data);
var dates=[];
for (var i = 0; i < dateStr.length; i++) {
date=new Date(dateStr[i]);
dates.push(date);
}
fillDates(dates);
}
,
error : function(data) {
alert("Problem!" );
}
});
}
The function getDates()
is called when the value of the <select>
changes.
I have tried to debug using the browser developer tool and it seems that the function defined in the beforeShowDay
is never executed.
Any help will be much appreciated! Thanks.
var dateStr = JSON.parse(data);
. You could simpy usedataType: "json"
.