前后端结合实现amazeUI分页效果


Posted in HTML / CSS onAugust 21, 2020

前后端结合实现amazeUI分页,代码如下所示;

借鉴

本文在博客https://blog.csdn.net/brave_coder/article/details/52367124的基础上实现的,非常感谢大佬的分享。

前端实现

1、引入paginator.js

(function ($) {
    $.fn.paginator = function (options) {
        //this指向当前的选择器
        var config = {
            url: "",
            pageParent: "",
            totalBars: -1,
            limit: -1,
            offset: 1,
            callback: null
        }
        //合并参数
        var opts = $.extend(config, options);
 
        opts.totalBars = Math.ceil(opts.totalBars / opts.limit);
        //计算按钮的总个数
 
        //获取offset参数
        var queryString = function (url) {
            var offset = (url.split("?")[1]).split("=")[1];
            return parseInt(offset);
        }
 
        //ajax核心方法,用于分页的数据操作
        var ajaxCore = function (offset, fn) {
            $.ajax({
                "url": opts.url,
                "data": {
                    "offset": offset,
                    "limit": opts.limit
                },
                "dataType": "JSON",
                "method": "POST",
                "success": fn
            });
        }
 
        //重新装配分页按钮
        var pageCore = function (offset) {
            if (opts.offset == offset) {
                return;
            } //如果是当前页面,那么就什么事都不用干了!
            else {
                ajaxCore(offset, opts.callback);
                $(opts.pageParent).empty();
                //否则,清空所有的节点,重新向DOM插入新的分页按钮
                var output = "";
                var nextBar = offset == opts.totalBars ? "<li class=\"am-disabled\"><a yxhref=\"javascript:;\">»</a></li>" : "<li><a yxhref=\"" + opts.url + (offset + 1) + "\">»</a></li>";
                var preBar = offset == 1 ? "<li class=\"am-disabled\"><a yxhref=\"javascript:;\">«</a></li>" : "<li><a yxhref=\"" + opts.url + (offset - 1) + "\">«</a></li>";
                //组装向上一个节点和下一页节点
                if (opts.totalBars > 7) {
                    if (offset < 5) {
                        output += preBar;
                        for (var i = 1; i <= 5; i++) {
                            if (i == offset) {
                                output += "<li class=\"am-active\"><a yxhref=\"" + opts.url + offset + "\">" + offset + "</a></li>";
                            } else {
                                output += "<li><a yxhref=\"" + opts.url + i + "\">" + i + "</a></li>";
                            }
                        }
                        output += "<li><span>...</span></li>";
                        output += "<li><a yxhref=\"" + opts.url + (opts.totalBars) + "\">" + (opts.totalBars) + "</a></li>" + nextBar;
                    } else if (offset >= 5 && offset <= opts.totalBars - 4) {
                        //当页面大于7个的时候,那么在第五个和倒数第五个时,执行
                        output += preBar;
                        output += "<li><a yxhref=\"" + opts.url + 1 + "\">" + 1 + "</a></li>";
                        //第一个
                        output += "<li><span>...</span></li>"; //省略号
 
                        output += "<li><a yxhref=\"" + opts.url + (offset - 1) + "\">" + (offset - 1) + "</a></li>";
 
                        output += "<li class=\"am-active\"><a  yxhref=\"" + opts.url + offset + "\">" + offset + "</a></li>";
 
                        output += "<li><a yxhref=\"" + opts.url + (offset + 1) + "\">" + (offset + 1) + "</a></li>";
 
                        output += "<li><span>...</span></li>"; //省略号;
 
                        output += "<li><a yxhref=\"" + opts.url + (opts.totalBars) + "\">" + (opts.totalBars) + "</a></li>"; //尾页
 
                        output += nextBar;
 
                    } else if (offset > opts.totalBars - 4 && offset <= opts.totalBars) {
                        //当页面位于倒数第四个时候
                        output += preBar;
                        output += "<li><a yxhref=\"" + opts.url + 1 + "\">" + 1 + "</a></li>" + "<li><span>...</span></li>";
 
                        for (var j = 4; j >= 0; j--) {
                            if (opts.totalBars - j == offset) {
                                output += "<li class=\"am-active\"><a yxhref=\"" + opts.url + (opts.totalBars - j) + "\">" + (opts.totalBars - j) + "</a></li>";
                            } else {
                                output += "<li><a yxhref=\"" + opts.url + (opts.totalBars - j) + "\">" + (opts.totalBars - j) + "</a></li>";
                            }
                        }
                        output += nextBar;
                    } else {
                        console.log("分页数据出错!");
                        return;
                    }
                } else {
                    output += preBar;
                    for (var i = 1; i <= opts.totalBars; i++) {
                        if (i == offset) {
                            output += "<li class=\"am-active\"><a yxhref=\"" + opts.url + offset + "\">" + offset+ "</a></li>";
                        } else {
                            output += "<li><a yxhref=\"" + opts.url + i + "\">" + i+ "</a></li>";
                        }
                    }
                    output += nextBar;
                }
                $(opts.pageParent).append(output);
                opts.offset = offset; //将偏移量赋值给config里面的offset
            }
        }
 
        //清理函数,防止多绑定事件和重新计算分页
        var clear = function () {
            $(opts.pageParent).empty().undelegate();
        }
 
 
        //初始化装配分页按钮
        var init = function (fn) {
            if (typeof (fn) != "function") {
                console.log("将不能正确的执行回调函数");
            } else {
                opts.callback = fn;
            }
            clear();
            ajaxCore(1, opts.callback);//执行初始化ajax方法
            var preBar = "<li class=\"am-disabled\"><a yxhref=\"javascript:;\">«</a></li>";
            //上一页,(禁用的效果)
            //如果只有一页,那么禁用下一页
            var nextBar = opts.totalBars > 1 ? "<li><a yxhref=\"" + opts.url + 2 + "\">»</a></li>" : "<li class=\"am-disabled\"><a yxhref=\"javascript:;\">»</a></li>";
            //最后一页
            var output = "<li class=\"am-active\"><a yxhref=\"" + opts.url + 1 + "\">1</a></li>";
 
            if (opts.totalBars <= 7) {
                for (var i = 1; i < opts.totalBars; i++) {
                    output += "<li><a yxhref=\"" + opts.url + (i + 1) + "\">" + (i + 1) + "</a></li>";
                }
            } else {
                for (var j = 1; j < 5; j++) {
                    output += "<li><a yxhref=\"" + opts.url + (j + 1) + "\">" + (j + 1) + "</a></li>";
                }
                output += "<li><span>...</span></li>";
                output += "<li><a yxhref=\"" + opts.url + (opts.totalBars) + "\">" + (opts.totalBars) + "</a></li>";
            }
            $(opts.pageParent).delegate("a","click", function () {
                var offset = queryString($(this).attr("yxhref"));
                console.log("ok");
                pageCore(offset);
            });
            $(opts.pageParent).append(preBar + output + nextBar);
        };
        init(opts.callback);//初始化分页引擎
    }
}(window.jQuery))

2、获取总页数,再获取分页

$.ajax({
        type: "GET",
        url: selectSendNumberNumsByContURL,//获取总数
        data: {},
        dataType: "json",
        success: function(data){

            if (data[0].code == 200) {

                $("#paginator").paginator({
                    url: selectSendNumberByContURL + "?offsets=",
                    pageParent: "#paginator",
                    totalBars: data[0].allNums,
                    limit: 10,
                    offset: 1,
                    callback: function (data1) {

                        //清空DOM节点
                        
                        //动态加dom节点
                    }
                });
            }else{

            }
        },
        error: function (err) {

        }
    });

后端实现(分页)

这里是controller,拿到offset(第几页)参数、limit(每页多少数量),再写SQL实现分页就好了。

@RequestMapping(value = "/selectNumberCheckByCont", method = RequestMethod.POST)
    @ResponseBody
    public List<ReturnUtils> selectNumberCheckByCont(HttpServletRequest request,
                                                     HttpServletResponse response) throws Exception {

        //统一设置返回数据格式
        response.setContentType("application/json");
        response.setHeader("Pragma", "no-cache");
        response.setCharacterEncoding("UTF-8");

        String offset = request.getParameter("offset");
        String limit = request.getParameter("limit");

        List<ReturnUtils> list = iNumberCheckService.selectNumberCheckByCont(offset, limit);

        return list;
    }

总结

到此这篇关于前后端结合实现amazeUI分页的文章就介绍到这了,更多相关amazeUI分页内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章,希望大家以后多多支持三水点靠木!

HTML / CSS 相关文章推荐
CSS3 3D制作实战案例分析
Sep 18 HTML / CSS
CSS3教程(6):创建网站多列
Apr 02 HTML / CSS
CSS3结构性伪类选择器九种写法
Apr 18 HTML / CSS
CSS3 制作旋转的大风车(充满童年回忆)
Jan 30 HTML / CSS
CSS3的resize属性使用初探
Sep 27 HTML / CSS
利用CSS3的border-radius绘制太极及爱心图案示例
May 17 HTML / CSS
利用CSS3实现进度条的两种姿势详解
Mar 21 HTML / CSS
css3如何绘制一个圆圆的loading转圈动画
Jan 09 HTML / CSS
CSS3 实现footer 固定在底部(无论页面多高始终在底部)
Oct 15 HTML / CSS
五个2015 年最佳HTML5 框架
Nov 11 HTML / CSS
h5页面背景图很长要有滚动条滑动效果的实现
Jan 27 HTML / CSS
html+css实现文字折叠特效实例
Jun 02 HTML / CSS
AmazeUI 加载进度条的实现示例
Aug 20 #HTML / CSS
AmazeUI图片轮播效果的示例代码
Aug 20 #HTML / CSS
AmazeUI折叠式卡片布局,整合内容列表、表格组件实现
Aug 20 #HTML / CSS
AmazeUI 平滑滚动效果的示例代码
Aug 20 #HTML / CSS
AmazeUI在模态框中嵌入表单形成模态输入框
Aug 20 #HTML / CSS
amaze ui 的使用详细教程
Aug 19 #HTML / CSS
AmazeUI中模态框的实现
Aug 19 #HTML / CSS
You might like
关于PHP的curl开启问题探讨
2014/04/08 PHP
php实现生成带二维码图片并强制下载功能
2018/02/24 PHP
PHP实现字符串的全排列详解
2019/04/24 PHP
setTimeout 不断吐食CPU的问题分析
2009/04/01 Javascript
js String对象中常用方法小结(字符串操作)
2012/01/27 Javascript
jQuery使用数组编写图片无缝向左滚动
2012/12/11 Javascript
用js写了一个类似php的print_r输出换行功能
2013/02/18 Javascript
AngularJS 遇到的小坑与技巧小结
2016/06/07 Javascript
js仿腾讯QQ的web登陆界面
2016/08/19 Javascript
Javascript点击按钮随机改变数字与其颜色
2016/09/01 Javascript
利用JavaScript实现拖拽改变元素大小
2016/12/14 Javascript
微信小程序实现获取准确的腾讯定位地址功能示例
2019/03/27 Javascript
使用element-ui的el-menu导航选中后刷新页面保持当前选中状态
2019/07/19 Javascript
layui数据表格重载实现往后台传参
2019/11/15 Javascript
Python利用公共键如何对字典列表进行排序详解
2018/05/19 Python
python的schedule定时任务模块二次封装方法
2019/02/19 Python
使用 Python 快速实现 HTTP 和 FTP 服务器的方法
2019/07/22 Python
把django中admin后台界面的英文修改为中文显示的方法
2019/07/26 Python
基于Django的乐观锁与悲观锁解决订单并发问题详解
2019/07/31 Python
python matplotlib中的subplot函数使用详解
2020/01/19 Python
Tensorflow 卷积的梯度反向传播过程
2020/02/10 Python
PyTorch笔记之scatter()函数的使用
2020/02/12 Python
Python多线程正确用法实例解析
2020/05/30 Python
浅谈tensorflow使用张量时的一些注意点tf.concat,tf.reshape,tf.stack
2020/06/23 Python
英国顶级家庭折扣店:The Works
2017/09/06 全球购物
艺龙旅行网酒店预订:国内、港澳台酒店
2018/06/26 全球购物
ParcelABC西班牙:包裹运送和快递服务
2019/12/24 全球购物
比较基础的php面试题及答案-填空题
2014/04/26 面试题
最新教师自我评价分享
2013/11/12 职场文书
自动化专业个人求职信范文
2013/11/29 职场文书
培训讲师邀请函
2014/01/10 职场文书
勿忘国耻9.18演讲稿(经典篇)
2014/09/14 职场文书
班主任培训研修日志
2015/11/13 职场文书
简历上的自我评价,该怎么写呢?
2019/06/13 职场文书
Nginx配置文件详解以及优化建议指南
2021/09/15 Servers
PostgreSQL事务回卷实战案例详析
2022/03/25 PostgreSQL