0

Problem while trying to do databinding on selectedItem property ListBox. ZK 6.0.0 MVVM.

asked 2012-06-06 00:37:27 +0800

yagamipaul gravatar image yagamipaul
15 2

updated 2012-06-07 23:45:53 +0800

Hi, thanks in advance for your help.

i've been having some problems when i try to do databinding to a list box or a combobox.

I have a Collection of a custom type, my collection is an ArrayList, and I use it as a model for the component, I load the collection from Database using hibernate.

The data gets loaded correctly, i can open the popup as a selection list and it works. I am binding the selectedItem property to an Object to do
the save/load operations. The save operation works OK, but not the load. When I open the page, there is no selected item on the component, but the Object binded to selected item is been setted.

The initial value of the object is loaded in another query by hibernate, so the property binded to selectedItem has the same value, but maybe is not the same instance.

I have noticed that if I update the object binded to "selectedItem" to use the same instances that the "model", when i open the page I will get the selectedItem on list, as it should be.

I have a list of CellPhoneNumber objects, each one has a property called "operator" thats the one that I'm trying to bind to a listBox.
The list box should show a label for the operator selected for every CellPhoneNumber. Instead of that, always shows nothing selected, but the list has data, so I can select any operator and assign to a CellphoneNumber.

My listbox on ZUL:

<listbox  mold="select" multiple="false" model="@load(vm.operatorList)"
		selectedItem="@bind(item.operator)" >
		<template name="model" var="itemOperator">
			<listitem  label="@bind(itemOperator.label)" value="@bind(itemOperator)" ></listitem>
		</template>
</listbox>

My model, (this doesn't work to get the initial value selected):

this.operatorList = facade.listAllActiveOperators();

My second model, updated, and this works:

                this.operatorList = facade.listAllActiveOperators();
		Map<Long, Operator> map = new HashMap<Long, Operator>();
                // Track the instances by Id using a map
		for(Operator o : operatorList){
			map.put(o.getId(), o);
		}

                // Update a list of objects, set the property operator by using the same instaces as the model
		for (CellPhoneNumber cp: this.cellPhoneNumbers) {			
			cp.setOperator(map.get(cp.getOperator().getId()));
		}

I dont understand why I have to use the same instances in the model and the selectedItem binded object. On the initial load almost always will be different.

Thanks.
Jean Paul Manjarres C.

delete flag offensive retag edit

1 Reply

Sort by ยป oldest newest

answered 2012-06-08 00:16:39 +0800

yagamipaul gravatar image yagamipaul
15 2

After debugging a lot my code, i found the problem.

When you user HIbernate o JPA, your entities get proxied, so, the queried bean's class are not the same of the entities.
You need to define correctly your hash and equals code for the entity to be used on a list box or a combobox.

You have to be careful that the equals method don't compare the class, and don't use the properties directly but using getters and setters.

My equals method that work, looks like this:

@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
//		if (getClass() != obj.getClass()) {
//			return false;
//		}
		Operator other = (Operator) obj;
		if (id != other.getId()) {
			return false;
		}
		if (label == null) {
			if (other.label != null) {
				return false;
			}
		} else if (!label.equals(other.getLabel())) {
			return false;
		}
		if (name != other.getName()) {
			return false;
		}
		return true;
	}

For me, the question is answered. I couldn't find this information on any docs.

Thanks.
Jean Paul Manjarres Correal.

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: 2012-06-06 00:37:27 +0800

Seen: 235 times

Last updated: Jun 08 '12

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