隨筆的一個(gè)記錄。
有兩個(gè)參考類(lèi),
import java.util.List;/*** 分頁(yè)顯示對(duì)象* @param <T>*/public class PageView<T> {/** 分頁(yè)數(shù)據(jù) **/private List<T> records;/** 頁(yè)碼開(kāi)始索引和結(jié)束索引 **/private PageIndex pageindex;/** 總頁(yè)數(shù) **/private long totalpage = 1;/** 每頁(yè)顯示記錄數(shù) **/private int maxresult = 12;/** 當(dāng)前頁(yè) **/private int currentpage = 1;/** 總記錄數(shù) **/private long totalrecord;/** 頁(yè)碼數(shù)量 **/private int pagecode = 10;/** 要獲取記錄的開(kāi)始索引 **/public int getFirstResult() {return (this.currentpage-1) * this.maxresult;}public int getPagecode() {return pagecode;}public void setPagecode(int pagecode) {this.pagecode = pagecode;}public PageView(int maxresult, int currentpage) {this.maxresult = maxresult;this.currentpage = currentpage;}public void setQueryResult(QueryResult<T> qr){setTotalrecord(qr.getTotal());setRecords(qr.getResultList());}public long getTotalrecord() {return totalrecord;}public void setTotalrecord(long totalrecord) {this.totalrecord = totalrecord;setTotalpage(this.totalrecord % this.maxresult == 0 ? this.totalrecord / this.maxresult : this.totalrecord / this.maxresult + 1);}public List<T> getRecords() {return records;}public void setRecords(List<T> records) {this.records = records;}public PageIndex getPageindex() {return pageindex;}public long getTotalpage() {return totalpage;}public void setTotalpage(long totalpage) {this.totalpage = totalpage;this.pageindex = PageIndex.getPageIndex(pagecode, currentpage, totalpage);}/*** 每頁(yè)顯示記錄數(shù)* @return*/public int getMaxresult() {return maxresult;}public int getCurrentpage() {return currentpage;}}
另外一個(gè)
/*** 算出頁(yè)碼的開(kāi)始索引和結(jié)束索引*/public class PageIndex {/** 開(kāi)始索引 */private long startindex;/** 結(jié)束索引 */private long endindex;public PageIndex(long startindex, long endindex) {this.startindex = startindex;this.endindex = endindex;}public long getStartindex() {return startindex;}public void setStartindex(long startindex) {this.startindex = startindex;}public long getEndindex() {return endindex;}public void setEndindex(long endindex) {this.endindex = endindex;}/*** 算出頁(yè)碼的開(kāi)始索引和結(jié)束索引* @param viewpagecount 頁(yè)碼數(shù)量* @param currentPage 當(dāng)前頁(yè)數(shù)* @param totalpage 總頁(yè)數(shù)* @return*/public static PageIndex getPageIndex(long viewpagecount, int currentPage, long totalpage){long startpage = currentPage - (viewpagecount % 2 == 0 ? viewpagecount / 2 - 1 : viewpagecount / 2);long endpage = currentPage + viewpagecount / 2;if(startpage < 1){startpage = 1;if(totalpage >= viewpagecount) endpage = viewpagecount;else endpage = totalpage;}if(endpage > totalpage){endpage = totalpage;if((endpage - viewpagecount)> 0) startpage = endpage - viewpagecount + 1;else startpage = 1;}return new PageIndex(startpage, endpage);}
下面是Freemarker的分頁(yè)宏
<#-- 總頁(yè)數(shù),當(dāng)前頁(yè) --><#macro pagination pageView><div class="pages"><label>共${pageView.totalpage}頁(yè),約${pageView.totalrecord}條數(shù)據(jù)</label><#if pageView.currentpage != 1><a href="javascript:pageinationView(1)" title="首頁(yè)" class="nav"><span>首頁(yè)</span></a><a href="javascript:pageinationView(${pageView.currentpage - 1})" title="上一頁(yè)" class="nav"><span>上一頁(yè)</span></a><#else><span>首頁(yè)</span><span>上一頁(yè)</span></#if><#list pageView.pageindex.startindex..pageView.pageindex.endindex as index><#if pageView.currentpage == index><a href="#" class="current">${index}</a><#else><a href="javascript:pageinationView(${index})" title="第${index}頁(yè)" >${index}</a></#if></#list><#if pageView.currentpage != pageView.totalpage><a href="javascript:pageinationView(${pageView.currentpage + 1})" title="下一頁(yè)" class="nav"><span>下一頁(yè)</span></a><a href="javascript:pageinationView(${pageView.totalpage})" title="未頁(yè)" class="nav"><span>未頁(yè)</span></a><#else><span>下一頁(yè)</span><span>未頁(yè)</span></#if></div><script type="text/javascript">function pageinationView(pageNum) {document.getElementById("pageNum").value=pageNum;document.getElementById("pageinationForm").submit();}</script></#macro>
頁(yè)面的引用
<!--分頁(yè) --><form id="pageinationForm" method="post" action="clazz-list"><input type="hidden" id="pageNum" name="pageNum" value="${pageNum}" /></form><#import "*/main/main-page.ftl" as pager><@pager.pagination pageView=pageView/><!-- 分頁(yè) -->
現(xiàn)在應(yīng)該出來(lái)了基本的分頁(yè)。等后續(xù)的工作完成再記錄!
聯(lián)系客服