zoukankan      html  css  js  c++  java
  • 分页功能实现笔记

    分页后台代码实现笔记

    1.新建一个Page实体类
    (1)当前页
    private int currentPage
    (2)当前显示的条数
    private int currentCount;
    (3)总条数
    private int totalCount;
    (4)总页数
    private int totalPage
    (5)每页显示的数据
    private List<T> list=new ArrayList<T>();

    2.编写方法web层
    //获取当前页
    String currentPageStr=request.getParameter("currentPage");
    if(currentPageStr==null){
    currentPageStr="1";
    }
    Integer currentPage=Integer.parseInt(currentPageStr);
    //认为每页显示12条
    int curryCount=12;

    PageBean pageBean=null;
    pageBean=service.findPageBean(currentPage,curryCount);


    request.setAttribute("pageBean",pageBean);


    (1)分页操作service层(目的封装一个PageBean,并返回)
    public PageBean findPageBean(int currentPage,int curryCount){
    Dao dao=new Dao();

    PageBean pageBean=new PageBean();

    //显示当前页
    pageBean.setCurrentPage(currentPage);
    //当前页现实的条数
    pageBean.setCurrentCount(currentCount);
    //总条数
    int totalCount=dao.getTotalCount();
    pageBean.setTotalCount(totalCount);
    //总页数
    /*
    *总条数       当前页显示的条数      总页数
    * 10                         4                        3
    * 11                         4                        3
    * 12                         4                        3
    * 13                         4                        4
    *
    *公式:总页数=Math.ceil(1.0*总条数/当前页显示的条数)
    */
    int totalPage=(int)Math.ceil(1.0*totalCount/currentCount);
    pageBean.setTotalPage(totalPage);
    //每页显示的数据
    /*
    *页数与limit起始索引的关系
    *每页显示4条
    *页数            起始索引          每页显示条数
    * 1                      0                           4
    * 2                      4                           4
    * 3                      8                           4
    * 4                     12                          4
    *
    *索引index = (当前页数-1)*每页显示的条数
    */
    int index=(currentPage-1)*currentCount;

    List<T> list=dao.findListForPageBean(index,currentCount);
    pageBean.setList(list);
    return pageBean;


    }
    (2)Dao层
    public int totalCount(){
    //数据库查询总条数count函数
    String sql="select count(*) from table";
    }

    public List<T> findListForPageBean(int index,int currentCount){
    String sql="select * from table limit ?,?";
    }

    前台分页实现笔记

    分页前台实现笔记
    <c:forEach items="${pageBean.list}">

    </c:forEach>
    <!-- 分页 -->
    <c:forEach begin="1" end="${pageBean.totalPage}" var="page">


    <!-- 当前页是否是第一页 -->
    <c:if test="${pageBean.currentPage==1}">
    <上一页不能点>
    </c:if>
    <c:if test="${pageBean.currentPage!=1}">
    <a href="${pageContext.request.contextPath}/Servlet?currentPage=${pageBean.currentPage-1}}">
    </c:if>


    <!-- 判断当前页 -->
    <c:if test="${pageBean.currentPage==page}">
    <li class="active"><a href="javascript:void(0);">${page}</a></li>
    </c:if>
    <c:if test="${pageBean.currentPage!=page}">
    <li ><a href="${pageContext.request.contextPath}/Servlet?currentPage=${page}">${page}</a></li>
    </c:if>


    <!-- 当前页是否是最后一页 -->
    <c:if test="${pageBean.currentPage==pageBean.totalPage}">
    <下一页不能点>
    </c:if>
    <c:if test="${pageBean.currentPage!=pageBean.totalPage}">
    <a href="${pageContext.request.contextPath}/Servlet?currentPage=${pageBean.currentPage+1}}">
    </c:if>


    </c:forEach>

    页码颜色问题:

    在当前页面时的页码为蓝色,其他页码为无色,选择其他页面时,原页码变为无色,选择的页码变为蓝色

    html页码的代码:(当前页时第一页)

    <ul class="pagination">

    <li class="active"><a>1</a></li>

    <li><a>2</a></li>

    <li><a>3</a></li>

    <li><a>4</a></li>

    </ul>

    css代码:

    .pagination li.active a{ color:blue }

  • 相关阅读:
    qml----动画入门(六、简单的动画实现 SpringAnimation类)
    qml----动画入门(五、简单的动画实现 SmoothedAnimation类)
    qml----动画入门(四、简单的动画实现 PathAnimation类)
    qml----动画入门(三、简单的动画实现 RotationAnimation类)
    SSRS连接ORACLE数据库制作报表
    JavaScript Number 对象
    Django--filter()-字段查找(双下划线的使用详解)
    Django--filter(**kwargs)
    Django--QuerySet--基础查询
    python 内置函数
  • 原文地址:https://www.cnblogs.com/FanJava/p/8058740.html
Copyright © 2011-2022 走看看