jquery+Ajax实现简单分页条效果

2022-04-15 0 739

本文实例为大家分享了jquery+Ajax实现分页条效果的具体代码,供大家参考,具体内容如下

一、如果是jsp页面的话,可以用EL表达式和JSTL标签制作一个分页条,没有什么难度。用EL表达式和JSTL标签实现的缺点就是无法实现异步效果,整个页面是重新刷新了一遍的。

二、如果是普通的html页面,当然是无法使用EL表达式和JSTL标签的,这时只能通过异步Ajax的方式去实现。当然了,JSP页面两种方式都是可以使用的。

三、分页条,这里我是用Ajax和Jquery去做的。实现起来比较繁琐,代码特别长,因为都是拼接一大堆的字符串,然后使用html()方法或是append()方法去改变文档的内容。

四、事前分析

浏览器端需要发送给服务器端的参数有两个:

①当前的页码currentPage;
②页面的大小(一页显示几条记录)pageSize。

服务器端给浏览器端发送的是Json格式的数据,也就是一个页面实体类PageBean。其中PageBean有如下字段:

①总记录数totalCount;
②总页码totalPage;
③每页的数据 List list;
④当前页码currentPage;
⑤每页显示的记录数pageSize。

这个PageBean支持泛型,代码如下:

public class PageBean<T> 
{
    private int totalCount; // 总记录数
    private int totalPage ; // 总页码
    private List<T> list ; // 每页的数据
    private int currentPage ; //当前页码
    private int rows;//每页显示的记录数,也就是pageSize

    public int getTotalCount() 
    {
        return totalCount;
    }

    public void setTotalCount(int totalCount)
    {
        this.totalCount = totalCount;
    }

    public int getTotalPage() 
    {
        return totalPage;
    }

    public void setTotalPage(int totalPage) 
    {
        this.totalPage = totalPage;
    }

    public List<T> getList() 
    {
        return list;
    }

    public void setList(List<T> list) 
    {
        this.list = list;
    }

    public int getCurrentPage() 
    {
        return currentPage;
    }

    public void setCurrentPage(int currentPage) 
    {
        this.currentPage = currentPage;
    }

    public int getRows() 
    {
        return rows;
    }

    public void setRows(int rows) {
        this.rows = rows;
    }

    @Override
    public String toString() 
    {
        return "PageBean{" +
                "totalCount=" + totalCount +
                ", totalPage=" + totalPage +
                ", list=" + list +
                ", currentPage=" + currentPage +
                ", rows=" + rows +
                '}';
    }
}

要想做到分页,肯定要用到SQL语句中的“limit”。举个例子说明一下含义。

select * from student limit 2,5;

具体含义:从student表当中查询数据,从索引为“2”的记录开始查询,往后查5条。

索引是从0开始的,所以上面的语句相当于查询了第3、第4、第5、第6、第7条记录,总共5条记录。

总而言之,第一个数字就是“从哪开始查”的意思,第二个数字就是“往后查几条”的意思。

这里的“从哪开始查”,需要计算出来。公式如下:

(currentPage-1)×pageSize

也就是当前页码减去一,括号,在乘以页面大小。

所以查询语句的伪代码如下:

select * from student limit (currentPage-1)×pageSize,pageSize;

对于总页码totalPage,可以通过总记录数totalCount和页面大小pageSize计算出来。代码如下:

totalPage=totalCount%pageSize==0?totalCount/pageSize:(totalCount/pageSize+1);

五、服务器端主要代码

import com.fasterxml.jackson.databind.ObjectMapper;
import domain.PageBean;
import domain.RainFall;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import util.JDBCUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/ViewRainByPageServlet")
public class ViewRainByPageServlet extends HttpServlet
{
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        JdbcTemplate template=new JdbcTemplate(JDBCUtils.getDataSource());
        String sql="select * from rain_fall limit ?,?";//查询部分元组
        String sql2="select * from rain_fall";//查询所有元组
        List<RainFall> countList = template.query(sql2, new BeanPropertyRowMapper<RainFall>(RainFall.class));
        int totalCount=countList.size();//从数据库获取总记录数
        int totalPage;//先声明一下总的页码,总的页码需要根据客户端发送过来的数据进行计算

        String currentPage = request.getParameter("currentPage");
        String pageSize = request.getParameter("pageSize");
        int intCurrentPage = Integer.parseInt(currentPage);//用户发来的当前页码
        if(intCurrentPage==0)//用户点击上一页按钮,currentPage就减1,会出现减到0的情况
        {
            intCurrentPage=1;//如果用户的currentPage=0,直接把页码设为1,把第一页的数据返回给客户端
        }

        int intPageSize = Integer.parseInt(pageSize);//用户发来的页面大小
        totalPage=totalCount%intPageSize==0?totalCount/intPageSize:(totalCount/intPageSize+1);//计算出总的页数
        if(intCurrentPage>totalPage)//用户点击下一页按钮,currentPage就加1,会出现大于总页数的情况
        {
            intCurrentPage=totalPage;//把当前页码设为总页数
        }

        int startIndex=(intCurrentPage-1)*intPageSize;//从索引为几的记录开始查询?

        List<RainFall> list = template.query(sql, new BeanPropertyRowMapper<RainFall>(RainFall.class),startIndex,intPageSize);
        ObjectMapper mapper=new ObjectMapper();
        response.setContentType("application/json;charset=utf-8");//设置响应数据类型和字符编码
        PageBean<RainFall> pageBean=new PageBean<>();//创建PageBean对象
        //封装PageBean对象
        pageBean.setTotalCount(totalCount);
        pageBean.setTotalPage(totalPage);
        pageBean.setList(list);
        pageBean.setCurrentPage(intCurrentPage);
        pageBean.setRows(intPageSize);
        //将数据写回客户端
        System.out.println(pageBean);
        mapper.writeValue(response.getOutputStream(),pageBean);

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {

        this.doPost(request, response);

    }
}

六、前端代码(很长)

<%--
  Created by Yingyong Lao.
  User: laoyingyong
  Date: 2019-12-10
  Time: 19:28
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>查看降雨信息</title>
    <!-- 1. 导入CSS的全局样式 -->
    <link href="css/bootstrap-3.3.7-dist/css/bootstrap.min.css" rel="external nofollow"  rel="stylesheet">
    <!-- 2. jQuery导入,建议使用1.9以上的版本 -->
    <script src="js/jquery-2.1.0.min.js"></script>
    <!-- 3. 导入bootstrap的js文件 -->
    <script src="js/bootstrap.min.js"></script>
    <script>
        $(function () //入口函数
        {
            $.post("ViewRainByPageServlet",{currentPage:1,pageSize:5},function (data)//页面加载完成之后发送ajax请求,请求前5条记录,完成界面的初始化
            {
               var totalCount=data.totalCount;//总记录数
               var totalPage=data.totalPage;//总页数
               var currentPage=data.currentPage;//当前页码

                if(currentPage==1)//如果当前页码为1,用户还点击上一页的话,设置class="disabled" ,即出现一个“禁用”图标
                {
                    var str=' <li class="disabled" οnclick="findByPage('+(currentPage-1)+',5)">\n' +
                        '                    <a href="#" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  aria-label="Previous">\n' +
                        '                        <span aria-hidden="true">&laquo;</span>\n' +
                        '                    </a>\n' +
                        '                </li>';

                }
                else//否则上一页的按钮就是正常的样式
                {
                    var str=' <li οnclick="findByPage('+(currentPage-1)+',5)">\n' +
                        '                    <a href="#" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  aria-label="Previous">\n' +
                        '                        <span aria-hidden="true">&laquo;</span>\n' +
                        '                    </a>\n' +
                        '                </li>';

                }

                for(var i=1;i<=totalPage;i++)//遍历页码,这是两个图标(上一页和下一页)之间的数字
                {
                    if(i==currentPage)//如果当前的这个数字等于当前页码currentPage,就设置class="active",即页码呈高亮样式
                    {
                        var item='<li οnclick="findByPage('+i+',5);" class="active"><a href="#" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >'+i+'</a></li>';
                    }
                    else
                    {
                        var item='<li οnclick="findByPage('+i+',5);"><a href="#" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >'+i+'</a></li>';
                    }
                    str=str+item;

                }
                if(currentPage==totalPage)//如果当前页码为最后一页,用户还点击下一页的话,设置class="disabled" ,即出现一个“禁用”图标
                {
                    var strend='<li class="disabled" οnclick="findByPage('+(currentPage+1)+',5)">\n' +
                        '                    <a href="#" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  aria-label="Next">\n' +
                        '                        <span aria-hidden="true">&raquo;</span>\n' +
                        '                    </a>\n' +
                        '                </li>\n' +
                        '                <span style="font-size: 24px" id="total_sp">共'+totalCount+'条记录,共'+totalPage+'页</span>';

                }
                else //不是最后一页,就是正常的样式
                {
                    var strend='<li οnclick="findByPage('+(currentPage+1)+',5)">\n' +
                        '                    <a href="#" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  aria-label="Next">\n' +
                        '                        <span aria-hidden="true">&raquo;</span>\n' +
                        '                    </a>\n' +
                        '                </li>\n' +
                        '                <span style="font-size: 24px" id="total_sp">共'+totalCount+'条记录,共'+totalPage+'页</span>';

                }

                str=str+strend;
                $("#fenyelan").html(str);//分页条初始化

                var array=data.list;
                for(var i=0;i<array.length;i++)
                {
                    var obj=array[i];
                    var id=obj.id;
                    var area=obj.area;
                    var precipitation=obj.precipitation;
                    var month=obj.month;
                    var releaseDate=obj.releaseDate;
                    //表格的初始化
                    $("#rain_table").append('<tr class="info">\n' +
                        '                <td style="text-align: center">'+id+'</td>\n' +
                        '                <td style="text-align: center">'+area+'</td>\n' +
                        '                <td style="text-align: center">'+precipitation+'</td>\n' +
                        '                <td style="text-align: center">'+month+'</td>\n' +
                        '                <td style="text-align: center">'+releaseDate+'</td>\n' +
                        '            </tr>');
                }

            });//页面加载完成之后发送ajax请求end

        });//入口函数end



        //页面按钮的点击回调函数,不需要写在入口函数里面,因为只有页面按钮被点击时,这个函数才会被调用
        function findByPage(curPage,paSize) //被调用的时候,需要传递两个参数:当前页码和页码的大小(一页有几条记录)
        {
            $.post("ViewRainByPageServlet",{currentPage:curPage,pageSize:paSize},function (data) //发送ajax请求
            {
                var totalCount=data.totalCount;//总记录数
                var totalPage=data.totalPage;//总页数
                var currentPage=data.currentPage;//当前页码


                if(currentPage==1)//如果当前页码为1,用户还点击上一页的话,设置class="disabled" ,即出现一个“禁用”图标
                {
                    var str=' <li class="disabled" οnclick="findByPage('+(currentPage-1)+',5)">\n' +
                        '                    <a href="#" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  aria-label="Previous">\n' +
                        '                        <span aria-hidden="true">&laquo;</span>\n' +
                        '                    </a>\n' +
                        '                </li>';

                }
                else//不为第一页,上一页按钮就是正常的样式
                {
                    var str=' <li οnclick="findByPage('+(currentPage-1)+',5)">\n' +
                        '                    <a href="#" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  aria-label="Previous">\n' +
                        '                        <span aria-hidden="true">&laquo;</span>\n' +
                        '                    </a>\n' +
                        '                </li>';

                }


                //分页条中间数字部分
                for(var i=1;i<=totalPage;i++)
                {
                    if(i==currentPage)//如果当前的这个数字等于当前页码currentPage,就设置class="active",即页码呈高亮样式
                    {

                        var item='<li οnclick="findByPage('+i+',5);" class="active"><a href="#" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >'+i+'</a></li>';
                    }
                    else
                    {
                        var item='<li οnclick="findByPage('+i+',5);"><a href="#" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >'+i+'</a></li>';
                    }


                    str=str+item;

                }
                if(currentPage==totalPage)//如果当前页码为最后一页,用户还点击下一页的话,设置class="disabled" ,即出现一个“禁用”图标
                {
                    var strend='<li class="disabled" οnclick="findByPage('+(currentPage+1)+',5)">\n' +
                        '                    <a href="#" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  aria-label="Next">\n' +
                        '                        <span aria-hidden="true">&raquo;</span>\n' +
                        '                    </a>\n' +
                        '                </li>\n' +
                        '                <span style="font-size: 24px" id="total_sp">共'+totalCount+'条记录,共'+totalPage+'页</span>';

                }
                else //不是最后一页,就是正常的样式
                {
                    var strend='<li οnclick="findByPage('+(currentPage+1)+',5)">\n' +
                        '                    <a href="#" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  aria-label="Next">\n' +
                        '                        <span aria-hidden="true">&raquo;</span>\n' +
                        '                    </a>\n' +
                        '                </li>\n' +
                        '                <span style="font-size: 24px" id="total_sp">共'+totalCount+'条记录,共'+totalPage+'页</span>';

                }
                str=str+strend;
                $("#fenyelan").html(str);//改变分页条的内容

                //表格的字符串
                var tableStr='<caption style="text-align: center;font-size: 24px">降雨信息一览</caption>\n' +
                    '            <tr class="success">\n' +
                    '                <th style="text-align: center">id</th>\n' +
                    '                <th style="text-align: center">地区</th>\n' +
                    '                <th style="text-align: center">降雨量(mm)</th>\n' +
                    '                <th style="text-align: center">月份</th>\n' +
                    '                <th style="text-align: center">发布日期</th>\n' +
                    '            </tr>';
                var array=data.list;//具体内容的对象数组
                for(var i=0;i<array.length;i++)//遍历数对象组
                {
                    var obj=array[i];//数组的一个元素就是一个对象
                    var id=obj.id;
                    var area=obj.area;
                    var precipitation=obj.precipitation;
                    var month=obj.month;
                    var releaseDate=obj.releaseDate;
                    //一行记录的字符串
                    var oneRecord='<tr class="info">\n' +
                        '                <td style="text-align: center">'+id+'</td>\n' +
                        '                <td style="text-align: center">'+area+'</td>\n' +
                        '                <td style="text-align: center">'+precipitation+'</td>\n' +
                        '                <td style="text-align: center">'+month+'</td>\n' +
                        '                <td style="text-align: center">'+releaseDate+'</td>\n' +
                        '            </tr>';

                    tableStr=tableStr+oneRecord;//表格字符串的追加,每遍历一条记录,就会追加一行
                }
                $("#rain_table").html(tableStr);//改变表格里面的内容

            });//ajax请求end

        }//页面按钮的点击函数end

    </script>
</head>
<body>
<div class="container">
    <div class="row">

        <table class="table table-bordered table-hover" id="rain_table">
            <caption style="text-align: center;font-size: 24px">降雨信息一览</caption>
            <tr class="success">
                <th style="text-align: center">id</th>
                <th style="text-align: center">地区</th>
                <th style="text-align: center">降雨量(mm)</th>
                <th style="text-align: center">月份</th>
                <th style="text-align: center">发布日期</th>
            </tr>
        </table>


        <nav aria-label="Page navigation">
            <ul class="pagination" id="fenyelan">
                <li>
                    <a href="#" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  aria-label="Previous">
                        <span aria-hidden="true">&laquo;</span>
                    </a>
                </li>
                <li><a href="#" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >1</a></li>
                <li>
                    <a href="#" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  aria-label="Next">
                        <span aria-hidden="true">&raquo;</span>
                    </a>
                </li>
                <span style="font-size: 24px" id="total_sp">共2条记录,共1页</span>
            </ul>
        </nav>



    </div>

</div>

</body>
</html>

七、效果展示

jquery+Ajax实现简单分页条效果

这只是一个简单的分页条,没有百度分页条“前五后四”的效果。当数据量变得非常庞大时,这个分页条就会占据很大的空间。有时间的话再优化一下吧。至于Jquery代码,代码的注释里已经写得够清楚了,这里就不再做过多的解释。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持NICE源码。

免责声明:
1、本网站所有发布的源码、软件和资料均为收集各大资源网站整理而来;仅限用于学习和研究目的,您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容。 不得使用于非法商业用途,不得违反国家法律。否则后果自负!

2、本站信息来自网络,版权争议与本站无关。一切关于该资源商业行为与www.niceym.com无关。
如果您喜欢该程序,请支持正版源码、软件,购买注册,得到更好的正版服务。
如有侵犯你版权的,请邮件与我们联系处理(邮箱:skknet@qq.com),本站将立即改正。

NICE源码网 JavaScript jquery+Ajax实现简单分页条效果 https://www.niceym.com/28786.html