I get an array that looks like this:
var dateUnvailableCalendar = {{ datesUnvailable|json_encode|raw }};
and I want to transform it to look like this:
{
'from': '2023-03-06 00:00',
'to': '2023-03-13 00:00'
},
{
'from': '2023-03-01 00:00',
'to': '2023-03-01 00:00'
}
do you know how i could do this please?
Thank you for help.
WORK
calendar.flatpickr({
disable: [
function (date) {
return date.getDay()===dayWeek[0]||
date.getDay()===dayWeek[1]||
date.getDay()===dayWeek[2]||
date.getDay()===dayWeek[3]||
date.getDay()===dayWeek[4]||
date.getDay()===dayWeek[5]||
date.getDay()===dayWeek[6]
},
{
'from': '2023-03-06 00:00',
'to': '2023-03-13 00:00'
},
{
'from': '2023-03-01 00:00',
'to': '2023-03-01 00:00'
}
],
minDate: "today",
onChange: function (selectedDates, dateStr) {
calendarHandleChange(dateStr);
}
});
in this example the dates that will be deactivated will be those that are in the function.
calendar.flatpickr({
disable: [
function(date) {
return date.getDay() === dayWeek[0] ||
date.getDay() === dayWeek[1] ||
date.getDay() === dayWeek[2] ||
date.getDay() === dayWeek[3] ||
date.getDay() === dayWeek[4] ||
date.getDay() === dayWeek[5] ||
date.getDay() === dayWeek[6]
},
[
{"from":"2023-03-06 00:00","to":"2023-03-13 00:00"},
{"from":"2023-03-01 00:00","to":"2023-03-01 00:00"}
]
],
minDate: "today",
onChange: function (selectedDates, dateStr) {
calendarHandleChange(dateStr);
}
});
do you know why it doesn’t work sending the array to it?
2
Answers
Take a look at what you’re doing here:
What is
dateUnvailableCalendar
? Is it an array? If so then the resulting structure is this:That is, this
disable
property is an array with two elements, one of which is a function and the other of which is another array.It sounds like you want to append the contents of this
dateUnvailableCalendar
array to create one larger array. You can use the spread operator, for example:Edit: The functionality is demonstrated here:
(Credit to this answer for providing the minimal reproducible example that the OP refuses to.)
Works on more than 2 elements of
dateUnvailableCalendar
Your problem will be solved if you try to use the index of
dateUnvailableCalendar
in flatpickr disable property (e.g.dateUnvailableCalendar[0]
). Try this: