zk listbox 点击列标题实现排序功能

前台(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:style src="css/a.css"/>
    
<z:window id="gradingWin" border="none" apply="test.test">
<z:listbox id="testListbox">
    <z:listhead>
        <z:listheader id="fullName" label="Full Name"/>
        <z:listheader id="age" label="Age"/>
    </z:listhead>
</z:listbox>
</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.util.GenericForwardComposer;
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 test extends GenericForwardComposer<Component>{
    
    private static final long serialVersionUID = 1L;
    
    private ListModelList<Person> testmodel;
    private Listbox testListbox;
    private Listheader fullName;
    private Listheader age;
    
    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");
        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);
    }
    
    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 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;
    }
}

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。