Solved

Jquery datepicker cannot properly disable specific dates.

Posted on 2010-09-03
6
469 Views
Last Modified: 2012-06-22
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
Comment
Question by:Solutionabc
  • 3
  • 3
6 Comments
 
LVL 22

Expert Comment

by:kadaba
ID: 33602202
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
 

Author Comment

by:Solutionabc
ID: 33602754
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
 

Author Comment

by:Solutionabc
ID: 33602819
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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 22

Expert Comment

by:kadaba
ID: 33602905
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
 
LVL 22

Accepted Solution

by:
kadaba earned 500 total points
ID: 33602931
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
 

Author Comment

by:Solutionabc
ID: 33602980
I got it!

thanks for your time kadaba.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
adding text to textboxes based on dropbox choice 13 43
PHP AJAX Wordpress 9 20
CSS: Making Pure CSS read more boxes thinner 5 31
Button and js nou working 3 7
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
OverviewThis article demonstrates a simple search form using AJAX. The purpose of the article is to demonstrate how to use the same code to render a page and javascript (JQuery) and AJAX to make subsequent calls to refine the results. The princip…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question