0

Databinding a combobox

asked 2009-12-20 15:31:53 +0800

subscriber gravatar image subscriber
57 2 2

my class file is

public class Thing extends NamedEntity{

@Column(name= "PARENT_ID")
private Thing parent_id;

@Column(name= "KEYWORDS")
private String keywords;
public Thing getParent_id() {
return parent_id;
}

public void setParent_id(Thing parentId) {
parent_id = parentId;
}

public String getKeywords() {
return keywords;
}

public void setKeywords(String keywords) {
this.keywords = keywords;
}

}

------------------------------------------------------------------------
my controller file
public class ThingController extends GenericForwardComposer{

AnnotateDataBinder binder;
Thing thing;
List<Thing> thingList;


public Thing getThing() {
return thing;
}
public void setThing(Thing thing) {
this.thing = thing;
}
public List<Thing> getThingList() {
return thingList;
}
public void setThingList(List<Thing> thingList) {
this.thingList = thingList;
}

public void onCreate(Event event)
{
if( binder == null )
binder = (AnnotateDataBinder)event.getPage().getZScriptVariable("binder");
}


public void onClick$save(){
GenericService.getPersistableDao(application).save(thing);
}

}
------------------------------------------------------------------------------
my zul page
<?taglib uri="http://www.zkoss.org/dsp/web/core" prefix="c"?>
<?page title="new page title" contentType="text/html;charset=UTF-8"?>
<?init class="org.zkoss.zkplus.databind.AnnotateDataBinderInit"?>
<zk>
<zscript>
model.Thing thing = new model.Thing();
List thingList = service.GenericService.getPersistableDao(application).list(model.Thing.class)
</zscript>
<window title="${c:l('app.title')}" border="normal" apply="controller.ThingController">
<grid>
<rows>
<row>
<label value="Parent Name"/>
<combobox model="@{thingList}" selectedItem="@{selected}" value="@{selected.name}" id="parent_id">
<comboitem self="@{each=t}" label="@{t.name}" value="@{t.id}"/>
</combobox>
</row>

<row>
<label value="Name"/>
<textbox id="name" value="@{thing.name}" constraint="no empty"></textbox>
</row>

<row>
<button label="${c:l('form.save')}" id="save" width="50px" />
</row>
</rows>
</grid>
</window>
</zk>


my problem is on zul page. everything works ok except parent_id
<combobox model="@{thingList}" selectedItem="@{selected}" value="@{selected.name}" id="parent_id">
<comboitem self="@{each=t}" label="@{t.name}" value="@{t.id}"/>
</combobox>

how can i bind thing.id to class parent_id in combobox. i looked at lot of examples but couldnt find it.
in combobox i am listing thingList(there are Thing.class in it). From combobox selected item id is Thing class's parent_id but cant bind it

delete flag offensive retag edit

13 Replies

Sort by ยป oldest newest

answered 2009-12-21 00:20:15 +0800

henrichen gravatar image henrichen
3869 2
ZK Team

There is no way for GenericForwardComposer to "autowire" the combobox "parent_id" to Thing because a Combobox is not a Thing.

link publish delete flag offensive edit

answered 2009-12-21 03:31:01 +0800

subscriber gravatar image subscriber
57 2 2

omg isnt there any way to bind a listbox or comobox to main model? Something like a listvalues, realvalue, viewvalue, keyvalueo or another method

link publish delete flag offensive edit

answered 2009-12-21 04:15:38 +0800

henrichen gravatar image henrichen
3869 2
ZK Team

What is your use case?

Select a Thing from the Combobox, modify its name, and then save?

link publish delete flag offensive edit

answered 2009-12-21 05:30:59 +0800

subscriber gravatar image subscriber
57 2 2

You know in delphi there is a db combobox for binding data. it has database fieldvalue, list view value, list datavalue, and list keyvalue for bind list data and own data.

another example i have a Person class and it has department_id in database. i need listing deparments in combobox and setting selected department's id to person class. Also this is simple action for most of framework. but cant do it yet:(

link publish delete flag offensive edit

answered 2009-12-21 22:47:59 +0800

henrichen gravatar image henrichen
3869 2
ZK Team

updated 2009-12-21 22:49:14 +0800

Use the ThingController as your place to keep information, so you shall bind UI to the controller.

<?taglib uri="http://www.zkoss.org/dsp/web/core" prefix="c"?>
<?page title="new page title" contentType="text/html;charset=UTF-8"?>
<?init class="org.zkoss.zkplus.databind.AnnotateDataBinderInit"?>
<zk>
<zscript>
  List thingList = service.GenericService.getPersistableDao(application).list(model.Thing.class)
</zscript>
<window id="main" title="${c:l('app.title')}" border="normal" apply="controller.ThingController">
<grid>
<rows>
<row>
<label value="Parent Name"/>
<combobox model="@{thingList}" selectedItem="@{main$composer.thing}" value="@{main$composer.thing.name}">
<comboitem self="@{each=t}" label="@{t.name}" value="@{t.id}"/>
</combobox>
</row>

<row>
<label value="Name"/>
<textbox id="name" value="@{main$composer.thing.name}" constraint="no empty"></textbox>
</row>

<row>
<button label="${c:l('form.save')}" id="save" width="50px" />
</row>
</rows>
</grid>
</window>
</zk>

main$composer refer to your ThingController. Here means the composer applied to Window "main".

Then you can bind UI value to your ThingController properties (Need getXxx and setXxx). In this example, I bind selected "Thing" to property "thing" in ThingController, So then you can change the name of the selected Thing. Then if you press "save" button, the onClick$save() method of your ThingController will be called and persistent the modified "Thing" into database.

Here are some articles that talking about how data binding works. They might be aged but the concept is the same. They shall help you understand how the databinding works.

http://docs.zkoss.org/wiki/Two-way_Data_Binding_with_ZUML_Annotations
http://docs.zkoss.org/wiki/Zero_Code_Data_Binding_with_ZUML_Annotations
http://docs.zkoss.org/wiki/Data_Binding_Collection_Data_with_ZUML_Annotations

link publish delete flag offensive edit

answered 2009-12-22 03:29:19 +0800

subscriber gravatar image subscriber
57 2 2

Thanks for replies but Still i cant bind Thing parent_id in combox? Just i need is, when i select a item in combobox, selected items value bind to thing parent_id.

i looked every sample but cant find what i am looking for.

link publish delete flag offensive edit

answered 2009-12-22 05:18:23 +0800

henrichen gravatar image henrichen
3869 2
ZK Team

I don't quite follow you. The easiest way to discuss is that you provide the use case to describe your intention in steps and maybe we know how to help you.

link publish delete flag offensive edit

answered 2009-12-22 07:50:11 +0800

robertpic71 gravatar image robertpic71
1275 1

@subscriber

Unlike Delphi, there is no need to bind the id's in an ORM-Environment. The selectedItem="@{selected}" is not the id or value - it's the whole object.

So, like henris, example you need:

- A defined Object Thing (with getter/setter) to store the selected Item.
- Now you can refer to this object and view i.e. the name

Try you code:
<?taglib uri="http://www.zkoss.org/dsp/web/core" prefix="c"?>
<?page title="new page title" contentType="text/html;charset=UTF-8"?>
<?init class="org.zkoss.zkplus.databind.AnnotateDataBinderInit"?>
<zk>
<zscript>
model.Thing thing = new model.Thing();
List thingList = service.GenericService.getPersistableDao(application).list(model.Thing.class)
</zscript>
<window title="${c:l('app.title')}" border="normal" apply="controller.ThingController">
<grid>
<rows>
<row>
<label value="Parent Name"/>
<combobox model="@{thingList}" selectedItem="@{thing}" value="@{selected.name}" id="parent_id">
<comboitem self="@{each=t}" label="@{t.name}" value="@{t.id}"/>
</combobox>
</row>

<row>
<label value="Name"/>
<textbox id="name" value="@{thing.name}" constraint="no empty"></textbox>
</row>

<row>
<button label="${c:l('form.save')}" id="save" width="50px" />
</row>
</rows>
</grid>
</window>
</zk>

To explain:
1. you fill the model with the thingList
2. the combobox selects the thing for you and return whole thing object (there is no need for read the thing with the parent-id)
3. the view-binding (name.thing) refers to the selected object itself

PS: If you really need the id, this could done by Typeconverters.

@henri
I think he try the pre-ORM-Way: show the description and get the value (= id for db access).

/Robert

link publish delete flag offensive edit

answered 2009-12-22 21:54:16 +0800

subscriber gravatar image subscriber
57 2 2

i think i cant explain my problem clearly:) Thing class has a field its name is parent_id as u see in class file. Every Thing has a Parent. Parent is also a Thing like a tree model.

So user must select a parent(this a Thing too) for creating new Thing and type new Thing name.

<textbox id="name" value="@{thing.name}" constraint="no empty"></textbox> this works clear and binds Thing's name to name. But in Combobox selected Things id must be new Thing's parent_id.

In Controller(ThingController also in save method) GenericService.getPersistableDao(application).save(thing); thing.getParentId is null coz cant bind in combobox selected Thing id for new Thing parent_id.

link publish delete flag offensive edit

answered 2009-12-23 06:59:00 +0800

subscriber gravatar image subscriber
57 2 2

Guys problem solved. thanks for your replies.

but i wanna ask a weird thing that i met.

my Thing.class has an attribute named "thing". when i tried to set selected item from combobox to my attribute "thing" as following, that didnt work.

<combobox autodrop="true" model="@{thingList}" selectedItem="@{thing}">
<comboitem self="@{each=_thing}" label="@{_thing.name}" value="@{_thing.id}"/>
</combobox>

Thing.class also has an attribute "parent" it is also a Thing instance. when i tried to set selected to "thing"'s "parent" , that worked.

<combobox autodrop="true" model="@{thingList}" selectedItem="@{thing.parent}">
<comboitem self="@{each=_thing}" label="@{_thing.name}" value="@{_thing.id}"/>
</combobox>

how can be this weirdness explained?

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-12-20 15:31:53 +0800

Seen: 3,509 times

Last updated: Dec 24 '09

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