Solved

Jquery datepicker cannot properly disable specific dates.

Posted on 2010-09-03
6
487 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: 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!

 
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

717 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