论坛首页 入门技术论坛

完美封装分页

浏览 10420 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (9) :: 隐藏帖 (0)
作者 正文
   发表时间:2011-04-28  
public class Pagination {
/**
     * 当前页
     */
    private int curPage;
    /**
     * 每页显示的记录数
     */
    private int pageSize;
    /**
     * 记录总行数
     */
    private int rowsCount;
    /**
     * 总页数
     */
    private int pageCount;
   
    /**
     *
     * @param totalSize 总记录数
     * @param curPage 当前的页码数
     * @param pageSize 每页记录数
     *
     */
    public Pagination(int totalSize, int curPage, int pageSize) {
        this.curPage = curPage;
        this.pageSize = pageSize;
        this.rowsCount = totalSize;
        this.pageCount = (int) Math.ceil((double) rowsCount / pageSize);
    }

public int getCurPage() {
return curPage;
}


public int getPageCount() {
return pageCount;
}

public int getPageSize() {
return pageSize;
}

public int getRowsCount() {
return rowsCount;
}
//判断是否首页
public boolean isFirst() {
        return (curPage==1)?true:false;
    }
//判断是否尾页
public boolean isLast() {
        return (curPage==pageCount)?true:false;
    }
/**
     * 获取工具条
     * @return 返回用于翻页、跳转、控制页面大小的工具条
     */
    public String getToolBar()
    {
        String str="<script>function changePageValue(cur_page) {if(document.all.ischange!=null){if(document.all.ischange.value=='1'){document.all.isFirstQuery.value='0';}else{document.all.isFirstQuery.value='1';}}else{document.all.isFirstQuery.value='1';} var curPage=document.getElementById('cur_page');"
        +"curPage.value=cur_page;var form=document.forms[0];var a=form.submit();if(a==false){return false;}form.onsubmit();}function mySubmit(){goto_Page(); if(document.all.ischange!=null){if(document.all.ischange.value=='1'){document.all.isFirstQuery.value='0';}else{document.all.isFirstQuery.value='1';}}else{document.all.isFirstQuery.value='1';}  var form = document.forms[0];form.submit();}"
        +"function goto_Page(){document.getElementById('cur_page').value=document.forms[0].txtPage.value;}</script>";
        str+="<input type='hidden' name='cur_page' id='cur_page' value='"+curPage+"'>";
        str+="<input type='hidden' name='pageCount' id='pageCount' value='"+pageCount+"'>";
        str+="<input type='hidden' name='rowsCount' id='rowsCount' value='"+rowsCount+"'>";
        str+="<input type='hidden' name='isFirstQuery' id='isFirstQuery'>";
    HttpServletRequest request = ServletActionContext.getRequest();
String root = request.getContextPath();
        if(isFirst())
            str+="<img src='"+root+"/images/pg-first.gif' alt='首页' border='0'>&nbsp;<img src='"+root+"/images/pg-prev.gif' alt='上一页' border='0'>&nbsp;";
        else
        {
            str+="<a style='cursor:hand;FONT-SIZE: 12px; COLOR: #D80000;' onclick='javascript:changePageValue(1)'><img src='"+root+"/images/pg-first.gif' alt='首页' border='0'></a>&nbsp;";
            str+="<a style='cursor:hand;FONT-SIZE: 12px; COLOR: #D80000;' onclick='javascript:changePageValue("+(curPage-1)+")'><img src='"+root+"/images/pg-prev.gif' alt='上一页' border='0'></a>&nbsp;";
        }
        if(isLast())
            str+="<img src='"+root+"/images/pg-next.gif' alt='下一页' border='0'>&nbsp;<img src='"+root+"/images/pg-last.gif' alt='尾页' border='0'>&nbsp;";
        else
        {
            str+="<a style='cursor:hand;FONT-SIZE: 12px; COLOR: #D80000;' onclick='javascript:changePageValue("+(curPage+1)+")'><img src='"+root+"/images/pg-next.gif' alt='下一页' border='0'></a>&nbsp;";
            str+="<a style='cursor:hand;FONT-SIZE: 12px; COLOR: #D80000;' onclick='javascript:changePageValue("+pageCount+")'><img src='"+root+"/images/pg-last.gif' alt='末页' border='0'></a>&nbsp;";
        }
        str+="&nbsp;共 <b>"+rowsCount+"</b> 条记录&nbsp;";
        if (curPage > pageCount){
        curPage = pageCount;
        }
        str+="&nbsp;[当前第"+this.curPage+"页]/[共 <b>"+pageCount+"</b> 页]&nbsp;";
        str+="&nbsp;跳转到第 <input type='text'  class='button01' onfocus='this.focus();this.select();' id='txtPage' value='"+curPage+"' size='2' name='txtPage'> 页 ";
        str+=" <input type='button' class='input2' title='跳转' style='background-image:url("+root+"/images/go.gif);width:18px;border:0px;cursor:pointer;height:18px;' name='ok' onclick='changePageValue(document.forms[0].txtPage.value)'>";
        return str;
    }
    public String toString()
    {
    String str = "";
    str = "该类是用于分页的工具类";
    return str;
    }
}
   发表时间:2011-04-28  
orderBy 和 orderByType 这二个属性加上如何?这样就具有哪个字段按升序还是降序排列的功能了。。
0 请登录后投票
   发表时间:2011-04-29  
用javascript方式做分页,有个问题,每个页面的地址都是一样的,这样对于seo优化,会有不好的影响。

所以,所谓的完美,还是针对于技术来说了。
0 请登录后投票
   发表时间:2011-04-29   最后修改:2011-04-29
搞什么东东。
getToolBar()里还有js,还有html。
函数职责不全,还有乱七八糟的逻辑。
天天念叨模型数据分层,视图和数据是怎么分的。
0 请登录后投票
   发表时间:2011-04-29  
getToolBar()
这里面的一坨东西让人很郁闷
0 请登录后投票
   发表时间:2011-04-29  
问一句: 你的查询参数怎么获取?

还在用 document.all. 你测过其他浏览器没有?

就这还完美?

有想过弄成 itEye 这样的分页没有?
0 请登录后投票
   发表时间:2011-04-29  
看到那个getToolBar() 就晕了

竟然混入了HTML JS
竟然用+来拼接字符串
0 请登录后投票
   发表时间:2011-04-29  
完美?。天啊
0 请登录后投票
   发表时间:2011-04-29  
没有所谓的完美。
LZ这个只能说是你的一种实现方法。
这方法可以说有点笨拙的感觉
0 请登录后投票
   发表时间:2011-04-29   最后修改:2011-04-29
让我想起了五年前的做的分页....但至少要把js,html分开吧
0 请登录后投票
论坛首页 入门技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics