过年在家,玩到提不起兴致写代码,更别说写博客了……罪过罪过……

于是今天把自己折腾的play!项目的分页整理完善了一下,做个简单记录,免得博客长草~

需求很简单,如下图所示:

分页样式

显示5个页码,其他的用省略号表示

代码如下:

@defining((pageNavMap("currentPageNum"), pageNavMap("totalPageNum"))) { case (page_curPageNum, page_totalPageNum) =>
<div class="page-navigation">
    @if(page_curPageNum > 1){
    <a href="/person/@userprofile.id">首页</a>
    <a href='/person/@userprofile.id/p/@(page_curPageNum-1)'>上一页</a>
    }
    @if(page_totalPageNum < 6){
    @for(i <- 1 to page_totalPageNum){
        @if(i == page_curPageNum){
        <span class="curPage">@page_curPageNum</span>
        }else{
        <a href='/person/@userprofile.id/p/@i'>@i</a>
        }
    }
    }else{
    @if(page_curPageNum < 4){
        @for(i <- 1 to 5){
        @if(i == page_curPageNum){
        <span class="curPage">@page_curPageNum</span>
        }else{
        <a href='/person/@userprofile.id/p/@i'>@i</a>
        }
        }
        <span class="pagebreak">…</span>
    }else{
    @if(page_curPageNum > (page_totalPageNum-2)){
    <span class="pagebreak">…</span>
    <a href='/person/@userprofile.id/p/@(page_curPageNum-3)'>@(page_curPageNum-3)</a>
    <a href='/person/@userprofile.id/p/@(page_curPageNum-2)'>@(page_curPageNum-2)</a>
    <a href='/person/@userprofile.id/p/@(page_curPageNum-1)'>@(page_curPageNum-1)</a>
    <span class="curPage">@page_curPageNum</span>
    @if(page_curPageNum != page_totalPageNum){
    <a href='/person/@userprofile.id/p/@(page_totalPageNum)'>@(page_totalPageNum)</a>
    }
    }else{
    <span class="pagebreak">…</span>
    <a href='/person/@userprofile.id/p/@(page_curPageNum-2)'>@(page_curPageNum-2)</a>
    <a href='/person/@userprofile.id/p/@(page_curPageNum-1)'>@(page_curPageNum-1)</a>
    <span class="curPage">@page_curPageNum</span>
    <a href='/person/@userprofile.id/p/@(page_curPageNum+1)'>@(page_curPageNum+1)</a>
    <a  href='/person/@userprofile.id/p/@(page_curPageNum+2)'>@(page_curPageNum+2)</a>
    <span class="pagebreak">…</span>
    }
}
    }
    
@if(page_curPageNum < page_totalPageNum){
    <a href='/person/@userprofile.id/p/@(page_curPageNum+1)'>下一页</a>
    <a href='/person/@userprofile.id/p/@page_totalPageNum'>末页</a>
    }

</div>
}

其中,“pageNavMap”是保存分页信息的Map,后台定义如下:

Map pageMap = new HashMap();
pageNavMap.put("totalPageNum", totalPageCount);
pageNavMap.put("currentPageNum", curPageNum>1?curPageNum:1);

这只是用于页面显示的代码,至于后台分页的代码,可以很简单的通过play!内的EBean自带的“ PagingList”接口搞定

PagingList的api地址:http://www.avaje.org/static/javadoc/pub/com/avaje/ebean/PagingList.html