-
FEATURED COMPONENTS
First time here? Check out the FAQ!
Hello everyone,
I need your help in clarifying something. It's about the default index of a Listbox when using Template.
I have these test code
Test.java
package foo; import java.io.Serializable; public class Test implements Serializable { /** * */ private static final long serialVersionUID = -3121205098864452889L; private int id; private String text; public Test(int id, String text){ this.id = id; this.text = text; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getText() { return text; } public void setText(String text) { this.text = text; } }
and
index.zul
<?page title="Auto Generated index.zul"?> <window title="Hello World!!" border="normal" width="200px"> <listbox id="listbox" mold="select"> <template name="model"> <listitem label="${each.text}" selected="${each.id == 1}"/> </template> </listbox> <button label="click me" onClick="System.out.println(listbox.getSelectedIndex())"/> <zscript> ListModelList model = new ListModelList(); listbox.setModel(model); model.add(new foo.Test(1,"test1")); model.add(new foo.Test(2,"test2")); model.add(new foo.Test(3,"test3")); model.add(new foo.Test(4,"test4")); model.add(new foo.Test(5,"test5")); </zscript> </window>
Now, here is the thing,
if i click the button without changing the selected item of the listbox it will always print -1 to the console, it will print the initial selected index minus one exactly.
but if i change the selected item in the listbox and click the button, then it will print the correct selected index of the listbox.
Is this the correct behaviour when using template?
Thank you,
Erlangga
Hi sjoshi, thank you for your reply
yes, i know if i don't select any listitem the selectedIndex will return -1. but in my script i have explicitly set the selected attribute of listitem based on the condition in the script
selected="${each.id == 1}
Asked: 2013-01-10 13:58:55 +0800
Seen: 168 times
Last updated: Jan 22 '14
If you will not select any item and trying to get the index of selecteditem you will get -1 when you will select any item then you will get correct index.
sjoshi ( 2013-01-15 05:00:03 +0800 )editOk So when page rendered the item which you default selected show as selected?
sjoshi ( 2013-01-15 11:01:46 +0800 )editYes, the page shows the listitem with the selected attribute that evaluates to true.
erlangga ( 2013-01-16 05:13:38 +0800 )editcan anyone help enlighten me on this problem?
erlangga ( 2013-01-25 10:19:16 +0800 )editCan you please elaborate What you want to achieve so it may be easy for me to answer your question
sjoshi ( 2013-01-25 16:36:41 +0800 )edit