Solved

Jquery datepicker cannot properly disable specific dates.

Posted on 2010-09-03
6
474 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
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.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
powershell script 9 63
Underline in nav for the page that you are currently on. 1 22
how can i turn the array into an object 4 39
Filktering Alphabetically 8 28
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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 …

790 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