Elaborando la risposta di Rob Van Dam, ecco un esempio di codice (utilizzando jQuery UI datepicker) per il recupero delle date abilitate utilizzando AJAX. Io uso AJAX per interrogare una pagina PHP che restituisce la data abilitata da un database (perché voglio solo mostrare le date in cui ho dati reali).
L'idea è di eseguire la query AJAX ogni volta che l'utente cambia mese e quindi di memorizzare le date abilitate per questo mese in un array globale (allowedDates) che verrà utilizzato in seguito da beforeShowDay.
Ovviamente, è possibile eseguire la query AJAX in beforeShowDay, ma questo sarebbe abbastanza inefficiente a causa dell'elevato numero di query AJAX (più di 30 per ogni mese).
var allowedDates = new Object();
function queryAllowedDates (year, month, id) {
$.ajax({
type: 'GET',
url: 'calendar_days.php',
dataType: 'json',
success: function(response) {
allowedDates[id] = response.allowedDates;
},
data: {year:year,month:month},
async: false
});
}
$("#datepicker1").datepicker({
dateFormat: 'dd.mm.yy',
changeMonth: true,
changeYear: true ,
beforeShow: function (input) {
var currentDate = $(input).datepicker('getDate');
var id = $(input).attr('id');
queryAllowedDates(currentDate.getFullYear(), currentDate.getMonth()+1,id);
},
onChangeMonthYear: function (year, month, inst) {
queryAllowedDates(year, month, inst.input.attr('id'));
},
beforeShowDay: function (day) {
var id = $(this).attr('id');
var date_str = [
day.getFullYear(),
day.getMonth() + 1,
day.getDate()
].join('-');
if (allowedDates[id] != undefined && allowedDates[id][date_str]) {
return [true, 'good_date', 'This date is selectable'];
} else {
return [false, 'bad_date', 'This date is NOT selectable'];
}
}
});
In questo esempio, la pagina PHP restituisce una matrice JSON contenente qualcosa come:
{"allowedDates":{"2008-12-04":1,"2008-12-11":1}}
Quale tecnologia lato server stai usando? Hai solo taggato il clientide? A seconda del server (asp.net, ecc.) Sono disponibili molti controlli in cui è possibile implementare questa funzionalità. –
Non merita una risposta completa, ma sono stato in grado di aggirare jQuery sourcecode e modificare i fine settimana per un buon Datepicker. – dmanexe