前台(test.zul):
<?page title="测试" contentType="text/html;charset=UTF-8"?> <z:zk xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:z="http://www.zkoss.org/2005/zul" xsi:schemaLocation="http://www.zkoss.org/2005/zul http://www.zkoss.org/2005/zul"> <z:window id="gradingWin" border="none" apply="test.ListboxCtrl"> <z:listbox id="testListbox"> <z:listhead> <z:listheader id="fullName" label="Full Name"/> <z:listheader id="age" label="Age"/> </z:listhead> </z:listbox> <z:label style="color:red;font-size:30px;" value="修改fullName为wwee的Age为50"/> <z:button id="testBtn" label="修改"></z:button> </z:window> </z:zk>
后台(test.java):
package test; import java.util.ArrayList; import java.util.Comparator; import java.util.List; import org.zkoss.zk.ui.Component; import org.zkoss.zk.ui.event.Event; import org.zkoss.zk.ui.event.EventListener; import org.zkoss.zk.ui.event.Events; import org.zkoss.zk.ui.util.GenericForwardComposer; import org.zkoss.zul.Button; import org.zkoss.zul.ListModelList; import org.zkoss.zul.Listbox; import org.zkoss.zul.Listcell; import org.zkoss.zul.Listheader; import org.zkoss.zul.Listitem; import org.zkoss.zul.ListitemRenderer; import org.zkoss.zul.Window; public class ListboxCtrl extends GenericForwardComposer<Component>{ private static final long serialVersionUID = 1L; private ListModelList<Person> testmodel; private Listbox testListbox; private Listheader fullName; private Listheader age; private Button testBtn; Window gradingWin; @Override public void doAfterCompose(Component comp) throws Exception { // TODO Auto-generated method stub super.doAfterCompose(comp); //填充测试数据 Person person = new Person(); List<Person> persons = person.getPersons(); // 设置数据模型 testmodel = new ListModelList<Person>(persons); testListbox.setModel(testmodel); testListbox.setItemRenderer(new testItemRenderer()); //设置分页 testListbox.setMold("paging"); testListbox.getPagingChild().setAutohide(false); fullName.setSort("auto");//必须设置,否则无法使用升、降序排列功能(zk规定) age.setSort("auto");//同上 // 按姓名升序 ComparatorName comparatorName = new ComparatorName(); fullName.setSortAscending(comparatorName); // 按姓名降序 ComparatorDescName comparatorDescName = new ComparatorDescName(); fullName.setSortDescending(comparatorDescName); // 按年龄升序 ComparatorAge comparatorAge = new ComparatorAge(); age.setSortAscending(comparatorAge); // 按年龄降序 ComparatorDescAge comparatorDescAge = new ComparatorDescAge(); age.setSortDescending(comparatorDescAge); testBtn.addEventListener(Events.ON_CLICK, new EventListener<Event>() { @Override public void onEvent(Event event) throws Exception { // TODO Auto-generated method stub ListModelList<Person> model = (ListModelList) testListbox.getModel(); for (int i = 0; i < model.size(); i++) { Person c = (Person) model.get(i); if (c.getFullName().equals("wwee")) { c.setAge(50); model.set(i, c); break; } } } }); } public class testItemRenderer implements ListitemRenderer<Person>{ @Override public void render(Listitem item, Person person, int index) throws Exception { // TODO Auto-generated method stub new Listcell(person.getFullName()).setParent(item); new Listcell(person.getAge()+"").setParent(item); } } public class ComparatorAge implements Comparator<Person>{ @Override public int compare(Person o1, Person o2) { // TODO Auto-generated method stub if (o1.getAge() > o2.getAge()) { return 1; }else if (o1.getAge() < o2.getAge()){ return -1; }else { return 0; } } } public class ComparatorDescAge implements Comparator<Person>{ @Override public int compare(Person o1, Person o2) { // TODO Auto-generated method stub if (o1.getAge() < o2.getAge()) { return 1; }else if (o1.getAge() > o2.getAge()){ return -1; }else { return 0; } } } public class ComparatorName implements Comparator<Person>{ @Override public int compare(Person o1, Person o2) { // TODO Auto-generated method stub return o1.getFirstName().compareTo(o2.getFirstName()); } } public class ComparatorDescName implements Comparator<Person>{ @Override public int compare(Person o1, Person o2) { // TODO Auto-generated method stub return o2.getFirstName().compareTo(o1.getFirstName()); } } } class Person implements Comparator <Person> { private String firstName; private String lastName; private String fullName; private int age; List<Person> persons = new ArrayList<Person>(); public Person(String f, String l, int a) { firstName = f; lastName = l; age = a; fullName = f+l; } public Person(){ persons.add(new Person("a", "b", 2)); persons.add(new Person("a", "b", 3)); persons.add(new Person("a", "b", 4)); persons.add(new Person("cc", "dd", 2)); persons.add(new Person("cc", "dd", 3)); persons.add(new Person("cc", "dd", 4)); persons.add(new Person("ww", "ee", 23)); } public String getFirstName() { return firstName; } public String getLastName() { return lastName; } public String getFullName() { return fullName; } public void setFullName(String fullName){ this.fullName = fullName; } public void setAge(int age){ this.age = age; } public int getAge() { return age; } public List<Person> getPersons(){ return persons; } @Override public int compare(Person o1, Person o2) { // TODO Auto-generated method stub return 0; } }