-
FEATURED COMPONENTS
First time here? Check out the FAQ!
1 | initial version | |
You can try override the updateChange_ (client side function) and service (server side function), for example:
<zk xmlns:w="client">
<zscript><![CDATA[
class CustomDatebox extends Datebox {
String _formats;
public void setFormats (String formats) {
if (formats == null || formats.isEmpty()) {
_formats = "yyyy/MM/dd";
} else {
_formats = formats;
}
setFormat(_formats.split(",")[0].trim());
}
public void service(org.zkoss.zk.au.AuRequest request, boolean everError) {
final String cmd = request.getCommand();
if ("onInputModified".equals(cmd)) {
System.out.println("service");
String value = request.getData().get("value");
System.out.println(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;
System.out.println(found);
break;
} catch (Exception ex) {
System.out.println("exception");
}
}
if (!found) {
setFormat(_formats.split(",")[0].trim());
}
} else {
super.service(request, everError);
}
}
}
]]></zscript>
<datebox id="dbx" formats="yyyy/MM/dd, yyyy" use="CustomDatebox">
<attribute w:name="updateChange_"><![CDATA[
function () {
var val = this.getInputNode().value;
this.$updateChange_();
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>
2 | No.2 Revision |
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)) {
System.out.println("service");
String value = request.getData().get("value");
System.out.println(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;
System.out.println(found);
break;
} catch (Exception ex) {
System.out.println("exception");
}
}
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>