views:

209

answers:

2

i need to create a Dojo widget that lets users specify date & time. i found a sample implementation attached to an entry in the Dojo bug tracker. It looks nice and mostly works, but when i submit the form, the value sent by the client is not the user-selected value but the value sent from the server.

What changes do i need to make to get the widget to submit the date & time value?

Sample usage is to render a JSP with basic HTML tags (form & input), then dojo.addOnLoad a function which selects the basic elements by ID, adds dojoType attribute, and dojo.parser.parse()-es the page.

Thanks in advance.

The widget is implemented in two files. The application uses Dojo 1.3.

File 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);
            }
        }
    }       

    }
);

File 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) {}

});
A: 

It sounds like you want to use getValue. The convention now is to use _getValueAttr and then call attr("value") but I think that started in Dojo 1.4 and this code would need to be ported to use those new patterns.

Noe that value should be a Javascript Date object which would best be sent to the server using dojo.date.stamp.toISOString()

peller
A: 

This began to work fine after i added a "serialize" method to DateTimeCombo.js which builds exactly the output format i want.

This seems odd to me, since there is already a serialize implementation in _DateTimeTextBox.js that should output the value in the required ISO format.

davidbessire