2013-07-16 60 views
1

环境:Dojo.1.8.4dijit.DateTextBox与dojox.widget.Calendar - 初始化

如果文字框不包含一个有效的日期,我想在过去的某个日期开始的日历弹出(这是出生日期输入框)。达到这个目标的最好方法是什么?

<div maxlength="12" 
    data-dojo-type="dijit/form/DateTextBox" 
    data-dojo-props="required: true, constraints:{min:'1880-01-01', 
     max: new Date()}, popupClass: 'dojox.widget.Calendar'"> 
</div> 

我希望能够在上面放置一个'startDate'参数或类似的东西,使弹出窗口的构造函数能够把它拿起来并使用它。

看起来好像dojox.widget._CalendarBase在其构造函数中将日期设置为当前日期。 (实际上似乎将其设置在构造函数和声明中)。

回答

0

这是如何扩展dojox.widget.Calendar以接受弹出窗口传递的currentFocus。像上面的aspect方法,暂时禁用的onChange为了防止值被放置在父文字框

define([ 
    "dojo/_base/declare", 
    "dojox/widget/Calendar" 
], function(declare, calendar){ 
    return declare("my.dijit.Calendar", [calendar], { 
     postCreate: function() { 
      this.inherited(arguments); 

      var oldOnChange = this.onChange; 
      this.onChange = function(){}; 

      this.set('value', this.parseInitialValue(this.currentFocus)); 

      this.onChange = oldOnChange; 
     } 
    }); 
}); 
2

注意:如果您使用的是dijit/Calendar下拉列表中的默认值,请使用dropDownDefaultValue属性,因为@vogomatix在其他答案中说。

以下是如果您使用dojox/widget/Calendar作为下拉菜单的情况。


默认情况下,弹出窗口设置为文本框的当前值,如果为null,则会使用当前日期。

你可以做

1)设置文本框的你想要的默认是

OR

2什么值)的使用方面,当它来设置日历中的价值被打开。

require([ 
    'dojo/dom', 
    'dojo/aspect', 
    'dijit/form/DateTextBox', 
    'dojox/widget/Calendar' 
], function(dom, aspect, DateTextBox, Calendar){ 

    var dt = new DateTextBox({ popupClass: Calendar }, dom.byId('dtText')); 

    aspect.after(dt, 'openDropDown', function() { 

     // only default if there is no value 
     if(dt.get('value') == null) { 

      // Do not set the text box when changing value, 
      // so temporarily override the onchange function 
      var oldOnChange = dt.dropDown.onChange; 
      dt.dropDown.onChange = function(){}; 

      dt.dropDown.set('value', new Date(1980, 7, 4)); // default to August 4th, 1980 

      dt.dropDown.onChange = oldOnChange; 
     } 
    });  
}); 

看到它在行动:

http://jsfiddle.net/cswing/kQYhQ/

+0

三江源 - 每次我问这些问题我都学到新的.... – vogomatix

+0

似乎有DateTextBox('_DateTimeTextBox')的一个'dropDownDefaultValue'属性(如果我可以得到它的功能,它似乎是一个更简单的路线!:-) – vogomatix

+1

是的,在查看源代码时我没有看到。它最终将该值设置为currentFocus属性。好找 –

1

一种解决方案是设置dropDownDefaultValue,这与标准的日历的方式。

var dt = new DateTextBox({dropDownDefaultValue: new Date(1950,1,1)}, 
    dom.byId('dtText')); 

dropDownDefaultValue被传递给在currentFocus通过弹出的形式压延。不幸的是,dojox.widget.Calendar不能识别currentFocus,并且要么扩展该类来执行此操作,要么使用上面的aspect方法。