?
Solved

Jquery datepicker cannot properly disable specific dates.

Posted on 2010-09-03
6
Medium Priority
?
491 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
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

 
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 2000 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

Stack Overflow Podcast - Developer Story

Welcome to the Stack Overflow podcast recorded Thursday July 20 at Stack Overflow Headquearters in NYC. Your hosts today are podcast regulars Jay Hanlon, David Fullerton, and Ilana Yitzhaki, plus the quite irregular Matt Sherman (Stack Overflow Engineering Manager extraordinaire)

Question has a verified solution.

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

Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
This article discusses how to implement server side field validation and display customized error messages to the client.
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses

777 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