0

How to bind 2 listboxes to the same model class?

asked 2009-10-12 13:05:01 +0800

TomGlados gravatar image TomGlados
45 1

updated 2009-10-12 13:10:01 +0800

As a real-life example, I have a window with 2 listboxes:
- a Users listbox (model="@{userModel.users}", selectedItem="@{userModel.selectedItem}" to list all users (columns: full name, department)
- and a Departments listbox (mold="select", model="@{departmentModel}, selectedItem="@{userModel.selectedItem.department}) to set the selected user's department.
So far no problems.

Now, I would like to add a filter to the window in order to filter the Users list by Department. So I add another listbox (mold="select", model="@{departmentModel}, selectedItem="@{departmentModel.selectedItemAsFilter}, OnClick="filterUsers()").

When I run my application and select a user from the Users list I receive a "Cannot find associated CollectionItem..." error.

To demonstrate the problem I added a listbox to the Data binding example of ZK Explorer:

<?init class="org.zkoss.zkplus.databind.AnnotateDataBinderInit" arg0="./mywin"?>
<window id="mywin" border="none">
ZK name check list :
	<zscript>
	//init example data beans
	import org.zkoss.zkdemo.userguide.Person;
	List persons = new ArrayList();
	persons.add(new Person("Balu", "Haben"));
	persons.add(new Person("Sadira", "Jobs"));
	persons.add(new Person("Dacey", "Obert"));
	persons.add(new Person("Gabby", "Taffy"));
	Person selected = persons.get(0);
 	</zscript>
	
        <!--                                                                                      -->
        <!--  Additional listbox to demonstrate the databinding problem -->
        <!--                                                                                      -->
        <listbox id="filter" mold="select" model="@{persons}" >
                <listitem self="@{each=person}" label="@{person.lastName}" />
        </listbox>


	<listbox model="@{persons}" selectedItem="@{selected}" rows="5">
		<listhead >
				<listheader label="Name"/>		
		</listhead>
		<listitem self="@{each=person}" label="@{person.fullName}" />
	</listbox>
	<grid>
		<columns>
			<column label="First Name" />
			<column label="Last Name" />
			<column label="Full Name" />
		</columns>
		<rows>
			<row>
				<textbox id="firstName" value="@{selected.firstName}" />
				<textbox value="@{selected.lastName}" />
				<label value="@{selected.fullName}" />
			</row>
		</rows>
	</grid>
</window>

The above code has no practical value at all but you can simply copy-paste it to ZK Explorer to see the problem described. Select a person from the main list to see the error message.


Is this a limitation of databinding or I just need to do something in a different way?

I see that creating a separate DepartmentFilter class then binding it to the departmentFilter listbox solves the problem, but I would like to avoid generating redundant filter classes.

Regards:
Tom

delete flag offensive retag edit

2 Replies

Sort by ยป oldest newest

answered 2009-10-12 13:51:27 +0800

TomGlados gravatar image TomGlados
45 1

updated 2009-10-12 14:23:52 +0800

This problem has been discussed here:
http://www.zkoss.org/forum/index.zul#path%3DlistComment%3BdiscussionId%3D7596%3BcategoryId%3D14%3B

and there is also a feature request:
http://sourceforge.net/tracker/index.php?func=detail&aid=2114050&group_id=152762&atid=785191

According to the above thread I changed the zul file to use unique binder variable names. I simply use <listitem self="@{each=filterDepartment}"> and <listcell label="@{filterDepartment.name}"> in the department filter listbox and it seems to work.

I know I should search the forum before I ask:)

Anyway thanks for everyone.
Tom

link publish delete flag offensive edit

answered 2016-09-06 15:18:18 +0800

Dgray gravatar image Dgray
3 1

In my case I had kind of problem:

index.zul:

    <?component name="myComponent" inline="true" macroURI="_myComponent.zul"?>

    <customGrid>
        ...
        <row self="@{each=object}" value="@{object}">
            <customDetail>
                 <myComponent/>
            </customDetail>
        </row>
              ...
    </customGrid>

_myComponent.zul

<customGrid model="@{object.getList}">
    <listbox model="@{object.getDataForListBox}">
        ...
    </listbox>

</customGrid>

And I had exception "Cannot find associated CollectionItem" with expession:object.getDataForListBox

How I solved this problem:

_myComponent.zul

  <customGrid model="@{object.getList}">
        <listbox forEach="@{object.getDataForListBox}">
              ...
        </listbox>
  </customGrid>
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: 2009-10-12 13:05:01 +0800

Seen: 359 times

Last updated: Sep 06 '16

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