Login Register

DateTextBox - Mondays only selectable

I want my users to only be able to select Mondays from a datetextbox - any ideas on how this is possible?

Thanks, Martin.

you should be able to do

you should be able to do this... set isDisabledDate="isMonday" in your markup... and then..
function isMonday(date, locale){
    return (date.getDay()==1) ? false : true;
}
-Karl

actually, not sure this will work yet...

We don't support isDisabledDate on DateTextBox, just on _Calendar, so you'd probably have to use _Calendar directly, for now (which doesn't do a11y). There's an enhancement request open for 1.1 to add this to DateTextBox.

Yeah, the logic is sorta flipped, eh?

function isNotMonday(date){
return date.getDay() != 1;
}

:-)

DateTextBox and isDisabledDate

Was this ever added? I haven't been able to find it and actually need it right now..

Solution for Dojo 1.2

I've been trying to do a similar kind of thing. Initially I tried declaring a custom version of dijit._Calendar and overriding the isDisabledDate method in that then overriding the popup class. This didn't work, after some code exploring I realized that isDisabledDate is overridden in _DateTimeTextBox in the _open method.

So, the only way to achieve this functionality is to subclass DateTextBox and override _open, then change the behavior of the isDisabledDate function there. So declare a class like so and use it in place of dijit.form.DateTextBox. In this case, I return true if out of range, otherwise I return true if it's a weekend.

dojo.declare(
	"altTesting.CustomDateTextBox",
	[dijit.form.DateTextBox],
	{
		_open: function(){
			// summary:
			//	opens the TimePicker, and sets the onValueSelected value

			if(this.disabled || this.readOnly || !this.popupClass){return;}

			var textBox = this;

			if(!this._picker){
				var PopupProto=dojo.getObject(this.popupClass, false);
				this._picker = new PopupProto({
					onValueSelected: function(value){
						if(textBox._tabbingAway){
							delete textBox._tabbingAway;
						}else{
							textBox.focus(); // focus the textbox before the popup closes to avoid reopening the popup
						}
						setTimeout(dojo.hitch(textBox, "_close"), 1); // allow focus time to take

						// this will cause InlineEditBox and other handlers to do stuff so make sure it's last
						dijit.form._DateTimeTextBox.superclass._setValueAttr.call(textBox, value, true);
					},
					lang: textBox.lang,
					constraints: textBox.constraints,
					isDisabledDate: function(/*Date*/ date){
						// summary:
						// 	disables dates outside of the min/max of the _DateTimeTextBox
						var compare = dojo.date.compare;
						var constraints = textBox.constraints;
						var isDisabled = constraints && (constraints.min && (compare(constraints.min, date, "date") > 0) || 
							(constraints.max && compare(constraints.max, date, "date") < 0));
						
						if(isDisabled){
							return true;
						}
						
						return dojo.date.locale.isWeekend(date, dojo.locale);
					}
				});
				this._picker.attr('value', this.attr('value') || new Date());
			}
			if(!this._opened){
				dijit.popup.open({
					parent: this,
					popup: this._picker,
					around: this.domNode,
					onCancel: dojo.hitch(this, this._close),
					onClose: function(){ textBox._opened=false; }
				});
				this._opened=true;
			}
			
			dojo.marginBox(this._picker.domNode,{ w:this.domNode.offsetWidth });
		}
	}
);