首页 / 美食 / 正文

分页样式(实现分页的最简单的方式(附带源码下载))

放大字体  缩小字体 来源:英国旅行社 2026-04-17 17:36  浏览次数:11

文章概略:

  1. 分层实现

  2. 我们先来说下原理吧,分页就是数据库的数据太多了,一次性查看不完,分批次查看。一般分页分为从数据库处理分页和在服务器端处理分页两大类。

    分页无非就是服务器返回“总页数”和“当前页数”,然后客户端(一般是浏览器)对应处理。我们分层来写。

    看下预览图(假设每个页面显示10个页数):实现分页的最简单的方式(附带源码下载)nerror="javascript:errorimg.call(this);">css代码:用作美化,你可以改成你喜欢的样式

    width: 80%;

    text-align: center;

    font-size: 1.2em;

    }

    #paging a{

    font-size: 1.2em;

    padding:5px 10px;

    }

    background-color: #FF464E;

    }

    html代码:

    备注:我是有的是java编写的,用了ssm框架,而且url是RESTful风格的,你可以改成普通风格的,代码注释很详细,我在这不解释了。

    普通风格:http://localhost:8080/youxuan/index?page=2

    总页数小于10页时(共6页),全部显示,访问第1页如下:

    总页数大于10,当前页数小于10时,显示前10页,访问第2页如下:

    总页数大于10,当前页数大于10时,我们让他显示左右各5页,但是考虑增加5页可能超出总页数。我们增加判断以后结果如下:

    实现分页的最简单的方式(附带源码下载)nerror="javascript:errorimg.call(this);">

    <script>

    分页:接收参数:总页数,当前页数

    可能的情况:

    全部显示

    当前页小于10页,显示前10页

    当前页数+5页大于总页数,显示到最后一页

    */

    var url = path+"/index";//a标签的地址

    var nowPage=${Paging.nowPage};//当前页

    var count=10;//页面显示多少记录,默认每页显示10个记录

    if(nowPage>1){

    }

    if(countPage<=count){

    document.writeln(" <a href=\""+url+"/"+i+"\">"+i+"</a>");//采用RESTful格式url

    }else{

    //当前页小于10页,显示前10页

    for (var i=1;i<=count;i++) {

    document.writeln(" <a href=\""+url+"/"+i+"\">"+i+"</a>");//采用RESTful格式url

    //显示省略号和最后一页

    document.writeln("...<a href=\""+url+"/"+countPage+"\">"+countPage+"</a>");

    //当前页数大于等于10页,左右各显示5页

    //当前页+5大于总页数,显示到最后一页,显示前10页

    for (var i = nowPage - 10; i <= countPage; i++) {

    document.writeln(" <a href=\"" + url + "/" + i + "\">" + i + "</a>");//采用RESTful格式url

    } else {

    //当前页+5小于总页数,左右各显示5页

    document.writeln(" <a href=\"" + url + "/" + i + "\">" + i + "</a>");//采用RESTful格式url

    //显示省略号和最后一页

    document.writeln("...<a href=\"" + url + "/" + countPage + "\">" + countPage + "</a>");

    }

    }

    //判断尾页是否显示

    document.writeln(" <a href=\""+url+"/"+countPage+"\">尾页</a>");

    </script>

    最好我们最好加个“跳转到第几页的功能”,如图实现分页的最简单的方式(附带源码下载)nerror="javascript:errorimg.call(this);">实现代码:

    <input type="button" onclick="goPage()" value="转到" style="background-color: #6c6c6c;padding: 6px; color: #F5F5F5;border: 0"/>

    <script>

    function goPage() {

    var type = /^[1-9]+$/;

    if (page.match(re) == null) {

    alert("亲,页数一个正整数哦~");

    //判断是否大于总页数

    alert("亲,总共只有"+countPage+"页呢~");

    window.location.href=url+"/"+page;

    }

    </script>

    最后,我们说一下后台实现,我用的是ssm框架,封装了一个分页的bean,代码如下

    /**

    * 分页工具类

    public class Paging {

    private int countPage;//总页数

    private int pageCount;//每页显示多少数据

    * 构造函数

    * @param pageCount

    * @param nowPage

    public Paging(int countDate,int pageCount,int nowPage){

    this.countDate=countDate;

    this.nowPage=nowPage;

    this.countPage=countDate/pageCount;

    this.countPage=countDate/pageCount+1;

    }

    * 构造函数,默认每页显示100条数据

    * @param nowPage

    public Paging(int countDate,int nowPage){

    this(countDate,100,nowPage);

    public int getCountDate() {

    return countDate;

    public void setCountDate(int countDate) {

    this.countDate = countDate;

    public int getCountPage() {

    return countPage;

    public void setCountPage(int countPage) {

    this.countPage = countPage;

    public int getNowPage() {

    return nowPage;

    public void setNowPage(int nowPage) {

    this.nowPage = nowPage;

    public int getPageCount() {

    return pageCount;

    public void setPageCount(int pageCount) {

    this.pageCount = pageCount;

    }

    然后再servlet(controller)中返回这个封装的对象。你可以不用封装也行,我觉的这样可移植性好,下次做项目直接拿来用了。这个项目我做完会开源贡献的,喜欢的点个赞吧。

    @两毛五哥哥,90逗逼程序员,欢迎骚扰

打赏
0相关评论
热门搜索排行
精彩图片
友情链接
声明:本站信息均由用户注册后自行发布,本站不承担任何法律责任。如有侵权请告知立立即做删除处理。
违法不良信息举报邮箱:115904045
头条快讯网 版权所有
中国互联网举报中心