-
FEATURED COMPONENTS
First time here? Check out the FAQ!
I have a page similar to the following example where I have a list of items with fields to display the details of the selected list item. I want the ability for my users to create a new item for the list, but haven't been able to get it to work. When the user clicks the button to create a new item for the list I'm creating the item and adding it to the model, but the list isn't refreshing with the new item displayed. I bound the model to my List in my window, but the refresh of the model is occurring prior to the method call associated with my button's onClick event. What am I doing wrong in the following example code?
data_bind_example.zul
<?xml version="1.0" encoding="UTF-8"?> <?init class="org.zkoss.zkplus.databind.AnnotateDataBinderInit" ?> <?page title="Data Binding Example" id="data_bind_example" ?> <window id="testWin" xmlns="http://www.zkoss.org/2005/zul" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:a="http://www.zkoss.org/2005/zk/annotation" xsi:schemaLocation="http://www.zkoss.org/2005/zul http://www.zkoss.org/2005/zul/zul.xsd" use="com.test.DataBindExampleWindow" width="95%" position="center" sclass="body1"> <hbox> <listbox model="@{testWin.people, load-when='first_name.onChange, last_name.onChange, create_person_btn.onClick', access='load'}" selectedItem="@{testWin.selectedPerson, load-when='create_person_btn.onClick', access='both'}" itemRenderer="com.test.PersonListitemRenderer" rows="10"> </listbox> <vbox> <hbox> First Name: <textbox id="first_name" value="@{testWin.selectedPerson.firstName}" /> </hbox> <hbox> Last Name: <textbox id="last_name" value="@{testWin.selectedPerson.lastName}" /> </hbox> <hbox> Date of Birth: <datebox value="@{testWin.selectedPerson.dateOfBirth}" format="MM/dd/yyyy" /> </hbox> </vbox> </hbox> <button id="create_person_btn" onClick="testWin.createPerson()" label="Create" /> </window>
DataBindExampleWindow.java
package com.test; import java.util.ArrayList; import java.util.Calendar; import java.util.List; import org.zkoss.zul.ListModel; import org.zkoss.zul.SimpleListModel; import org.zkoss.zul.Window; public class DataBindExampleWindow extends Window { private static final long serialVersionUID = 2255149977062345551L; private List<Person> people = new ArrayList<Person>(); private Person selectedPerson = null; public DataBindExampleWindow() { super(); init(); } public DataBindExampleWindow(String title, String border, boolean closable) { super(title, border, closable); init(); } public ListModel getPersonListModel() { return new SimpleListModel(people); } private void init() { Calendar bday = Calendar.getInstance(); bday.add(Calendar.YEAR, -1 * (int) Math.round(Math.random() * 70)); bday.add(Calendar.MONTH, (int) Math.round(Math.random() * 12)); bday.add(Calendar.DATE, (int) Math.round(Math.random() * 31)); people.add(new Person("Barack", "Obama", bday.getTime())); bday = Calendar.getInstance(); bday.add(Calendar.YEAR, -1 * (int) Math.round(Math.random() * 70)); bday.add(Calendar.MONTH, (int) Math.round(Math.random() * 12)); bday.add(Calendar.DATE, (int) Math.round(Math.random() * 31)); people.add(new Person("Joe", "Biden", bday.getTime())); bday = Calendar.getInstance(); bday.add(Calendar.YEAR, -1 * (int) Math.round(Math.random() * 70)); bday.add(Calendar.MONTH, (int) Math.round(Math.random() * 12)); bday.add(Calendar.DATE, (int) Math.round(Math.random() * 31)); people.add(new Person("John", "McCain", bday.getTime())); bday = Calendar.getInstance(); bday.add(Calendar.YEAR, -1 * (int) Math.round(Math.random() * 70)); bday.add(Calendar.MONTH, (int) Math.round(Math.random() * 12)); bday.add(Calendar.DATE, (int) Math.round(Math.random() * 31)); people.add(new Person("Sarah", "Palin", bday.getTime())); bday = Calendar.getInstance(); bday.add(Calendar.YEAR, -1 * (int) Math.round(Math.random() * 70)); bday.add(Calendar.MONTH, (int) Math.round(Math.random() * 12)); bday.add(Calendar.DATE, (int) Math.round(Math.random() * 31)); people.add(new Person("George", "Bush", bday.getTime())); } public void createPerson() { final Person person = new Person(null, null, null); people.add(person); setSelectedPerson(person); } /** * @return the selectedPerson */ public Person getSelectedPerson() { return selectedPerson; } /** * @param selectedPerson the selectedPerson to set */ public void setSelectedPerson(Person selectedPerson) { this.selectedPerson = selectedPerson; } /** * @return the people */ public List<Person> getPeople() { return people; } /** * @param people the people to set */ public void setPeople(List<Person> people) { this.people = people; } }
Person.java
package com.test; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; public class Person { private String firstName = null; private String lastName = null; private Date dateOfBirth = null; private String formattedDateOfBirth = null; public Person() { super(); } public Person(String firstName, String lastName) { super(); this.firstName = firstName; this.lastName = lastName; } public Person(String firstName, String lastName, Date dateOfBirth) { super(); setFirstName(firstName); setLastName(lastName); setDateOfBirth(dateOfBirth); } public String getFullname() { final StringBuffer fullName = new StringBuffer(); if (firstName != null) { fullName.append(firstName).append(" "); } if (lastName != null) { fullName.append(lastName); } return fullName.toString().trim(); } public Date getDateOfBirth() { return dateOfBirth; } public void setDateOfBirth(Date dateOfBirth) { this.dateOfBirth = dateOfBirth; if (dateOfBirth == null) { setFormattedDateOfBirth(""); } else { final DateFormat formatter = new SimpleDateFormat("MMM d, yyyy"); setFormattedDateOfBirth(formatter.format(dateOfBirth)); } } 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 getFormattedDateOfBirth() { return formattedDateOfBirth; } public void setFormattedDateOfBirth(String formattedDateOfBirth) { this.formattedDateOfBirth = formattedDateOfBirth; } }
PersonListitemRenderer.java
package com.test; import org.zkoss.zul.Listcell; import org.zkoss.zul.Listitem; import org.zkoss.zul.ListitemRenderer; public class PersonListitemRenderer implements ListitemRenderer { public void render(Listitem item, Object data) throws Exception { if (data instanceof Person) { final Person p = (Person) data; item.setValue(p); final Listcell cell = new Listcell(p.getFullname()); // How to bind the label of the cell to my person's fullname? item.appendChild(cell); } } }
You should invoke the loadAll() method after you added the new item.
For example,
AnnotateDataBinder binder = page.getVariable("binder"); binder.loadAll();
Hi all,
i have the same problem to actualize my listbox. But i do Data Binding not in the zul-file. These are done in my java code.
The synchronisation of deleting and editing an item are working well. But by adding a new item to the listModelList i don't know how to synchronize it for to showing the new item in the listBox.
public void doSave(){ ... // create the new office object ... // save the object ... // synchronize the listBox ListModelList lml = (ListModelList) listBoxOffice.getListModel(); // check if office is new to the list or edited. // (-1) is not found, so it's new. if (lml.indexOf(office) == -1) { lml.add(office); listBoxOffice.invalidate(); // don't work lml.set(lml.indexOf(office), office); } else { lml.set(lml.indexOf(office), office); }
Does anybody have a idea to actualize the listBox in Java Code.
thanks
Stephan
Hi Stephan,
Have you tried I mentioned before?
@jumperchen
Don't work for me. I try this:
... private Listbox lbOffice // autowire ... Page page = lbOffice.getDesktop().getPage("indexPage"); AnnotateDataBinder binder = (AnnotateDataBinder) page.getVariable("binder"); binder.loadAll();
By debugging i see that the binder variable have 32 eńtries, but all are null. I have no binding in my zul-file, this is done in the java-code.
I'm wondering that changes or deletes are syncronized well in the listbox but no new item????
I miss a refresh() methode on the listBox. I added the new item to the ListModelList and they are binded to the Listbox. So there must be a methode to refresh/synchronize the listbox with the changed ListModelList.
Any idea
Stephan
Solved
many thx all.
I have had the AnnotateDataBinder in my BaseCtrl Class already defined.
So i have now only overhanded the ControllerClass which have access to the listBox to the new opened dialogWindow.
Then after saving the new object in database i call a public methode in the listBox Controller class for binder.loadAll(). That's all.
Stephan
to make a call
Asked: 2008-12-04 18:29:05 +0800
Seen: 1,758 times
Last updated: Jun 23 '09