0

How to make datebox accept multiple formats?

asked 2013-03-20 14:12:57 +0800

CR13 gravatar image CR13
36 1

Hi!

Is there a possibility, to make a datebox accept e.g. "2013" or "13.02.2013" as input? I wanted to catch and edit the value of a datebox, but i realized, that the value will be changed before it reaches a method in viewmodel, therefore overriding "onChange" would not work :(

Is there another possibility (e.g. in editing a javascript-function) ?

Thanks in advance!!

delete flag offensive retag edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2013-03-27 05:21:41 +0800

benbai gravatar image benbai
2228 6
http://www.zkoss.org

updated 2013-03-27 05:25:17 +0800

You can try override the updateChange_ (client side function) and service (server side function), for example:

<zk xmlns:w="client">
    <zscript><![CDATA[
        // custom Datebox class
        class CustomDatebox extends Datebox {
            String _formats;
            // add setFormats function
            public void setFormats (String formats) {
                if (formats == null || formats.isEmpty()) {
                    _formats = "yyyy/MM/dd";
                } else {
                    _formats = formats;
                }
                setFormat(_formats.split(",")[0].trim());
            }
            // override service function
            public void service(org.zkoss.zk.au.AuRequest request, boolean everError) {
                final String cmd = request.getCommand();
                if ("onInputModified".equals(cmd)) {
                    String value = request.getData().get("value");
                    boolean found = false;
                    for (String format : _formats.split(",")) {
                        // change format
                        //
                        // NOTE: This also affect the format of input node at client side
                        setFormat(format.trim());
                        try {
                            Object val = coerceFromString(value);
                            setValue((Date)val);
                            found = true;
                            break;
                        } catch (Exception ex) {

                        }
                    }
                    if (!found) {
                        // no valid format found, set back to first one
                        setFormat(_formats.split(",")[0].trim());
                    }
                } else {
                    super.service(request, everError);
                }
            }
        }
    ]]></zscript>
    <!-- multipe format seperated by comma -->
    <datebox id="dbx" formats="yyyy/MM/dd, yyyy" use="CustomDatebox">
        <attribute w:name="updateChange_"><![CDATA[
            function () {
                var val = this.getInputNode().value;
                this.$updateChange_();
                // fire custom event if value changed
                if (this.getInputNode().value != val) {
                    this.fire('onInputModified',
                            {value: val}, // value
                            {toServer:true}, // opts
                            90);
                    this.getInputNode().value = val;
                }
            }
        ]]></attribute>
    </datebox>
    <button label="show value">
        <attribute name="onClick"><![CDATA[
            alert(dbx.getValue());
        ]]></attribute>
    </button>
</zk>
link publish delete flag offensive edit
Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow
1 follower

RSS

Stats

Asked: 2013-03-20 14:12:57 +0800

Seen: 37 times

Last updated: Mar 27 '13

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More