0

MVVM Combox Box not selecting the item if set selecteditem property in VM............Please help

asked 2012-06-12 14:04:15 +0800

Senthilchettyin gravatar image Senthilchettyin flag of India
2623 3 8
http://emrpms.blogspot.in...

zul file

<?page title="new page title" contentType="text/html;charset=UTF-8"?>
<zk>
	<window id="myWin" title="new page title" border="normal"
		apply="org.zkoss.bind.BindComposer"
		viewModel="@id('myvm') @init('mydomainUI.Example1VM')">
		<combobox model="@bind(myvm.users)"
			selectedItem="@bind(myvm.selectedUser)"
			onSelect="@command('ShowSelectedUser')">
			<template name="model" var="p1">
				<comboitem label="@bind(p1.userName)"
					value="@bind(p1.userCode)" />
			</template>
		</combobox>
		<button id="Submit" label="Show Selected User Code"
			onClick="@command('ShowSelectedUser')" />
	</window>
</zk>

VM

package mydomainUI;

import java.util.ArrayList;
import java.util.List;
import org.zkoss.bind.annotation.Command;
import org.zkoss.bind.annotation.Init;
import org.zkoss.bind.annotation.NotifyChange;
import org.zkoss.zul.Messagebox;

import mydomain.users;

public class Example1VM {

	private List<users> allUsers= new ArrayList<users>();
	private users selectedUser = new users();
	
	public users getSelectedUser() {
		return selectedUser;
	}

	public void setSelectedUser(users selectedUser) {
		this.selectedUser = selectedUser;
	}

	@Command
	public void ShowSelectedUser()
	{
		Messagebox.show(this.selectedUser.getUserCode());
	}

	public List<users> getUsers() {
		
		users u1 = new users();
		u1.setUserName("John");
		u1.setUserCode("User101");
		u1.setUserPassword("xxxxx");
		u1.setFirstName("JohnFirstName");
		u1.setLastName("JohnLastName");
		u1.setHomePhone("1111111111");
		u1.setMobilePhone("222222");
		u1.setActive("Y");
		allUsers.add(u1);

		u1 = new users();
		u1.setUserName("Robert");
		u1.setUserCode("User102");
		u1.setUserPassword("xxxxx");
		u1.setActive("Y");
		u1.setFirstName("RobertFirstName");
		u1.setLastName("RobertLastName");
		u1.setHomePhone("53534343");
		u1.setMobilePhone("4534343");
		allUsers.add(u1);

		u1 = new users();
		u1.setUserName("Sean");
		u1.setUserCode("User103");
		u1.setUserPassword("xxxxx");
		u1.setActive("N");
		u1.setFirstName("SeanFirstName");
		u1.setLastName("SeanLastName");
		u1.setHomePhone("643434343");
		u1.setMobilePhone("64343445434");
		allUsers.add(u1);

		u1 = new users();
		u1.setUserName("Marry");
		u1.setUserCode("User104");
		u1.setUserPassword("xxxxx");
		u1.setActive("N");
		u1.setFirstName("MarryFirstName");
		u1.setLastName("MarryLastName");
		u1.setHomePhone("8644344");
		u1.setMobilePhone("44333");
		allUsers.add(u1);

		
		users u2 = new users();
		u2.setUserCode("User101");
		u2.setUserName("John");
		setSelectedUser(u2);

		return allUsers;
		
	}
}

users.JAVA

package mydomain;

public class users {

	private String userCode;
	private String userName;
	private String userPassword;
	private String active;
	private String firstName;
	private String lastName;
	private String homePhone;
	private String mobilePhone;

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public String getHomePhone() {
		return homePhone;
	}

	public void setHomePhone(String homePhone) {
		this.homePhone = homePhone;
	}

	public String getMobilePhone() {
		return mobilePhone;
	}

	public void setMobilePhone(String mobilePhone) {
		this.mobilePhone = mobilePhone;
	}

	public String getUserCode() {
		return userCode;
	}

	public void setUserCode(String userCode) {
		this.userCode = userCode;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getUserPassword() {
		return userPassword;
	}

	public void setUserPassword(String userPassword) {
		this.userPassword = userPassword;
	}

	public String getActive() {
		return active;
	}

	public void setActive(String active) {
		this.active = active;
	}

}


zk fiddle

I am trying set default value in the VM after all the items are loaded. but the UI Combox box is not showing that default value set.

delete flag offensive retag edit

4 Replies

Sort by ยป oldest newest

answered 2012-06-13 01:21:40 +0800

henrichen gravatar image henrichen
3869 2
ZK Team

updated 2012-06-13 01:23:30 +0800

selectedUser must equal to one item of the allUsers or no way for the Binder to know that one of the item in the allUsers is selected.

Two way to solve your case:

1. Implement hashCode() and equals() in your "users" class so any instance that has same, say userCode, are equals to each other. BTW, I suggest follow the Java coding convention, class name better start with uppercase character.
2. set directly one item in the allUsers to selectedUser:

  ...
		users u1 = new users();
		u1.setUserName("John");
		u1.setUserCode("User101");
		u1.setUserPassword("xxxxx");
		u1.setFirstName("JohnFirstName");
		u1.setLastName("JohnLastName");
		u1.setHomePhone("1111111111");
		u1.setMobilePhone("222222");
		u1.setActive("Y");
		allUsers.add(u1);

          users u2 = u1;  //<------ set the item directlyl into selectedUser
          
		u1 = new users();
		u1.setUserName("Robert");

 ...

//		users u2 = new users();
//		u2.setUserCode("User101");
//		u2.setUserName("John");
		setSelectedUser(u2);

link publish delete flag offensive edit

answered 2012-06-13 04:11:12 +0800

Senthilchettyin gravatar image Senthilchettyin flag of India
2623 3 8
http://emrpms.blogspot.in...

Hi henrichen

Thank you for your reply. But this is only prototype, actual values are coming from the DB using Hibernate. So i may not know which object holds my id value.
ZK Should improve here, by setting the value property , it should match and default the combox box.

However, for time being , i am resolving as follows


for (users myuser : allUsers) {
if (myuser.getUserCode() == "User102") {
setSelectedUser(myuser);
}
}


by the way, i am also new java. is there any better method exists to find a object in the collection instead of loop.

link publish delete flag offensive edit

answered 2012-06-13 06:23:27 +0800

henrichen gravatar image henrichen
3869 2
ZK Team

Then I will suggest you go the number 1 solution. Provide equals() and hashCode() on userCode for your "users" class. That will solve the issue and is the typical way to handle Hibernate data object.

link publish delete flag offensive edit

answered 2012-06-13 07:50:31 +0800

Senthilchettyin gravatar image Senthilchettyin flag of India
2623 3 8
http://emrpms.blogspot.in...

Oh. Let me learn the hashcode and equals operator stuff, then i will try to implement.

Since i am new Java, came from VB 6

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
3 followers

RSS

Stats

Asked: 2012-06-12 14:04:15 +0800

Seen: 231 times

Last updated: Jun 13 '12

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