I have a daterangepicker which receives dates from the backend, to block those dates in the datepicker.
I have been reading the documentation of moment.js, and its respective method isBetween
, and also the plugin of range
, besides some examples that I have seen. Since I've realized that I can only add single dates to block them, which is great, but I have many days that should be blocked, which is not yet functional.
I tried to modify the method to use it but I really do not know how to do it since it does not throw me errors, it just does not work.
The javascript code that I have done so far is as follows:
// here receive the json array from PHP to JS
var daysData = <?= json_encode($data) ?>;
var isArea = {{ Auth::user()->area_id }}
var newA = [];
for( j of daysData){
let start = moment(j["start"]);
let end = moment(j["end"]);
for (let m = moment(start); m.diff(end, 'days') <= 0; m.add(1, 'days')) {
newA[m.format('DD/MM/YYYY')] = j;
}
}
console.log(daysData);
$('input[name="datefilter"]').daterangepicker({
isInvalidDate: function(date) {
var valid = false ; // default css class
let d = date.format("DD/MM/YYYY");
if(typeof newA[d] !== 'undefined'){
if(newA[d].area_id !== isArea){
if(newA[d].acept == 1){
valid = true;
}
}
}
return valid;
},
isCustomDate: function(date) {
var daySettings = 'day_green';
let d = date.format("DD/MM/YYYY");
if(typeof newA[d] !== 'undefined'){
daySettings = 'day_red';
if(newA[d].area_id == isArea){
if(newA[d].acept == 0){
daySettings = 'day_orange';
}
}
}
return daySettings;
},
EDITED and the json that I receive in the array after the loop in the view is this:
[17/08/2018: {…}, 18/08/2018: {…}, 22/08/2018: {…}, 23/08/2018: {…},
24/08/2018: {…}, …]
09/08/2018:
acept:0
area_id: null
end: "2018-08-10"
start: "2018-08-09"
title:"asd"
__proto__:Object
10/08/2018:{area_id: null, title: "asd", start: "2018-08-09", end: "2018-08-
10", acept: 0}
17/08/2018:{area_id: 1, title: "asd", start: "2018-08-17", end: "2018-08-
18", acept: 1}
18/08/2018:{area_id: 1, title: "asd", start: "2018-08-18", end: "2018-08-
22", acept: 1}
22/08/2018:{area_id: 1, title: "asdf", start: "2018-08-22", end: "2018-08-
24", acept: 1}
23/08/2018:{area_id: 1, title: "asd", start: "2018-08-22", end: "2018-08-
23", acept: 1}
24/08/2018:{area_id: 1, title: "asdf", start: "2018-08-22", end: "2018-08-
24", acept: 1}
length:0
__proto__:Array(0)
but it's really not happening to me, and I've seen that the isBetween method does not exactly serve me for what I want to do. And I think I'm not coding it as it should.
Thank you very much in advance. Greetings.