Friday, 6 July 2012

ZK Component extend

In this post, we will see how we can create our component by extending ZK Component. All the jquery plugin can be used in ZK. The following example will explain how to create Phonebox, Zip Code and Tax ID.

For your further reference, you can see the following http://www.zkoss.org/zkdemo/effects/form_effect
You can download the jquery plugin from http://digitalbush.com/projects/masked-input-plugin/

ZK Version 6
Project Name : MyComponents
Project Structure:

image

Download the jquery plugin and Add in webcontent/js folder.

Here is the source for other files

Demo.jul

<?page title="new page title" contentType="text/html;charset=UTF-8"?>
<zk>
<window id="myWin" title="new page title" border="normal" >
<label value="Phone :" />
<phonebox id="phone" width='150px' />
<label value="Extension :" />
<Extension id="txtPhoneExt" />
<label value="zipCode :" />
<ZipCode id="txtZipCode" />
<label value="Tax ID:" />
<EIN id="txtEIN" />
<label value="Alpha Numeric :" />
<AlphaNumeric />
</window>
</zk>


AlphaNumeric.java


package components;

import org.zkoss.zul.Textbox;

public class AlphaNumeric extends Textbox {
private static final long serialVersionUID = 1L;

public AlphaNumeric() {
setWidgetListener("onBind", "jq(this).mask('******');");
}
}


EIN.java

package components;
import org.zkoss.zul.Textbox;

public class EIN extends Textbox {
private static final long serialVersionUID = 1L;
public EIN() {
setWidgetListener("onBind", "jq(this).mask('99-9999999');");
}
}

Extension.java

package components;

import org.zkoss.zul.Textbox;

public class Extension extends Textbox {
private static final long serialVersionUID = 1L;

public Extension() {
setWidgetListener("onBind", "jq(this).mask('99999');");
}
}

NPI.java

package components;

import org.zkoss.zul.Textbox;

public class NPI extends Textbox {
private static final long serialVersionUID = 1L;
public NPI() {
setWidgetListener("onBind", "jq(this).mask('9999999999');");
}
}

Phonebox.java

package components;
import org.zkoss.zul.Textbox;

public class Phonebox extends Textbox {
private static final long serialVersionUID = 1L;
public Phonebox() {
setWidgetListener("onBind", "jq(this).mask('(999) 999-9999');");
}
}

ZipCode.java

package components;

import org.zkoss.zul.Textbox;

public class ZipCode extends Textbox {
private static final long serialVersionUID = 1L;

public ZipCode() {
setWidgetListener("onBind", "jq(this).mask('99999-9999');");
}
}


In the webcontent folder, create a file in the name of my-addon.xml (you can keep any file name)


<?xml version="1.0" encoding="UTF-8"?>
<language-addon>
<addon-name>ecosmos</addon-name>
<language-name>xul/html</language-name>

<component>
<component-name>phonebox</component-name>
<component-class>components.Phonebox
</component-class>
<extends>textbox</extends>
</component>

<component>
<component-name>EIN</component-name>
<component-class>components.EIN</component-class>
<extends>textbox</extends>
</component>

<component>
<component-name>NPI</component-name>
<component-class>components.NPI</component-class>
<extends>textbox</extends>
</component>

<component>
<component-name>Extension</component-name>
<component-class>components.Extension
</component-class>
<extends>textbox</extends>
</component>


<component>
<component-name>ZipCode</component-name>
<component-class>components.ZipCode
</component-class>
<extends>textbox</extends>
</component>

<component>
<component-name>AlphaNumeric</component-name>
<component-class>components.AlphaNumeric
</component-class>
<extends>textbox</extends>
</component>





<addon-name>my.extension</addon-name><!-- any name you like -->
<javascript src="js/jquery.maskedinput-1.3.js" /> <!-- assume you package it as /myjs/foo.js -->

</language-addon>


And in the ZK.Xml , add the reference for the above file as follows


<?xml version="1.0" encoding="UTF-8"?>

<!-- Created by ZK Studio -->

<zk>
<device-config>
<device-type>ajax</device-type>
<timeout-uri>/timeout.zul</timeout-uri><!-- An empty URL can cause the
browser to reload the same URL -->
</device-config>


<language-config>
<addon-uri>/WEB-INF/my-addon.xml</addon-uri>
</language-config>

</zk>


Now you can run the demo.zul and check the output


image


You can download source here

2 comments:

  1. Can you post any Macro Component Demo in zk6

    ReplyDelete
  2. I delight in the content on your internet site. Cheers!

    Generic Viagra

    ReplyDelete