ZK提供两种分页方式,一种是自带的“mold="paging"”的形式;另一种是采用“<paging>”组件添加分页功能。具体实现如下:
第一种:采用自带的“mold="paging"”:
infoList.zul:
<zk> <window id="mainwin" border="normal" width="600px" apply="${myComposer}"> <listbox id="li" mold="paging" pageSize="5"> <listhead> <listheader label="Id" sort="auto(Id)"/> <listheader label="Name" sort="auto(Name)"/> <listheader label="Address" sort="auto(Address)"/> <listheader /> </listhead> <listitem self="@{each=in }"> <listcell id="colId" label="@{in.id }"></listcell> <listcell id="colName" label="@{in.name }"></listcell> <listcell id="colAddr" label="@{in.address }"></listcell> <listcell></listcell> </listitem> </listbox> </window> </zk>
后台控制类 MyComposer.java:
public class MyComposer extends GenericForwardComposer { protected Textbox name; protected Textbox address; private Listbox li; private Window mainwin; private AnnotateDataBinder binder; private Service service; private List<Info> infoList; private int totalsize; public void setInfoList(List<Info> infoList) { this.infoList = infoList; } public void setService(Service service) { this.service = service; } public void onCreate$mainwin(Event event) throws Exception { binder = new AnnotateDataBinder(mainwin); binder.loadAll(); } public void doAfterCompose(Component component) throws Exception { super.doAfterCompose(component); infoList = service.findAllInfos(); totalsize = infoList.size(); li.setModel(new ListModelList(infoList)); li.setItemRenderer(new ListitemRenderer() { @Override public void render(Listitem item, Object data) throws Exception { // TODO Auto-generated method stub Info itinfo = (Info) data; Listcell colId = new Listcell(); Listcell colName = new Listcell(); Listcell colAddr = new Listcell(); colId.setLabel(itinfo.getId().toString()); colId.setParent(item); colName.setLabel(itinfo.getName()); colName.setParent(item); colAddr.setLabel(itinfo.getAddress()); colAddr.setParent(item); } }); } }
第二种:采用“<paging>”组件进行分页:
paging.zul:
<zk> <window id="pagingWin" title="paging" border="normal" apply="${paging_ctrl}"> <grid> <rows> <row>人员列表:</row> <row> <listbox id="li" width="100%"> <listhead> <listheader label="ID" sort="auto(Id)" /> <listheader label="姓名" /> <listheader label="地址" /> </listhead> </listbox> <separator /> </row> </rows> </grid> <paging id="pge" pageSize="10"></paging> </window> </zk>
后台控制类Paging_ctrl.java:
public class Paging_ctrl extends GenericForwardComposer { private AnnotateDataBinder binder; private Window pagingWin; private Listbox li; private Paging pge; private List<Info> infoList; int maxCount = 0; private Service service; public void onCreate$pagingWin(Event event) throws Exception { binder = new AnnotateDataBinder(pagingWin); binder.loadAll(); } public void doAfterCompose(Component component) throws Exception { super.doAfterCompose(component); maxCount = service.findAllInfos().size(); pge.setTotalSize(maxCount); final int PAGE_SIZE = pge.getPageSize(); redraw(0, PAGE_SIZE); pge.addEventListener("onPaging", new EventListener() { public void onEvent(Event event) { PagingEvent pe = (PagingEvent) event; int pgno = pe.getActivePage();// 页数(从零计算) int start = pgno * PAGE_SIZE; redraw(start, PAGE_SIZE); } }); } @SuppressWarnings("unchecked") private void redraw(int offSet, int pageSize) { li.getItems().clear(); List<Info> list = service.findInfoByPage(offSet, pageSize); for (Info info : list) { Listitem item = new Listitem(); item.setValue(info); item.appendChild(new Listcell("" + info.getId())); item.appendChild(new Listcell(info.getName())); item.appendChild(new Listcell(info.getAddress())); li.appendChild(item); } } public void setService(Service service) { this.service = service; } }
分析:
第一种方式是把数据全部加载到内存再进行分页,达到几十W跳数据后将降低性能;
而第二种方式则采用hibernate的分页查询方式,性能将大大加强。因此推荐第二种方式。