I am trying to use JQuery date picker and I want to make use of the beforeShowDay method in order to block out dates in the date picker. I have been able to get the widget to work and if I define an array, the beforeShowDay method works flawlessly. But my issue is passing the data from my Django model to create an array. Is there a way to create an array within the element in the template in order to achieve this?
template
JavaScript
x
25
25
1
<script>
2
# works as intended when the array is defined manually
3
var array = [
4
"2022-10-01"
5
]
6
7
# this is where I am having trouble creating an array from the model data
8
var array = [
9
{% for x in listing_reservations %}
10
{{x.dates}}
11
{% endfor %}
12
]
13
14
$(function() {
15
$( "#id_start_date" ).datepicker(
16
{
17
beforeShowDay: function(date){
18
var string = jQuery.datepicker.formatDate('yy-mm-dd', date);
19
return [ array.indexOf(string) == -1 ];
20
}
21
}
22
);
23
});
24
</script>
25
Advertisement
Answer
Finally figured it out, hopefully helps someone in the future! Thanks Haduki for the help :)
JavaScript
1
24
24
1
<script>
2
3
var array = [];
4
5
{% for x in reservations %}
6
var date = new Date("{{x.isoformat}}").toISOString().split('T')[0];
7
array.push(date);
8
{% endfor %}
9
10
console.info(array)
11
12
$(function() {
13
$( "#id_start_date" ).datepicker(
14
{
15
beforeShowDay: function(date){
16
var string = jQuery.datepicker.formatDate('yy-mm-dd', date);
17
return [ array.indexOf(string) == -1 ];
18
}
19
}
20
);
21
});
22
23
</script>
24