I have a tempusdominus datetimepicker and a linked select. I get a structure from the controller consisting of a Map> which keys are the dates that should be enabled in the datetimepicker and each date has a list of hours that I have to show in the linked select when the user picks a date.
I have this in my jsp:
<div class="row">
<div class="col-sm-6 labels">
<div class="form-group">
<label>Date</label>
<div class="input-group date" id="fechaCita" data-target-input="nearest">
<input type="text" class="form-control datetimepicker-input" data-target="#fechaCita"/>
<div class="input-group-append" data-target="#fechaCita" data-toggle="datetimepicker">
<div class="input-group-text"><i class="fa fa-calendar"></i></div>
</div>
</div>
</div>
</div>
<div class="col-sm-4 labels">
<div class="form-group">
<label>Time</label>
<div class="input-group date" id="horaInicio">
<form:select path="horario.horaInicio" id="selectHoraCita" class="form-control">
<form:option value="">--</form:option>
</form:select>
<input type="hidden" id="horaInicioSelected" />
</div>
</div>
</div>
Then in my javascript file I have the following:
$(function() {
$.ajax({
url: rutaBase + '/huecos.json',
success: { function(huecos) {
console.log('Huecos' + huecos);
var enabledDates = [];
for (var key in huecos) {
if (huecos.hasOwnProperty(key)) {
enabledDates.push(key);
}
}
$('#fechaCita').datetimepicker({
format : 'DD/MM/YYYY', // Solo se mostrarĂ¡ la fecha
enabledDates : enabledDates,
icons : {
date : "fa fa-calendar",
up : "fa fa-caret-up",
down : "fa fa-caret-down",
previous : "fa fa-caret-left",
next : "fa fa-caret-right",
today : "fa fa-today",
clear : "fa fa-clear",
close : "fa fa-close"
}
});
$("#fechaCita").on("change.datetimepicker", function (e) {
console.log('Onchange! ' + e.date);
$('#selectHoraCita').empty();
var horas = huecos[e.date];
horas.each(function(index, listItem) {
console.log('List item ' +listItem);
if (p.hasOwnProperty(e.date)) {
$('#horaCita').append($("<option></option>")).attr("value", listItem).text(listItem);
}
});
});
}
}
});
});
This Ajax call gets something like {2019-09-12=[12:00 - 13:00, 14:30 - 15:00], 2019-09-13=[10:00 - 11:00]} as a result.
But this doesn't work, I get a datetimepicker with date and time and I get nothing on the console... If I take out the ajax call and just put enabledDates: ["2019-09-12", "2019-09-13"] it works, but not if I try to get them from the ajax call.
I'm very useless with ajax/jquery/javascript so I'm sure I'm doing something wrong...
So I guessed it, I had various syntax errors:
$(function() {
$.ajax({
url : rutaBase + '/huecos.json',
success : function(huecos) {
var enabledDates = [];
for ( var key in huecos) {
if (huecos.hasOwnProperty(key)) {
enabledDates.push(key);
}
}
$('#fechaCita').datetimepicker({
format : 'DD/MM/YYYY', // Solo se mostrarĂ¡ la fecha
enabledDates : enabledDates,
icons : {
date : "fa fa-calendar",
up : "fa fa-caret-up",
down : "fa fa-caret-down",
previous : "fa fa-caret-left",
next : "fa fa-caret-right",
today : "fa fa-today",
clear : "fa fa-clear",
close : "fa fa-close"
}
});
$("#fechaCita").on(
"change.datetimepicker",
function(e) {
$('#selectHoraCita').empty();
$('#selectHoraCita').append($("<option></option>").attr("value","").text("--"));
var horas = [];
var fecha = moment(e.date).format('YYYY-MM-DD');
horas.push(huecos[fecha]);
horas
.forEach(function(listItem) {
listItem.forEach(function(opcion) {
$('#selectHoraCita').append(
$("<option></option>").attr(
"value", opcion).text(
opcion));
});
});
});
},
error : function() {
console.log('Error');
}
});
});