how to display description but store internal key

asked 2010-02-26 06:26:52 +0800

dickdastardly gravatar image dickdastardly
168 1 5

Dear ZK forum

Have a list of reference data objects that have an internal Integer key and a external display description.
I need to display the description within a listbox using foreach and each,

when one is selected i want to get the internal integer key save as selected value not the string description.
How do you do that ?
am I being an idiot?

Also (can I ask two questions in one thread? :-))

I am using spring MVC and would rather use spring conversion service than zk typeconverters, is that wise?
is that possible?

thanks in advance.

delete flag offensive retag edit

12 Replies

Sort by ยป oldest newest

answered 2010-02-26 19:41:38 +0800

dellsoft gravatar image dellsoft
114 2

updated 2010-02-26 19:42:16 +0800

use custom-attributes
following code

<textbox id="txtData" value="company" onFocus='self.setValue((String)self.getAttribute("key"))'
<custom-attributes key="01" value="company" />

link publish delete flag offensive edit

answered 2010-02-26 21:26:50 +0800

caclark gravatar image caclark
1753 2 5

Well, since you're using Spring, I suggest a fully MVC approach. Let me stress something *very strongly* first:
1) DON'T MIX forEach & databinding. They're evaluated at different times and don't play nicely together. The forEach construct is a one-time rendering phase technique.
2) Use MVC!!!! Yeah, some people prefer other ways, but you're obviously familiar with the benefits of separation of concerns. Use them.

Here's a very simple list of departments. The databinder instantiated via the ...AnnotateDataBinderInit... gets the domain model from the controller via the model="@{departmentController.departments}". In my controller's doAfterCompose(Component comp), I store a reference to the controller in the variable departmentController. I've since changed my technique and now generate my controllers and .zul files with win$controller. The GFC super class stores the same reference in win$composer. I don't care for that name so I use my own. Use whatever floats your boat.

You can see for the department manager name an example of the databinder evaluating dotted property notation to get a Hibernate lazy loaded manager's name.
The domain object is associated with its list item via value="@{departmentItem}" on the listitem definition. That object is retrieved in the double click/Edit handler from the event that's sent and the currently selected list item object is set in the controller via selectedItem="@{departmentController.selectedDepartment}" on the listbox definition.

Note: there's something in my code that seems to mess up the code /code handler, so this code sample and the paragraph after it are goofy

code: null
<?xml version="1.0" encoding="windows-1251"?> <?variable-resolver class="org.zkoss.zkplus.spring.DelegatingVariableResolver"?> <?init class="org.zkoss.zkplus.databind.AnnotateDataBinderInit" root="showDepartmentsWindow" ?> <window id="showDepartmentsWindow" border="normal" apply="${departmentController}" width="500px" height="300px" closable="true" sizable="true" left="20%" top="90px" xmlns:a="http://www.zkoss.org/2005/zk/annotation"> <caption><label style="display: block; text-align: center; font-weight: bold; font-size: 16px;">Departments</label></caption> <borderlayout id="outerLayout" width="100%" height="100%"> <center flex="true" border="normal"> <listbox id="departmentList" model="@{departmentController.departments}" selectedItem="@{departmentController.selectedDepartment}" fixedLayout="true" width="100%" height="100%" mold="paging" rows="0" vflex="true" > <listhead> <listheader width="30px" label="Id"/> <listheader width="50px" label="Name"/> <listheader width="50px" label="Manager"/> <listheader width="50px" label="Status"/> </listhead> <listitem id="departmentListItem" self="@{each='departmentItem'}" value="@{departmentItem}" forward="onDoubleClick=onEditDepartment" > <listcell label="@{departmentItem.departmentId}"/> <listcell label="@{departmentItem.name}"/> <listcell label="@{departmentItem.deptManager.name}"/> <listcell label="@{departmentItem.status}"/> </listitem> </listbox > </center> <south size="30px" flex="true" border="normal"> <div align="center" > <hbox align="center" > <button id="addButton" label="Add New Department" forward="onClick=onAddDepartment()" style="vertical-align: middle;"/> <separator width="10px" /> <button id="editButton" label="Edit Department" forward="onClick=onEditDepartment()"/> <separator width="10px" /> <button id="deleteButton" label="Delete Department" disabled="true" forward="onClick=onConfirmDelete()"/> </hbox> </div> </south> </borderlayout> </window>
</p><p><br/>I have the controller source handy, too, if you want it.  It's about 500 lines long.  Also have the detail window that's popped up on double click or Edit button click and its controller.  It's a typical CRUD list + detail window launched from my main app.  Post an email address somehow (don't think the forum has an email system in it) if you want it and I'll send it to you.</p><p><div style="class=code" class="code"><pre><div class="error">code: null</div>
I am using spring MVC and would rather use spring conversion service than zk typeconverters, is that wise?
is that possible?

I've used Spring MVC a lot. I don't think it's necessary to use the conversion service or possible. Once you shift your thinking from the Spring MVC submit, populate a bean with attributes, process, render a new view to the AJAX oriented ZK framework you'll understand why. You still have a model object backing it, but with databinding it's done quite differently. When you enter an event handler, it's already there for you...


link publish delete flag offensive edit

answered 2010-02-27 00:50:06 +0800

dickdastardly gravatar image dickdastardly
168 1 5

Thanks for bothering to look at this and spend your time replying

Thanks for the heads up regarding the use of foreach and databinding

I would be interested in seeing your controller code, if you don't mind sharing it.

I believe the zk approach will be more efficient than what i was planning on doing with the spring conversion service

I was attempting to get maximum functionality from zk without writing any zk related java code, however I now believe
I'll have too.


link publish delete flag offensive edit

answered 2010-02-27 02:51:47 +0800

terrytornado gravatar image terrytornado flag of Germany
9393 3 7 16

Hi Cary,

can you send me the zul+controller for checking my first module migration to databinding.



link publish delete flag offensive edit

answered 2010-02-28 16:08:14 +0800

dickdastardly gravatar image dickdastardly
168 1 5

why is setting up a combobox so difficult?

What am i missing?

Is'nt there a clear working tutorial somewhere on the web that shows how to set one up correctly?

I just need a drop down list of read only reference data such as Gender or Title and let the user pick one.
I'd like to use an java.util.List<Gender> genderList = new ArrayList<Gender>(); as my model="@{}"
and have the selected value stored in a Gender gender; instance variable, surely that cant be too hard! :-(

link publish delete flag offensive edit

answered 2010-02-28 19:18:41 +0800

henrichen gravatar image henrichen
3869 2
ZK Team

updated 2010-02-28 19:21:18 +0800


Assume you have a Spring managed bean "userService" which will hold the reference to current user and return the Gender list.


public class UserService {
  private User user;

  //getter and setter for user

  //return the Gender list
  public List<Gender> getGenderList() {

public class User  {
  private Gender gender;


  //getter and setter for gender


public class Gender {
  private int key;
  private String description;

  //getter and setter for key and description

<?variable-resolver class="org.zkoss.zkplus.spring.DelegatingVariableResolver"?>
<?init class="org.zkoss.zkplus.databind.AnnotateDataBinderInit"?>
<window width="500px" height="300px">
  <listbox model="@{userService.genderList}" selectedItem="@{userService.user.gender}" mold="select">
    <listitem self="@{each='gen'}" label="@{gen.description}"></listitem>

When end user made the selection, the Gender object will be store into user's gender variable by data binder.

link publish delete flag offensive edit

answered 2010-02-28 23:10:41 +0800

dickdastardly gravatar image dickdastardly
168 1 5

Thanks for the solution...

however I was trying to use a combobox/comboitem as thats the look and feel i was after

is this possible to achieve...

link publish delete flag offensive edit

answered 2010-03-01 13:17:36 +0800

dickdastardly gravatar image dickdastardly
168 1 5

Dear henrichen

Just managed to try out your solution and it works


Thanks very much

link publish delete flag offensive edit

answered 2010-03-01 16:37:18 +0800

caclark gravatar image caclark
1753 2 5

updated 2010-03-01 16:38:07 +0800

Stephan, dickdastardly, and anyone else...if you want the Eclipse project that I whipped up as a small demo, get it here: http://clarktrips.intltwins.org/caclark/zkmvcdemo.zip
It's not as comprehensive as Stephan's that he's published, but rather a quick, simple, MVC demo. It uses Spring, Hibernate, and the MVC paradigm.

And if you're bored, take a look at the top level page: http://clarktrips.intltwins.org and try to guess why one I am...

link publish delete flag offensive edit

answered 2010-03-02 02:48:07 +0800

tomCat gravatar image tomCat
276 3

Hi Cary,

thank you very much for sharing your project. if have a question about your showNotices.zul:

<button id="deleteButton" label="Delete Notice" disabled="@{win$controller.selectedNotice.noticeId eq null}" forward="onClick=onConfirmDelete()"/>

Does the "win$controller.selectedNotice.noticeId eq null" binding work in your application, or is it only for testing purposes?

Best regards,


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




Asked: 2010-02-26 06:26:52 +0800

Seen: 641 times

Last updated: Mar 04 '10

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