分页效果

微信截图_20191231163003.png

java:

    @At
    @Ok("beetl:/news_list.html")
    public Context list(@Param("p")int p) {
        if (p < 1)
            p = 1;
        QueryResult qr = getPetList(dao, p, 4);
        Context context = Lang.context();
        context.set("pageList", qr);
        return context;
    }

    public QueryResult getPetList(Dao dao, int pageNumber, int pageSize) {
        Pager pager = dao.createPager(pageNumber, pageSize);
        List<News> list = dao.query(News.class, null, pager);
        pager.setRecordCount(dao.count(News.class));
        return new QueryResult(list, pager);
    }

html:

    <%
        print(pageList.pager);
        
        var URL = "/news/list?p=";
        var pageNumber = pageList.pager.pageNumber;
        var pageCount = pageList.pager.pageCount;
        var startPage = 1;
        var endPage = 1;
        
        startPage = pageNumber - 4;
        if (startPage < 1){
            startPage = 1;
        };

       endPage = pageNumber + 4;
       if (endPage > pageCount) {
           endPage = pageCount;
       };
       print('<nav>');
       print('<ul class="pagination">');
       if (pageNumber <= 8){
            startPage = 1;
       }
       if ((pageCount - pageNumber) <8){
            endPage = pageCount;
       }
       
       if(pageNumber == 1){ 
            print('<li class="page-item disabled"><span class="page-link">上一页</span></li>');
       }else{
               print('<li class="page-item"><a class="page-link" href="'+ URL+(pageNumber-1) +'" >上一页</a></li>');
       }
       
       if(pageNumber >8 ){ 
               print('<li class="page-item"><a class="page-link" href="'+ URL+1 +'">1</a></li>');
               print('<li class="page-item"><a class="page-link">…</span></li>');
       }
       
       for(var i=startPage;i<=endPage;i++){
               if(pageNumber == i){
                   print('<li class="page-item active"><a class="page-link" href="'+ URL+i +'">'+ i +'</a></li>');
               }else{
                   print('<li class="page-item"><a class="page-link" href="'+ URL+i +'">'+ i +'</a></li>');
               }
       }
       
       if((pageCount-pageNumber)>=8 ){ 
               print('<li class="page-item"><a class="page-link">…</span></li>');
               print('<li class="page-item"><a class="page-link" href="'+ URL+(pageCount) +'">'+ pageCount +'</a></li>');
       }
       
       if(pageNumber == pageCount){ 
            print('<li class="page-item disabled"><span class="page-link">下页</span></li>');
       }else{
               print('<li class="page-item"><a class="page-link" href="'+ URL+(pageNumber+1) +'" >下页</a></li>');
       }
       print('</ul>');
       print('</nav>');
    %>
    

样式

<link rel="stylesheet" href="http://lib.sinaapp.com/js/bootstrap/4.3.1/css/bootstrap.min.css">

打完收枪


fangjunai
5 声望0 粉丝