Friday, 8 February 2013

ZK Passing Parameter between two files using MVVM–Part 1


This is the first series of articles about Passing parameter between two zul files using MVVM Design pattern.This article will focus on the how to pass values from the parent window to child window opened in modal mode.
ZK Version : ZK 6.5.0 CE

So What we are going to do ?
1. Assume that you have small screen with a button to show another screen which opened as modal.
2. Now on Clicking of the Button, we will show the Second screen and also we will pass some arguments to the destination window.
Step 1:
If you are new ZK, then first set the development environment by following this document.

Step 2:

Let us create the Project in eclipse. Click File -> New -> ZK Project . Let us name the project as ZKMVVMPassingParameter1

Step 3:
Now let us create a zul file in the name of ParentWindow.zul. This zul is pretty simple contains two labels with textboxes and one button. So on clicking the button, we will call another zul file and also we will also pass the value of the text boxes typed by the user.
<?page title="Parent Window" contentType="text/html;charset=UTF-8"?>
    <window title="Parent Window" border="normal"
        viewModel="@id('myvm') @init('com.demo.ParentVM')">
        <label value="First Name"></label>
        <textbox id="firstName" value="@bind(myvm.firstName)"></textbox>
        <label value="Last Name"></label>
        <textbox id="lastName" value="@bind(myvm.lastName)"></textbox>
        <button label="Show Window" id="showWindow"
Step 4:
Now we will add our VM to our Parentwindow.zul as follows. Create a package called "" and create a java file called "" Now on button click, we will call the ChildWindow.zul with the arguments as shown here.

package com.demo;
import java.util.HashMap;
import org.zkoss.bind.annotation.Command;
import org.zkoss.zk.ui.Executions;
public class ParentVM {
    private String firstName;
    private String lastName;
    public String getFirstName() {
        return firstName;
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    public String getLastName() {
        return lastName;
    public void setLastName(String lastName) {
        this.lastName = lastName;
    public void showChildWindow() {
        final HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("firstName", this.firstName);
        map.put("lastName", this.lastName);
        Executions.createComponents("ChildWindow.zul", null, map);
Step 5:
Now let us create a zul file in the name of ChildWindow.zul. In this zul file also, we have two labels and two text boxes in which by default when this zul file is called we are going to receive the arguments and fill the values.

<?page title="Child Window" contentType="text/html;charset=UTF-8"?>
    <window id="childwindow" title="Child Window" border="normal" mode="modal" width="20%">
        <label value="First Name"></label>
        <textbox value="${arg.firstName}"></textbox>
        <label value="Last Name"></label>
        <textbox value="${arg.lastName}"></textbox>
        <button label="Close" onClick="childwindow.detach()"></button>

Step 6:
That’s all. Now you can run ParentWindow.zul and type some value for first and last Name and click the button to show the values in the childwindow.zul
1. Type some values for the text boxes
2. Click the button, now you can see the values are populated.

Project Structure
You can download the source here.


No comments:

Post a Comment