1

环境:Dojo.1.8.4

如果 DateTextBox 不包含有效日期,我希望在过去的某个日期启动日历弹出窗口(这是一个出生日期输入框)。实现这一目标的最佳方法是什么?

<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在其构造函数中将日期设置为当前日期。(实际上似乎在构造函数和声明中都设置了它)。

4

3 回答 3

2

注意:如果您使用的是dijit/Calendar下拉默认值,则使用dropDownDefaultValue@vogomatix 在另一个答案中所说的属性。

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


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

你可以做

1)将文本框的值设置为您想要的默认值

或者

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/

于 2013-07-16T13:08:35.700 回答
1

一种解决方案是 set dropDownDefaultValue,它适用于标准日历。

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

dropDownDefaultValuecurrentFocus以弹出窗口的形式传递给日历。不幸的是dojox.widget.Calendar,无法识别currentFocus并且可以通过扩展类来实现这一点,或者使用上面的方面方法。

于 2013-07-16T14:31:33.590 回答
0

这是扩展 dojox.widget.Calendar 以接受currentFocus弹出窗口传递的方法。和aspect上面的方法一样,它暂时禁用 onChange 以防止值被放置在父 DateTextBox 中

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;
       }
   });
});
于 2013-07-16T15:59:47.900 回答