1

我需要创建一个允许用户指定日期和时间的 Dojo 小部件。我在 Dojo 错误跟踪器的条目中找到了一个示例实现。它看起来不错并且大部分都可以工作,但是当我提交表单时,客户端发送的值不是用户选择的值,而是从服务器发送的值。

我需要进行哪些更改才能让小部件提交日期和时间值?

示例用法是使用基本 HTML 标记(表单和输入)呈现 JSP,然后 dojo.addOnLoad 一个按 ID 选择基本元素的函数,添加 dojoType 属性和 dojo.parser.parse()-es 页面。

提前致谢。

该小部件在两个文件中实现。该应用程序使用 Dojo 1.3。

文件 1:DateTimeCombo.js

dojo.provide("dojox.form.DateTimeCombo");

dojo.require("dojox.form._DateTimeCombo");
dojo.require("dijit.form._DateTimeTextBox");

dojo.declare(
    "dojox.form.DateTimeCombo", 
    dijit.form._DateTimeTextBox,
    {
        baseClass: "dojoxformDateTimeCombo dijitTextBox",
        popupClass: "dojox.form._DateTimeCombo",
        pickerPostOpen: "pickerPostOpen_fn",        
        _selector: 'date',      
        constructor: function (argv) {},

        postMixInProperties: function()
        {               
            dojo.mixin(this.constraints, {
                /*
                datePattern: 'MM/dd/yyyy HH:mm:ss',  
                timePattern: 'HH:mm:ss',
                */
                datePattern: 'MM/dd/yyyy HH:mm',
                timePattern: 'HH:mm',

                clickableIncrement:'T00:15:00',
                visibleIncrement:'T00:15:00', 
                visibleRange:'T01:00:00'
            });
            this.inherited(arguments);
        },

    _open: function ()
    {
        this.inherited(arguments);
        if (this._picker!==null && (this.pickerPostOpen!==null && this.pickerPostOpen!==""))
        {
            if (this._picker.pickerPostOpen_fn!==null)
            {
                this._picker.pickerPostOpen_fn(this);
            }
        }
    }       

    }
);

文件 2:_DateTimeCombo.js

dojo.provide("dojox.form._DateTimeCombo");

dojo.require("dojo.date.stamp");
dojo.require("dijit._Widget");
dojo.require("dijit._Templated");
dojo.require("dijit._Calendar");
dojo.require("dijit.form.TimeTextBox");
dojo.require("dijit.form.Button");

dojo.declare("dojox.form._DateTimeCombo",
    [dijit._Widget, dijit._Templated],
{

    // invoked only if time picker is empty 
    defaultTime: function () {
        var res= new Date();
        res.setHours(0,0,0);
        return res;
    },

    // id of this table below is the same as this.id
    templateString:
    "   <table  class=\"dojoxDateTimeCombo\" waiRole=\"presentation\">\
        <tr class=\"dojoxTDComboCalendarContainer\">\
        <td>\
            <center><input dojoAttachPoint=\"calendar\" dojoType=\"dijit._Calendar\"></input></center>\
        </td>\
        </tr>\
        <tr class=\"dojoxTDComboTimeTextBoxContainer\">\
        <td>\
            <center><input dojoAttachPoint=\"timePicker\" dojoType=\"dijit.form.TimeTextBox\"></input></center>\
        </td>\
        </tr>\
        <tr><td><center><button dojoAttachPoint=\"ctButton\" dojoType=\"dijit.form.Button\">Ok</button></center></td></tr>\
        </table>\
    ",

    widgetsInTemplate: true,

    constructor: function(arg) {},

    postMixInProperties: function() {       
        this.inherited(arguments);      
    },

    postCreate: function() {
        this.inherited(arguments);  
        this.connect(this.ctButton, "onClick", "_onValueSelected");
    },

    // initialize pickers to calendar value
    pickerPostOpen_fn: function (parent_inst) {
        var parent_value = parent_inst.attr('value');
        if (parent_value !== null) {
            this.setValue(parent_value);
        }
    },

    // expects a valid date object
    setValue: function(value) {
        if (value!==null) {
            this.calendar.attr('value', value);
            this.timePicker.attr('value', value);
        }       
    },

    // return a Date constructed date in calendar & time in time picker.
    getValue: function() {
        var value = this.calendar.attr('value');
        var result=value;       
        if (this.timePicker.value !== null) {
            if ((this.timePicker.value instanceof Date) === true) {
                result.setHours(this.timePicker.value.getHours(),
                this.timePicker.value.getMinutes(),
                this.timePicker.value.getSeconds());
                return result;
            }
        } else {
            var defTime=this.defaultTime();
            result.setHours(defTime.getHours(),
                defTime.getMinutes(),
                defTime.getSeconds());
            return result;                  
        }
    },

    _onValueSelected: function() {
        var value = this.getValue();
        this.onValueSelected(value);
    },

    onValueSelected: function(value) {}

});
4

2 回答 2

0

听起来您想使用 getValue。现在的约定是使用 _getValueAttr 然后调用 attr("value") 但我认为这从 Dojo 1.4 开始,并且需要移植此代码以使用这些新模式。

不,该值应该是一个 Javascript Date 对象,最好使用 dojo.date.stamp.toISOString() 将其发送到服务器

于 2010-05-21T14:08:07.860 回答
0

在我向 DateTimeCombo.js 添加了一个“序列化”方法后,这开始正常工作,它构建了我想要的输出格式。

这对我来说似乎很奇怪,因为 _DateTimeTextBox.js 中已经有一个序列化实现,它应该以所需的 ISO 格式输出值。

于 2010-05-21T14:30:08.637 回答