-
FEATURED COMPONENTS
First time here? Check out the FAQ!
hey there,
I'm very new to ZK so forgive me if I could seem dumb.
Btw I'm just trying to figure out how get my data back by (selected) listItems into a listBox.
I have this method in my Controller class, in-code comments explains my problem
public void onSelect$listBox() { // with this I can get all selected items from my multicolumn Listbox (rapresenting a portion of my POJO) // but I'm able to retrive just the first column by using the getLabel() method. Set set = listBox.getSelectedItems(); for (java.util.Iterator<Listitem> it = set.iterator(); it.hasNext();) { Listitem li = it.next(); System.out.println(li.getLabel()); } // With this portion of code I can get all MyPojos but I don't know if they are selected or not. // Of course I could use the indexes I've retreived just few lines of code ago, but I don't like this // dirty solution. ListModelList lml = (ListModelList) listBox.getModel(); for (java.util.Iterator it =lml.iterator(); it.hasNext();){ //got mypojos!!! YES! MyPojo mp = (MyPojo )it.next(); } }
Any idea?
Thanks!
Luigi
You can CAST to your pojo
Set set = listBox.getSelectedItems(); for (java.util.Iterator<Listitem> it = set.iterator(); it.hasNext();) { Listitem li = it.next(); //got mypojos!!! YES! MyPojo mp = (MyPojo )it; System.out.println(li.getLabel()); }
OK.
Have a look on the followed sources of an Listbox ItemRenderer.
The pojo class is called 'Branch'.
See how to put the pojo in the ListItem and get them back.
/** * Item renderer for listitems in the listbox. * * @author bbruhns * @author sgerth * */ public class BranchListModelItemRenderer implements ListitemRenderer, Serializable { private static final long serialVersionUID = 1L; @Override public void render(Listitem item, Object data) throws Exception { Branche branche = (Branche) data; Listcell lc = new Listcell(branche.getBraBezeichnung()); lc.setParent(item); item.setAttribute("data", data); // ComponentsCtrl.applyForward(item, "onClick=onClicked"); ComponentsCtrl.applyForward(item, "onDoubleClick=onDoubleClicked"); } }
/** * Call the Branch dialog with the selected entry. <br> * <br> * This methode is forwarded from the listboxes item renderer. <br> * see: de.forsthaus.webui.branch.model.BranchListModelItemRenderer.java <br> * * @param event * @throws Exception */ public void onDoubleClicked(Event event) throws Exception { // get the selected object Listitem item = listBoxBranch.getSelectedItem(); if (item != null) { // CAST TO THE SELECTED OBJECT Branche aBranche = (Branche) item.getAttribute("data"); if (logger.isDebugEnabled()) { logger.debug("--> " + aBranche.getBraBezeichnung()); } showDetailView(aBranche); } }
best
Stephan
I've done so:
index.zul:
<window id="win" apply="MyController"> ... <listbox id="myList" model="@{win$composer.myPojo}" checkmark="true" multiple="true"> <listhead> <listheader label="Code" width="100px" /> <listheader label="Name" /> </listhead> <listitem self="@{each='objects'}" value="@{objects}"> <listcell label="@{objects.code}"/> <listcell label="@{objects.name}"/> </listitem> </listbox> <button id="buttonSelection" label="Selection"></button>
MyController.java:
public List<MyPojo> getMyPojo() { ... getting the list of objects to populate myList... } Listbox myList; public void onClick$buttonSelection() { Set<Listitem> select = myList.getSelectedItems(); for (Listitem li : select){ code = ((MyPojo)li.getValue()).getCode(); name = ((MyPojo)li.getValue()).getName(); anythingElseOnMyPojo = ((MyPojo)li.getValue()).getAnythingElseOnMyPojo(); ... } }
This thread is 15 months old. Are you asking a question or just giving an example of how you did it? I don't see how yours is going to work because of the signature of your onClick$buttonSelection() method. It should be onClick$buttonSelection(Event evt)...
Hi, Cary.
I'm just giving an example.
I've done it this way and is working, including the method signature.
I was looking for a solution to "Listitem cannot be cast..." when I've found this old thread. I've thought if I put my solution here it could help someone...
Asked: 2010-02-10 10:25:19 +0800
Seen: 786 times
Last updated: May 09 '11