-
FEATURED COMPONENTS
First time here? Check out the FAQ!
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; } }
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.
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);
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.
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
Asked: 2012-06-12 14:04:15 +0800
Seen: 231 times
Last updated: Jun 13 '12