John Davidson

javascript - Disable Specific Dates in DatePicker Based on php Result

0 comments
Message:


I am trying to disable already selected dates based on mysql query using php
I got the php result in javascript variable (var disabledDate).
Here is my code:


    $(function () { 
$('#school').change(function(){ // this is dropdown
var dis_date;
var id = $(this).val();
var dis_date;
$.ajax({
type: "GET",
url: "details.php",
data: "pass_id="+id,
success: success
});
function success(data) //data received here is ["2020/12/20","2020/12/19"]
{
var disabledDate = data.trim();
alert(disabledDate);
jQuery(function()
{
var date = new Date();
var currentMonth = date.getMonth();
var currentDate = date.getDate();
var currentYear = date.getFullYear();

$('#calender').datepicker
({
beforeShowDay:before,
dateFormat: 'yy-mm-dd',
minDate: new Date(currentYear, 11, 1),
disabledDates: disabledDate,
maxDate: new Date(currentYear, 12, 31)
});
});
var before= function(dt)
{
return [dt.getDay() === 0 || dt.getDay() === 6, ""];
}
}
});
});

Unable to disable the dates in datepicker.Any Help!!!



My Answer:
#datepicker').datepicker({ _ beforeShowDay: function(date){ _ var string = jQuery.datepicker.formatDate('yy-mm-dd', date); _ return [disabledDate.indexOf(string) == -1] _ } _ }); _ });_In this code, the beforeShowDay function is used to disable specific dates based on the disabledDate variable. The disabledDate variable should contain an array of dates in the format 'yy-mm-dd' that you want to disable. The indexOf method is used to check if the current date is in the disabledDate array, and return true if it is not found (meaning the date is not disabled).

Rate this post

3 of 5 based on 1375 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us