I need to set business hours based on a particular ID set by a select option. I am also using the same dropdown to update the events for that particular ID.
The AJAX is working fine however I can't set the options dynamically although the events are being rendered fine. Here's my code.
$('#select').change(function() {
var events = {
url: "cal/load.php",
type: 'POST',
data: {
id: $(this).val()
}
}
var bizHours = "";
$.ajax({
type: 'POST',
url: 'test.php',
data: {id:$(this).val()},
success: function(data)
{
bizHours = data;
}
})
$('#calendar').fullCalendar('option', {
businessHours: bizHours
});
$('#calendar').fullCalendar('removeEventSource', events);
$('#calendar').fullCalendar('addEventSource', events);
$('#calendar').fullCalendar('refetchEvents');
}).change();
This is the return from the test.php:
[{"dow":"1","start":"10:00:00","end":"18:00:00"},
{"dow":"2","start":"10:00:00","end":"18:00:00"},
{"dow":"3","start":"10:00:00","end":"18:00:00"},
{"dow":"4","start":"09:00:00","end":"17:00:00"},
{"dow":"5","start":"09:00:00","end":"17:00:00"}]
If I set it manually like this:
$('#calendar').fullCalendar('option', {
businessHours: [{"dow":"1","start":"10:00:00","end":"18:00:00"}
.....
});
Then this is fine, I think the variable bizHours
is not being set.
How can I update my business hours based on that select?
Update: Having moved the the functions inside the success function due to the async nature of AJAX, it still has the same effect, the business hours are not updated.
Here's the updated code:
$('#learn-ins').change(function() {
var events = {
url: "cal/load.php",
type: 'POST',
data: {
id: $(this).val()
}
}
bizHours = "";
$.ajax({
type: 'POST',
url: 'test.php',
data: {id:$(this).val()},
success: function(data)
{
bizHours = data;
$('#calendar').fullCalendar('option', { businessHours: bizHours });
$('#calendar').fullCalendar('removeEventSource', events);
$('#calendar').fullCalendar('addEventSource', events);
$('#calendar').fullCalendar('refetchEvents');
}
});
});
The test.php returns the correct JSON, I have added it onto console to see the values.