我的Dijit DateTimeCombo小部件不会在表单提交上发送选定的值

时间:2010-05-19 14:13:35

标签: dojo datetimepicker dijit.form

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

我需要做些什么更改才能让小部件提交日期&时间价值?

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

提前致谢。

小部件以两个文件实现。该应用程序使用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) {}

});

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

在我向DateTimeCombo.js添加“serialize”方法之后,这开始工作正常,它完全构建了我想要的输出格式。

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

相关问题