0

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.

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

		slides = new ListModelList<SlideBean>(data.getSlides());
		Executions.sendRedirect("/wam/ais/slides/checkout/result.zul");

	}



this is the result page:

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

		<vbox>
			<style>
				.hb{ float:right; }

			</style>
			<hbox sclass="hb">

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

			<listbox  model="@load(vm.slides)" >
				<listhead>
					<listheader>
						<checkbox />
					</listheader>
					<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" />
				</listhead>
				<template name="model" var="slide">
					<listitem>
						<listcell>
							<checkbox />
						</listcell>
						<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)" />
					</listitem>
				</template>

			</listbox>



		</vbox>

	</window>
</zk>


delete flag offensive retag edit

12 Replies

Sort by ยป oldest newest

answered 2012-07-19 13:04:25 +0800

boskonovic gravatar image boskonovic
46 1

@ntsetso
As i said i managed to solve my problem following Senthilchettyin. I've posted the solution here. I hope it helps.

link publish delete flag offensive edit

answered 2012-07-19 15:31:23 +0800

rdgrimes gravatar image rdgrimes
735 7

Hi helenkern2K12,

As along as both viewmodels belong to the same session, it will be availabe to zul 2's viewmodel. So, you must not be doing it right. The key is in your viewmodel proxy constructor:

ViewModel Proxy

package com.soc.ds.zk.viewmodel;

public class AlertsViewModelProxy
{
	private AlertsViewModel vm;

	public AlertsViewModelProxy()
	{
		super();

		Session session = Sessions.getCurrent();

		// Create one viewModel per session
		if (session.hasAttribute("alertsvm"))
		{
			vm = (AlertsViewModel) session.getAttribute("alertsvm");
		} else
		{
			vm = new AlertsViewModel();
			session.setAttribute("alertsvm", vm);
		}
	}
	
	
	public List<CustomerAlert> getCustomerAlertList()
	{
		return vm.getCustomerAlertList();
	}
	public void setCustomerAlertList(List<CustomerAlert> customerAlertList)
	{
		vm.setCustomerAlertList(customerAlertList);
	}

	@Command @NotifyChange("customerAlertList")
	public List<CustomerAlert> showCustomerAlerts()
	{
		return vm.showCustomerAlerts();
	}
}

ViewModel

package com.soc.ds.zk.viewmodel;

public class AlertsViewModel
{
	private List<CustomerAlert>	customerAlertList;

	public AlertsViewModel()
	{
		super();
	}

	public List<CustomerAlert> getCustomerAlertList()
	{
		return customerAlertList;
	}

	public void setCustomerAlertList(List<CustomerAlert> customerAlertList)
	{
		this.customerAlertList = customerAlertList;
	}
	
	public List<CustomerAlert> showCustomerAlerts()
	{
		List<CustomerAlert> localCustomerAlertList = null;
		
		// Logic to retrieve customer alerts from DAO 
		. . . 

		setCustomerAlertList(localCustomerAlertList);
		
		return customerAlertList;
	}
}

Then, if you have two zul views (in this case, nearly identical), they would both access it as such:

<zk>
<timer id="alertsRefreshTimer" delay="300000" repeats="true" onTimer="@command('showCustomerAlerts')" height="0px" width="0px" />	
<vlayout apply="org.zkoss.bind.BindComposer"
               viewModel="@id('vm') @init('com.soc.ds.zk.viewmodel.AlertsViewModelProxy')"
               spacing="0" height="100%" hflex="true" vflex="true">

               <div height="100%" vflex="true" >
                  <listbox autopaging="true" 
                               height="100%" hflex="true"
                               model="@load(vm.customerAlertList)" 
                               mold="paging" onFulfill="self.selectedIndex = -1"
                               selectedItem="@bind(vm.selectedItem)" 
                               sclass="ListLayoutNoBorder"
                               vflex="true" width="100%">
                   <listhead>
                   <listheader label="Date" align="left" width="80px" />
                   <listheader label="Message" align="left" />
                   <listheader label="Link" align="center" width="70px" />
                </listhead>
                <template name="model" var="item">
                   <listitem height="75px">
                      <listcell>
                        <hlayout width="100%">
                           <label value="@load(item.enteredDate) @converter('formatedDate', format='MM-dd-yyyy')" />
                        </hlayout>			
                     </listcell>
                     <listcell>
                  <hlayout width="100%">
                     <label value="@load(item.message)" />
                  </hlayout> 
               </listcell>
               <listcell>
                  <hlayout sclass="center" width="100%">
                     <image src="@load(empty item.link ? 'assets/images/blankpage.png' : 'assets/images/webpage.png')"  onClick="@command('navigateToUrl')" />
                  </hlayout>
               </listcell>
            </listitem>
         </template>
      </listbox>
   </div>
</vlayout>	
</zk>

In my case, I use it so that the min and max views, within my business portal, access the same viewmodel. This way, when I switch between min and max view, the data displayed is in sync and it looks like you're just maximizing or restoring the same window, even though it's actually two different zul files. Also, please take note that viewmodel annotations (@Command, @NotfityChange, @Init, etc) must all be defined in the proxy and NOT in the actual viewmodel itself.

The key though is in the viewmodel proxy and instantiating the actual viewmodel only once per session, and binding the same viewmodel to each view that needs it.

This, of course, opens up all sorts of possibilities. You can share viewmodels, not just by session, by by other scopes or groupings, as desired.

Hope that helps.

Ron

link publish delete flag offensive edit

answered 2012-07-19 15:33:19 +0800

ntsetso gravatar image ntsetso
59 1 4

Tks for sharing. Senthilchettyin's sulution also helped me to improve mine.

rgrds.

link publish delete flag offensive edit

answered 2012-07-19 15:42:28 +0800

rdgrimes gravatar image rdgrimes
735 7

updated 2012-07-19 16:16:14 +0800

Hi helenkern2K12,

The above code example was for option #1.

In like manner, with option #2, instead of having a view model proxy, you bind each zul straight to the viewmodel, and then do something similar as I have done in option #1.

@Command @NotifyChange("customerAlertList")
public List<CustomerAlert> showCustomerAlerts()
{
     Session session = Sessions.getCurrent();

     if (session.hasAttribute("mydatalist"))
     {
          customerAlertList  = (List<CustomerAlerts>) session.getAttribute("mydatalist");
     } else
     {
          customerAlertList = dao.getCustomerAlerts();
          session.setAttribute("mydatalist", customerAlertList);
     }
     
     return customerAlertList;
}

In this case, each zul does have its own separate viewmodel, but their 'showCustomerAlerts' method share the collection via the session attribute.

You can, of course, extend this type of logic to the getCustomerAlertList and setCustomerAlertList methods.

Ron

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

Follow

RSS

Stats

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

Seen: 713 times

Last updated: Jul 19 '12

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