how do i load a list of objects in a view model and display it in another zul?

asked 2012-06-11 08:40:20 +0800

boskonovic gravatar image boskonovic
46 1

I'm newbie with zk6 and i'm trying to implement a search method in my ViewModel class. in this method i want to load a list of objects and navigate to another page where i can display the list. I managed to load data and display it in the seach.zul but i couldn't display my list of objects in another(result.zul) page.

	@NotifyChange({ "searchBean", "slides" })
	public void doSearch() {

		slides = new ListModelList<SlideBean>(data.getSlides());


this is the result page:

<?page title="slides" contentType="text/html;charset=UTF-8"?>
	<window id="w_slides" title="SLIDES" border="normal"
		viewModel="@id('vm') @init('eu.dedalus.dia.ais.slides.checkout.ui.SearchVM')">

				.hb{ float:right; }

			<hbox sclass="hb">

				<button label="INDIETRO" />
				<button label="ESEGUI" />

			<listbox  model="@load(vm.slides)" >
						<checkbox />
					<listheader label="Icon for status" />
					<listheader label="Icon for details" />
					<listheader label="VETRINO" />
					<listheader label="ESAME" />
					<listheader label="PAZIENTE" />
					<listheader label="CONTENITORE" />
					<listheader label="SPEDIZIONE" />
				<template name="model" var="slide">
							<checkbox />
						<listcell label="@load(slide.state.id)" />
						<listcell label="#" />
						<listcell label="@load(slide.id)" />
						<listcell label="@load(slide.exam.id)" />
						<listcell label="@load(slide.exam.patient.firstName)" />
						<listcell label="@load(slide.box.id)" />
						<listcell label="@load(slide.box.pack.id)" />




delete flag offensive retag edit

12 Replies

Sort by ยป oldest newest

answered 2012-06-11 15:22:57 +0800

Senthilchettyin gravatar image Senthilchettyin flag of India
2625 3 8

You can check an example here

link publish delete flag offensive edit

answered 2012-06-11 08:56:14 +0800

ajvan gravatar image ajvan

Try something like this:

Put this peace of code before you call Executions.sendRedirect:

Map<String, Object> args = new HashMap<String, Object>();
args.put("slides", slides);
BindUtils.postGlobalCommand(null, null, "setSlides", args);

and in the SearchVM define a new method with:

public void setSlides(@BindingParam("slides")  ListModelList<SlideBean> newSlides) {
        slides = newSlides;       

You post a global command which is a way for communication between two ViewModels, and you pass a datastructure as a parameter.

I didn't test it, but I think it would work. :)

You can read about this here:
and here


link publish delete flag offensive edit

answered 2012-06-11 09:00:11 +0800

ajvan gravatar image ajvan



link publish delete flag offensive edit

answered 2012-06-11 12:26:11 +0800

boskonovic gravatar image boskonovic
46 1
Thank you for your quick answer. I tried the solution you mentioned with no success. I decided to test the GlobalCommand thing with a smaller code. the first ViewModel:
public class TestVM {
	public void testGC(){

the second ViewModel:
public class TestVM2 {
	public void global(){

and my zul pages:
	<window title="test1" border="normal"
		viewModel="@id('vm') @init('ui.TestVM')">
		<button label="btn" onClick="@command('testGC') @global-command('global')"/>

<?page title="test2" contentType="text/html;charset=UTF-8"?>
	<window title="test2" border="normal"
		viewModel="@id('vm') @init('ui.TestVM2')">
		New Content Here!

i also tried to call the global command using using BindUtils :
BindUtils.postGlobalCommand(null, null, "global", null);
does any one know if i did something wrong? or may be i'm not understing the GlobalCommand feature at all.
link publish delete flag offensive edit

answered 2012-06-11 12:44:29 +0800

ajvan gravatar image ajvan

You need to annotate the GlobalCommand since you can have more than one. So use @GlobalCommand("CommandName")

link publish delete flag offensive edit

answered 2012-06-11 13:22:07 +0800

boskonovic gravatar image boskonovic
46 1

I added the annotation @GlobalCommand("global") but that didn't help.

link publish delete flag offensive edit

answered 2012-06-12 03:52:26 +0800

rdgrimes gravatar image rdgrimes
735 7

Seems to me your global command didn't work because, before it's executed out of the first zul, it does a redirect that passes control to a second zul. So, the global command never gets executed.

But, here's a couple methods that I have used, in an MVVM, to make data accessible from one zul to another.

Method 1: bind both zul's to the same instance of the viewmodel. Because each zul @init will actually create a new instance of the viewmodel, they cannot possibly share data across viewmodel instances. But, you can do what I have done, which is to @init each zul to a viewmodelproxy, which turns around and calls a shared common viewmodel. See my "workaround" here: http://tracker.zkoss.org/browse/ZK-1077

Method 2: The data produced from zul 1's viewmodel can be stored as a session attribute and then retrieved by zul 2's viewmodel. See Sessions.getCurrent().setAttribute()

Method 3: Publish the data from zul 1's viewmodel into an EventQueue, and then subscribe to it in zul 2's viewmodel. Obviously, this means both zuls must be created and @inited before pushing the data out of zul 1's viewmodel and into the EventQueue. In other words, if you create zul 2 (the result zul) subsequent to zul 1's (the search), it probably won't work.

link publish delete flag offensive edit

answered 2012-06-12 06:37:57 +0800

bozo gravatar image bozo

Hi rdgrimes ,

I need exactly the same thing, only the use case is that I have several application "screens" (ZULs binded to a same VM).

What is the proper way of doing this, except your "hack"? To use MVC pattern instead of MVVM?

Using one ZUL where all the "screens" are defined and then navigating between them using the API (thus the parent VM is always the same)?

I'm also new to ZK, and this question is more for the more seasoned folk here.

link publish delete flag offensive edit

answered 2012-06-12 12:57:40 +0800

boskonovic gravatar image boskonovic
46 1

@rdgrimes for now i used the solution given in the example of Senthilchettyin. But your methods look very interesting. i'll give them a try for sure.
thank you

link publish delete flag offensive edit

answered 2012-07-19 02:34:23 +0800

ntsetso gravatar image ntsetso
59 1 4


I'm also a newbie and with a similar issue.
I have a Form1 in page1.zul bound to VM1 and a Form2 in page2.zul bound to VM2. Page2.zul will open after a user inputs some text in Form1 and clicks a submit button.
The user inputted data is passed to VM2 to fetch some records from a database and then the result is displayed in page2.zul's Form2 when it opens. Database is editable from page2.zul.

I understand how to do this if both forms are opened in the same page, which means that VM1 and VM2 are initiated the same time. However in my case I'd like Form2, i.e., page2.zul to be displayed only after user confirmation.

I had already developed a similar approach to Senthilchettyin, using a POJO to temporarily store user inputted data, which I thought could bring up concurrency issues.
I can see that Senthilchettyin's solution is more sophisticated and more elegant than mine.

Would like to try rdgrimes's solution which unfortunately I didn't understand.

@boskonovic Did you ever try? If some one did please post your example.

@rdgrimes Will you please elaborate you solution with a small example for a newbie?


link publish delete flag offensive edit
Your reply
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




Asked: 2012-06-11 08:40:20 +0800

Seen: 709 times

Last updated: Jul 19 '12

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