Skip to main content

Search and Top Navigation

#7279 closed feature (duplicate)

Opened April 25, 2011 07:00PM UTC

Closed October 11, 2012 05:54PM UTC

Datepicker: isDisabledDate: Check if a given date is currently disabled.

Reported by: sorpigal Owned by:
Priority: minor Milestone: 1.11.0
Component: ui.datepicker Version: 1.8.12
Keywords: Cc:
Blocked by: Blocking:
Description

For a project I'm working on I needed the ability to ask and answer the question "Is this date currently disabled in the datepicker?" for a specific date. I have not been able to find any built in way to do this by looking at the source, reading the documentation or searching google. If there is I apologize (also, what is it?)

In my scenario I have 2-4 datepickers on a page all of which have custom disabled dates as well as possibly minDate and maxDate and any of these datepickers may have the min and max dates may change after initialization. Tracking all of this separately makes no sense to me since the datepickers already track it anyway.

My crude solution is below. It's pretty much ripped from _generateHTML() without any attempt to understand how it works or make it nicer. Granted that it might be nicer to overload isDisabled perform this function if given a non-null parameter instead of creating a whole new method.

_isDisabledDateDatepicker: function(target, check_date){
	if(check_date) {
		var inst = $.data(target, PROP_NAME);

		var minDate = this._getMinMaxDate(inst, 'min');
		var maxDate = this._getMinMaxDate(inst, 'max');
		
		var beforeShowDay = this._get(inst, 'beforeShowDay');
		var showCurrentAtPos = this._get(inst, 'showCurrentAtPos');
		var selectOtherMonths = this._get(inst, 'selectOtherMonths');
		var drawMonth = inst.drawMonth - showCurrentAtPos;
		var daySettings = (beforeShowDay ?
			beforeShowDay.apply((inst.input ? inst.input[0] : null), [check_date]) : [true, '']);
		var otherMonth = (check_date.getMonth() != drawMonth);

		var unselectable = (otherMonth && !selectOtherMonths) || !daySettings[0] ||
			(minDate && check_date < minDate) || (maxDate && check_date > maxDate);
	
		return unselectable;
	}
	return false;
}

A small modification was obviously also required in $.fn.datepicker to add isDisabledDate to the list of methods.

I expect someone else may need this functionality sooner or later, so there it is.

Attachments (0)
Change History (7)

Changed July 28, 2011 12:23PM UTC by nicklevett comment:1

I don't think this is needed! To disable a date just use the beforeShowDay callback.

...beforeShowDay: function(date) {
var inst = $(this).data('datepicker');
var minDate = inst._getMinMaxDate(inst, 'min');
var maxDate = inst._getMinMaxDate(inst, 'max');
if (date < minDate || date > maxDate) {
return [false, ''];
}
return [true, ''];
}...

I haven't tested the above but you get the general idea.

Changed July 29, 2011 12:00AM UTC by sorpigal comment:2

Replying to [comment:1 nicklevett]:

I don't think this is needed! To disable a date just use the beforeShowDay callback.

I don't know if I don't understand you or if you don't understand me. I'm trying to find if a given day is disabled, not to disable a day. In my code I already use beforeShowDay to disable holiday dates, among others. I don't see how your code can answer the question "Is 2011-08-01 disabled?" given that the date may have been disabled by another beforeShowDay callback.

Changed September 26, 2011 07:01PM UTC by tad comment:3

Why not use a named callback for beforeShowDay and pass your test date to this function? It's the same code, anyway.

Changed September 27, 2011 10:17AM UTC by sorpigal comment:4

Replying to [comment:3 tad]:

Why not use a named callback for beforeShowDay and pass your test date to this function? It's the same code, anyway.

I don't see how that would solve the problem. Sample code, please?

beforeShowDay is only called when the datepicker redraws a particular date, not for all possible dates. What if the date I'm testing isn't currently visible?

Changed June 26, 2012 01:22AM UTC by scottgonzalez comment:5

type: enhancementfeature

Changed October 11, 2012 02:43PM UTC by scottgonzalez comment:6

milestone: 1.9.01.11.0

Changed October 11, 2012 05:54PM UTC by scottgonzalez comment:7

resolution: → duplicate
status: newclosed

Duplicate of #5446.