1

Default index of Listbox with Template tag

asked 2013-01-10 13:58:55 +0800

erlangga gravatar image erlangga
64 1

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

delete flag offensive retag edit

Comments

1

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 )edit

Ok So when page rendered the item which you default selected show as selected?

sjoshi ( 2013-01-15 11:01:46 +0800 )edit

Yes, the page shows the listitem with the selected attribute that evaluates to true.

erlangga ( 2013-01-16 05:13:38 +0800 )edit

can anyone help enlighten me on this problem?

erlangga ( 2013-01-25 10:19:16 +0800 )edit
1

Can 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

1 Answer

Sort by ยป oldest newest most voted
0

answered 2013-01-15 07:37:13 +0800

erlangga gravatar image erlangga
64 1

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}

try changing the '1' to '4' and click on the button, it will print the index is 2 where it should be 3.
is there any post render processing that i miss?
should i fire the onSelectionChanged event of the listbox?

link publish delete flag offensive edit
Your answer
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: 2013-01-10 13:58:55 +0800

Seen: 168 times

Last updated: Jan 22 '14

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