0

setSelectedItem on Listbox doesn't refresh databinding

asked 2010-06-15 06:30:44 +0800

kleptomac gravatar image kleptomac
75 2

Hi All,

I have a listbox which is bound to a model using annotations. And there is a small form which is linked to the "selectedItem" of the Listbox so that it shows the selected values:

================Code==========================
<window>
<a:bind model="books" selectedItem="selected"/>
<listbox rows="5" id="lstSample" model="@{win$composer.books}">
<listhead>
<listheader label="Title" width="100px"/>
<listheader label="Author" width="100px"/>
<listheader label="Details" width="100px"/>
</listhead>
<a:bind _var="book"/>
<listitem value="@{book}">
<a:bind label="book.title"/>
<listcell id="title"/>

<listcell>
<a:bind value="book.author"/>
<textbox id="author" inplace="true"/>
</listcell>

<a:bind label="book.details"/>
<listcell id="details"/>

</listitem>
</listbox>

<separator height="30px"/>

<!-- show the details of the selected item -->
<label value="show the details of the selected item (with databinding)"/>
<grid>
<rows>
<row>Title:
<a:bind value="selected.title"/>
<textbox/>
</row>
<row>Author:
<a:bind value="selected.author"/>
<textbox/>
</row>
</rows>
</grid>

<hbox>
<button id="getSelectedItem" label="Get Selected Item"/>
</hbox>
</window>

============ composer code ===================


Now, this works well except when I try to change the selected item programmatically. If I do a "setSelectedItem" on the Listbox from the composer, the newly selected item values are not reflected in the form below. The databinding doesn't work here. I tried doing a "binder.loadAll()" as well but it didn't work.

can someone help me on how to get the databinding to work here

Thanks,
Jai

delete flag offensive retag edit

9 Replies

Sort by ยป oldest newest

answered 2010-06-17 06:29:46 +0800

samchuang gravatar image samchuang
4084 4

updated 2010-06-17 06:30:24 +0800

Hi

I can't run your code, what is <a:bind ?
Do you have any other setting that didn't include here ?

link publish delete flag offensive edit

answered 2010-06-17 10:23:01 +0800

kleptomac gravatar image kleptomac
75 2

updated 2010-06-17 10:23:44 +0800

Im sorry. <a:bind> are the binding representations. here is the complete code

<?xml version="1.0" encoding="UTF-8"?>
<?init class="org.zkoss.zkplus.databind.AnnotateDataBinderInit" ?>
<?variable-resolver class="org.zkoss.zkplus.spring.DelegatingVariableResolver"?>
<?page zscriptLanguage="GroovyGrails"?>

<zk xmlns="http://www.zkoss.org/2005/zul"
xmlns:h="http://www.w3.org/1999/xhtml"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:a="http://www.zkoss.org/2005/zk/annotation"
xsi:schemaLocation="http://www.zkoss.org/2005/zul http://www.zkoss.org/2005/zul/zul.xsd">

<window>
<a:bind model="books" selectedItem="selected"/>
<listbox rows="5" id="lstSample" model="@{win$composer.books}">
<listhead>
<listheader label="Title" width="100px"/>
<listheader label="Author" width="100px"/>
<listheader label="Details" width="100px"/>
</listhead>
<a:bind _var="book"/>
<listitem value="@{book}">
<a:bind label="book.title"/>
<listcell id="title"/>

<listcell>
<a:bind value="book.author"/>
<textbox id="author" inplace="true"/>
</listcell>

<a:bind label="book.details"/>
<listcell id="details"/>

</listitem>
</listbox>

<separator height="30px"/>

<!-- show the details of the selected item -->
<label value="show the details of the selected item (with databinding)"/>
<grid>
<rows>
<row>Title:
<a:bind value="selected.title"/>
<textbox/>
</row>
<row>Author:
<a:bind value="selected.author"/>
<textbox/>
</row>
</rows>
</grid>

<hbox>
<button id="getSelectedItem" label="Get Selected Item"/>
</hbox>
</window>

</zk>

link publish delete flag offensive edit

answered 2010-06-18 02:06:29 +0800

samchuang gravatar image samchuang
4084 4

updated 2010-06-18 03:15:15 +0800

Hi

I modify your code as this, seems work fine, the lstbox do display the selected item

<?xml version="1.0" encoding="UTF-8"?>
<?init class="org.zkoss.zkplus.databind.AnnotateDataBinderInit" ?>
<?variable-resolver class="org.zkoss.zkplus.spring.DelegatingVariableResolver"?>
<?page zscriptLanguage="GroovyGrails"?>

<zk xmlns="http://www.zkoss.org/2005/zul"
	xmlns:h="http://www.w3.org/1999/xhtml"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:a="http://www.zkoss.org/2005/zk/annotation"
	xsi:schemaLocation="http://www.zkoss.org/2005/zul http://www.zkoss.org/2005/zul/zul.xsd">
<zscript language="Java"><![CDATA[
class Book {
	String title;
	String author;
	String details;
	
	public Book (String title, String author, String details) {
		this.title = title;
		this.author = author;
		this.details = details;
	}
	
	public void setTitle(String title) {
		this.title = title;
	}
	public String getTitle() {
		return title;
	} 
	
	public void setAuthor(String author) {
		this.author = author;
	}
	public String getAuthor() {
		return author;
	}
	public void setDetails(String details) {
		this.details = details;
	}
	public String getDetails() {
		return this.details;
	}
}

List books = new ArrayList();
books.add(new Book("a", "a_auth", "a_detail"));
books.add(new Book("b", "b_auth", "b_detail"));
books.add(new Book("c", "c_auth", "c_detail"));
]]></zscript>
	<window>
		<a:bind model="books" selectedItem="selected" />
		<listbox rows="5" id="lstSample"
			model="@{books}">
			<listhead>
				<listheader label="Title" width="100px" />
				<listheader label="Author" width="100px" />
				<listheader label="Details" width="100px" />
			</listhead>
			<a:bind _var="book" />
			<listitem value="@{book}">
				<a:bind label="book.title" />
				<listcell id="title" />

				<listcell>
					<a:bind value="book.author" />
					<textbox id="author" inplace="true" />
				</listcell>

				<a:bind label="book.details" />
				<listcell id="details" />

			</listitem>
		</listbox>

		<separator height="30px" />

		<!-- show the details of the selected item -->
		<label
			value="show the details of the selected item (with databinding)" />
		<grid>
			<rows>
				<row>
					Title:
					<a:bind value="selected.title" />
					<textbox />
				</row>
				<row>
					Author:
					<a:bind value="selected.author" />
					<textbox />
				</row>
			</rows>
		</grid>

		<hbox>
			<button id="getSelectedItem" label="Get Selected Item" />
		</hbox>
	</window>

</zk>

link publish delete flag offensive edit

answered 2010-06-18 06:54:43 +0800

kleptomac gravatar image kleptomac
75 2

It doesn't when when you trigger the "setSelectedItem()" programmatically. I figured it out though.

You need to dispatch a SelectEvent after doing a "setSelectedItem()" for the databinder to work.

Thanks,
Jai

link publish delete flag offensive edit

answered 2010-06-18 06:55:16 +0800

kleptomac gravatar image kleptomac
75 2

Refer to my other post here:

http://www.zkoss.org/forum/listComment/12711

link publish delete flag offensive edit

answered 2010-06-20 20:25:05 +0800

samchuang gravatar image samchuang
4084 4

Thanks for the your sharing

link publish delete flag offensive edit

answered 2010-06-22 01:12:48 +0800

jaikarthik gravatar image jaikarthik
69

hi kleptomac

i dont think triggering event is required.
binder.saveComponent(componentname) can do the work

Thanks
Jaikarthik Natarajan

link publish delete flag offensive edit

answered 2010-07-01 14:29:49 +0800

kleptomac gravatar image kleptomac
75 2

But what if we are not using binding or this is inside a custom component and we don't have access to the binder?

Thanks,
Jai

link publish delete flag offensive edit

answered 2012-05-15 15:14:08 +0800

intoxicadocoder gravatar image intoxicadocoder
57 1 1 4

I solve this problem using bandbox
<bandbox value="@load(pl.catSel)" mold="rounded">
<bandpopup>
<listbox id="lsbxCat" model="@load(pl.catModel)" selectedItem="@bind(pl.catSel)"/>
</bandpopup>
</bandbox>

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: 2010-06-15 06:30:44 +0800

Seen: 2,108 times

Last updated: May 15 '12

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