Opened 8 years ago

Closed 7 years ago

#7279 closed feature (duplicate)

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.

Change History (7)

comment:1 Changed 8 years ago by nicklevett

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.

comment:2 in reply to:  1 Changed 8 years ago by sorpigal

Replying to 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.

comment:3 Changed 8 years ago by tad

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

comment:4 in reply to:  3 Changed 8 years ago by sorpigal

Replying to 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?

comment:5 Changed 7 years ago by Scott González

Type: enhancementfeature

comment:6 Changed 7 years ago by Scott González

Milestone: 1.9.01.11.0

comment:7 Changed 7 years ago by Scott González

Resolution: duplicate
Status: newclosed

Duplicate of #5446.

Note: See TracTickets for help on using tickets.