ZK - Open Source Ajax Java FrameworkZK - Open Source Ajax Java Framework

Listbox and setSelectedIndex

satan87
6 Feb 2012 19:38:22 GMT
6 Feb 2012 19:38:22 GMTHello I got a listobox, wich trigger an event to update 2 others listboxes. I'am trying to create a link on another page in order to call this page with a predefine value selected on my litbox my calling page
public void onMyLineButtonClicked(Event event)
    {
    	if (event instanceof ForwardEvent)
    	{
    		event = Events.getRealOrigin((ForwardEvent)event);
    		Button b  = (Button)event.getTarget(); // retrieve the button
    		Listcell lc = (Listcell) b.getParent();  // retrieve the cell of the button
    		Listitem lit = (Listitem) lc.getParent(); // retrieve the line    		
    		String pId = ((Listcell)lit.getChildren().get(0)).getLabel() ; // take the first cell of the line, and retrieve the value of the label
    		Executions.getCurrent().getSession().setAttribute("productid", pId);
    		Executions.sendRedirect("/linkAll.zul");
    	}
    }
My called page :
<window title="US AS" onCreate="init()">
private void init()
{
	lsbproduct.setModel(new ListModelList( new dao.USASDB().getProducts() ) );
	lsbproduct.setItemRenderer( new linkall.rendererProduct() );
	
	String pid = Executions.getCurrent().getSession().getAttribute("productid").toString();
	
	if ( pid != null && !pid.equals(""))
	{
		lsbproduct.setSelectedIndex( Integer.parseInt(pid) - 1 );
		update();
	}


}
rivate void update()
{
//LINK
	
	Object o = lsbproduct.getSelectedItem();	

	try
	{
		Listitem li = (Listitem)o;
		String s = li.getLabel().toString();
		Messagebox.show( s );
		
	}catch(Exception e){e.printStackTrace();}
}
If i load the called page, and click on an item it's working fine. but in this case, i got my ID which is null and my label which is empty My rendrer :
@Override
	public void render(Listitem lst, Object data) throws Exception 
	{
		Product p = (Product) data;	
		lst.setValue( p.getId() );
		lst.setLabel( p.getName() );
	}
terrytornadoTop Contributor
7 Feb 2012 10:15:10 GMT
7 Feb 2012 10:15:10 GMT

The rendering is the same in ALL your listboxes.

@Override
	public void render(Listitem lst, Object data) throws Exception 
	{
		Product p = (Product) data;	

               /* UI rendering stuff */
		Listcell lc;
                lc = new Listcell( p.getId() );
		lc.setParent(item);
		lc = new Listcell( p.getName() );
		lc.setParent(item);

                /* Store the object in the listitem */
		item.setValue( data );

                /* if needed forward an event */
		ComponentsCtrl.applyForward(item, "onDoubleClick=onDoubleClicked");

	}

.

private void update() {
	
        /* Get the selected listitem */
	Listitem li  = lsbproduct.getSelectedItem();	

        /* Get the object by casting to the right type */
        Product  product = (Produce) li.getValue();

	if (product != null) {
    	  try 	{
		String s = product.getName();
		Messagebox.show( s );
	} catch(Exception e){
               e.printStackTrace();
        }
}


best
Stephan

satan87
7 Feb 2012 18:54:27 GMT
7 Feb 2012 18:54:27 GMT

Hi

thanks for the answer but it's still not working

I update this : lc = new Listcell( ""+p.getId() );

In update, product is null !, and li (Listitem li = lsbproduct.getSelectedItem();) got only 1 child

I though it was because Product is an Hibernate class, but i got the same issue using a non hibernate class

Any idea why ?

Thanks
Nicolas

satan87
7 Feb 2012 20:24:33 GMT
7 Feb 2012 20:24:33 GMT

i think my issue comes from the onCreate

i tried this

private void init()
{
	lsbproduct.setModel(new ListModelList( new dao.USASDB().getProducts() ) );
	lsbproduct.setItemRenderer( new linkall.rendererProduct() );
	/*
	if (Executions.getCurrent().getSession().getAttribute("productid")!=null)
	{
		String pid = Executions.getCurrent().getSession().getAttribute("productid").toString();
	
		if ( pid != null && !pid.equals(""))
		{
			//lsbproduct.setSelectedIndex( Integer.parseInt(pid) - 1 );
			lsbproduct.setSelectedItem(  lsbproduct.getItemAtIndex( Integer.parseInt(pid) - 1 )  );
			update();
		}
	}
	*/
	update();
	
}
import dao.*;
private void update()
{
	
	//Object o = lsbproduct.getSelectedItem();
	//String pid = ((Listitem) o).getValue().toString();
	
	try
	{
		Messagebox.show( "" + lsbproduct.getItemAtIndex(0).getLabel() );
	}catch(Exception e)
	{
		e.printStackTrace();
	}
	
}

and when i open the page, the messgebox is empty !!
after when i click on the listbox, the message is correctly fill

Any idea ?

Thanks
Nicolas

ashishd
8 Feb 2012 08:23:53 GMT
8 Feb 2012 08:23:53 GMT

Hi Satan87,
Can you be more clear in describing your use case? I am trying to understand why you need onCreate event to setModel and then immediately call update() function to get the selected item index? That just looks wrong to me. Maybe you can use zkfiddle.org to post your sample code so we can see it in action and modify your example online to provide you suggestion.

satan87
8 Feb 2012 19:36:56 GMT
8 Feb 2012 19:36:56 GMT

Hi

i actually find a way to resolve my issue, but here what i want to do and the result

What :
i got a page with 3 listbox
1 for a product
2 for a type

when you click on a product, both others listbox are populated with types (from the db)

You can access the page
- directly, in this case, no product are selected
- from a link, in that case one product should be preselected, and both types listbox populated

in order to do that, i use an attribute in the session.
In the "onCreate" event of my page, i look if i have a session attribute
if i don't i just load the product listbox
if i have, i load the product listbox, select the product i want, and launch the load of the 2 other listbox

but i notice that, inside the onCreate, the listbox are created and populated asynchronously.

in order to force the listbox to be render and populated before the rest of the page i did that :

<window onCreate="init()">
<listbox onCreate="inilb()" />
</windw>

<zscript>

initlb
{
lstb.setmodel
lstb.setrendere
lstb.rendererALL():
}


init()
{
if i have my session attribute, i launch the update
}

update
{
i update my 2 type listboxes
}

</zscript>

If it can help someone.
nicolas