0

Refresh Listbox Model after button click

asked 2008-12-04 18:29:05 +0800

blandrp gravatar image blandrp
72 2

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);
		}
	}
}

delete flag offensive retag edit

8 Replies

Sort by » oldest newest

answered 2008-12-05 02:21:27 +0800

jumperchen gravatar image jumperchen
3909 2 8
http://jumperchen.blogspo... ZK Team

You should invoke the loadAll() method after you added the new item.
For example,

AnnotateDataBinder binder = page.getVariable("binder");
      binder.loadAll();

link publish delete flag offensive edit

answered 2008-12-08 16:24:46 +0800

terrytornado gravatar image terrytornado flag of Germany
9393 3 7 16
http://www.oxitec.de/

updated 2008-12-08 16:33:07 +0800

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

link publish delete flag offensive edit

answered 2008-12-09 02:47:00 +0800

jumperchen gravatar image jumperchen
3909 2 8
http://jumperchen.blogspo... ZK Team

Hi Stephan,

Have you tried I mentioned before?

link publish delete flag offensive edit

answered 2008-12-09 12:25:36 +0800

terrytornado gravatar image terrytornado flag of Germany
9393 3 7 16
http://www.oxitec.de/

@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

link publish delete flag offensive edit

answered 2008-12-10 10:12:18 +0800

terrytornado gravatar image terrytornado flag of Germany
9393 3 7 16
http://www.oxitec.de/

updated 2008-12-10 14:29:05 +0800

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

link publish delete flag offensive edit

answered 2009-06-22 11:27:47 +0800

biapar gravatar image biapar
12

Code?

link publish delete flag offensive edit

answered 2009-06-23 10:36:07 +0800

terrytornado gravatar image terrytornado flag of Germany
9393 3 7 16
http://www.oxitec.de/

updated 2009-06-23 10:36:31 +0800

@biapar

Sorry, it's old stuff. I do it today others.
Have a look at the whole code here.

Stephan

link publish delete flag offensive edit

answered 2009-06-23 11:09:18 +0800

robertpic71 gravatar image robertpic71
1275 1

@biapar
>> Code?

Since 3.6.1 the is a load-after (see docs)

However, because i have many UI elements i prefer catch binder-instance an do a bind.loadAll() or bind.load(Component scope).

Check this examples.

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: 2008-12-04 18:29:05 +0800

Seen: 1,758 times

Last updated: Jun 23 '09

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