码迷,mamicode.com
首页 > 其他好文 > 详细

ZK listbox 两种分页使用及比较

时间:2015-12-10 19:11:50      阅读:904      评论:0      收藏:0      [点我收藏+]

标签:

参考:http://tsinglongwu.iteye.com/blog/849923

以下代码模拟数据量大时情况,采用“<paging>”组件方式

前台Listbox.zul :

技术分享
<?page title="Listbox使用" contentType="text/html;charset=UTF-8"?>
<zk xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xmlns="http://www.zkoss.org/2005/zul" 
        xsi:schemaLocation="http://www.zkoss.org/2005/zul http://www.zkoss.org/2005/zul">
<window id="listboxWin" border="normal" apply="test.ListboxCtrl">
    <listbox id="listbox">
        <listhead sizable="true" >
            <listheader id="nameListHeader" label="姓名" align="center"/>
        </listhead>
    </listbox>
    <paging id="pagetest" pageSize="10" totalSize="50000" detailed="true"/>
</window>
</zk>
View Code

ListboxCtrl.java :

技术分享
package test;

import java.util.ArrayList;
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.util.GenericForwardComposer;
import org.zkoss.zul.Label;
import org.zkoss.zul.ListModelList;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listcell;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.ListitemRenderer;
import org.zkoss.zul.Paging;
import org.zkoss.zul.event.PagingEvent;

public class ListboxCtrl extends GenericForwardComposer<Component>{
    
    private static final long serialVersionUID = 1L;
    
    private Listbox listbox;
    private ListModelList<String> model;
    private List<String> testList = new ArrayList<String>();
    private Paging pagetest;
    private int count = 1;
    
    @Override
    public void doAfterCompose(Component comp) throws Exception {
        // TODO Auto-generated method stub
        super.doAfterCompose(comp);
        
        //填充第一页数据
        for (int i = 1; i < pagetest.getPageSize()+1; i++) {
            testList.add(i+"");
        }
        
        model = new ListModelList<String>(testList);
        listbox.setModel(model);
        listbox.setItemRenderer(new testItemRenderer());
        
        //监听分页改变事件
        pagetest.addEventListener("onPaging", new EventListener<Event>() {

            @Override
            public void onEvent(Event event) throws Exception {
                // TODO Auto-generated method stub
                PagingEvent pe = (PagingEvent) event;
                
                redraw(pe.getActivePage()+1,pagetest.getPageSize());
            }
        });
    }
    
    public class testItemRenderer implements ListitemRenderer<String>{

        @Override
        public void render(Listitem item, String data, final int index)
                throws Exception {
            // TODO Auto-generated method stub
            
            item.setValue(data);
            Listcell listcell2 = new Listcell();
            Label label = new Label(data);
            listcell2.appendChild(label);
            listcell2.setParent(item);
        }
    }
    
    private void redraw(int activePage,int pageSize) {  
        
        //清空所有数据
        listbox.getItems().clear();  
        testList.clear();
        
        int total = activePage * pageSize;
        
        for (int i = total-pageSize+1; i <= total; i++) {
            
            //当超过总页数时
            if (i > pagetest.getTotalSize()) {
                break;
            }
            
            testList.add(i+"");
        }
        
        model = new ListModelList<String>(testList);
        listbox.setModel(model);
    } 
}
View Code

效果:

技术分享

 

ZK listbox 两种分页使用及比较

标签:

原文地址:http://www.cnblogs.com/langdangyunliu/p/5036743.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!