Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 504
  • Last Modified:

Jquery datepicker cannot properly disable specific dates.

Hi,

okay... I am using the datepicker from here:
http://jqueryui.com/demos/datepicker/

I am pulling dates from a db table using ajax and php. The dates need to be disabled in the datepicker, I used the following code from this site and modified it slightly so that it accepts the disabledDays array dynamically from the db I changed the format of the date it accepts.

http://brandontruong.blogspot.com/2010/01/jquery-ui-datepicker-disable-specified.html

My problem is that not all the dates that should be disabled are being disabled in datepicker. I can alert the array and all the dates are showing up properly it's just the nationalDays function that seems to be reading it incorrectly.

See below for my modified version of the disabledDays code:

How can I make it disable ALL the dates properly?

thanks!
// Above the disabledDays is parsed as javascript from the ajax call php page.

function nationalDays(date) {
 var m = date.getMonth(), d = date.getDate(), y = date.getFullYear();

 //console.log('Checking (raw): ' + m + '-' + d + '-' + y);
 for (i = 0; i < disabledDays.length; i++) {
 

if(ArrayContains(disabledDays, y + '-' + 0 + (m+1) + '-' + d) || new Date() > date) {
 //console.log('bad: ' + (m+1) + '-' + d + '-' + y + ' / ' + disabledDays[i]);
 return [false];
 }
 }
 //console.log('good: ' + (m+1) + '-' + d + '-' + y);
 return [true];
}
function noWeekendsOrHolidays(date) {
 var noWeekend = jQuery.datepicker.noWeekends(date);
 return nationalDays(date);
}

/* taken from mootools */
function ArrayIndexOf(array,item,from){
 var len = array.length;
 for (var i = (from < 0) ? Math.max(0, len + from) : from || 0; i < len; i++){
 if (array[i] === item) return i;
 }
 return -1;
}
/* taken from mootools */
function ArrayContains(array,item,from){
 return ArrayIndexOf(array,item,from) != -1;
}

jQuery(document).ready(function() {

$("#datepicker").click(function() {
        $(this).datepicker().datepicker( "show" )
      

    });
     
 jQuery('#datepicker').datepicker({ 
minDate: new Date(2010, 0, 1), maxDate: new Date(2010, 12, 31), dateFormat: 'yy-mm-dd',constrainInput: true,beforeShowDay: noWeekendsOrHolidays


});


	});

Open in new window

0
Solutionabc
Asked:
Solutionabc
  • 3
  • 3
1 Solution
 
kadabaCommented:
Just check with this.

<html>
<head>
	<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.3/themes/base/jquery-ui.css" type="text/css" media="all" />
	<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"></script>
	<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.3/jquery-ui.min.js" type="text/javascript"></script>
			
	<script type="text/javascript">
	
	
		// disable 8th september, 17th september, 25th december
		var disabledDays = new Array("2010-9-8","2010-9-17","2010-12-24");
		
		function nationalDays(date) 
		{
			var m = date.getMonth() + 1;
			var d = date.getDate();
			var	y = date.getFullYear();
			if(ArrayContains(disabledDays, y + '-' + m + '-' + d)) {
				return [false];
			}
			return [true];
		}
		function noWeekendsOrHolidays(date) 
		{
			var noWeekend = jQuery.datepicker.noWeekends(date);
			if (noWeekend[0]) {
				return nationalDays(date);
			} else {
				return noWeekend;
			}
		}

		/* taken from mootools */
		function ArrayIndexOf(array,item,from){
		 var len = array.length;
		 for (var i = (from < 0) ? Math.max(0, len + from) : from || 0; i < len; i++){
		 if (array[i] === item) return i;
		 }
		 return -1;
		}
		/* taken from mootools */
		function ArrayContains(array,item,from){
		 return ArrayIndexOf(array,item,from) != -1;
		}

		
	$(document).ready(function() {

		$('#datepicker').datepicker({ 
			minDate: new Date(2010, 0, 1),
			maxDate: new Date(2010, 11, 31),
			dateFormat: 'yy-mm-dd',constrainInput: true,
			beforeShowDay: noWeekendsOrHolidays
		});


		});

	</script>

</head>
<body>
<div class="demo">
<p>Date: <input id="datepicker" type="text"></p>
</div>
</body>
</html>

Open in new window

0
 
SolutionabcAuthor Commented:
Thanks kadaba,

just one thing I cannot hard code my unavailable dates so I need to know how to get my ajax disabledDays array working.

with your changes it looks a lot better, I just need the datepicker to accept the date in this format:

"2010-09-08","2010-09-17","2010-12-24" (because that is the format they are inputed and stored in the db)

Rather than

"2010-9-8","2010-9-17","2010-12-24"

the datepicker will not recognize my dates if they have a 0 in the month or day.

how can I fix that?
thank you!
0
 
SolutionabcAuthor Commented:
I guess I could do two things,

1. make datepicker accept my 0's in the month and day

or

2. strip out the zero's from the month and day from the disabledDays array.

which one is easier and how could I do it?

thanks a ton.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
kadabaCommented:
if var disabledDays = new Array("2010-09-8","2010-09-17","2010-12-24"); this is the array which will be fetched then replace var m = date.getMonth() + 1; with                                                       var m = date.getMonth() + 1;
            m = m >= 10?m: "0"+m;
This should do it. I hope you are able to get the array  filled up with the required dates.
0
 
kadabaCommented:
you can use the same for the date as well... here is the complete code... only variation is two lines of code.

<html>
<head>
	<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.3/themes/base/jquery-ui.css" type="text/css" media="all" />
	<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"></script>
	<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.3/jquery-ui.min.js" type="text/javascript"></script>
			
	<script type="text/javascript">
	
	
		// disable 8th september,6th september, 7th september 15th september, 25th december
		var disabledDays = new Array("2010-09-08","2010-09-06","2010-09-07","2010-09-15","2010-12-24");
		
		function nationalDays(date) 
		{
			var m = date.getMonth() + 1;
			// Added this line
			m = m >= 10?m: "0"+m;
			var d = date.getDate();
			// added this line
			d = d >= 10?d: "0"+d;
			var	y = date.getFullYear();
			if(ArrayContains(disabledDays, y + '-' + m + '-' + d)) {
				return [false];
			}
			return [true];
		}
		function noWeekendsOrHolidays(date) 
		{
			var noWeekend = jQuery.datepicker.noWeekends(date);
			if (noWeekend[0]) {
				return nationalDays(date);
			} else {
				return noWeekend;
			}
		}

		/* taken from mootools */
		function ArrayIndexOf(array,item,from){
		 var len = array.length;
		 for (var i = (from < 0) ? Math.max(0, len + from) : from || 0; i < len; i++){
		 if (array[i] === item) return i;
		 }
		 return -1;
		}
		/* taken from mootools */
		function ArrayContains(array,item,from){
		 return ArrayIndexOf(array,item,from) != -1;
		}

		
	$(document).ready(function() {

		$('#datepicker').datepicker({ 
			minDate: new Date(2010, 0, 1),
			maxDate: new Date(2010, 11, 31),
			dateFormat: 'yy-mm-dd',constrainInput: true,
			beforeShowDay: noWeekendsOrHolidays
		});


		});

	</script>

</head>
<body>
<div class="demo">
<p>Date: <input id="datepicker" type="text"></p>
</div>
</body>
</html>

Open in new window

0
 
SolutionabcAuthor Commented:
I got it!

thanks for your time kadaba.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now