前后端结合实现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 相关文章推荐
基于DOM+CSS3实现OrgChart组织结构图插件
Mar 02 HTML / CSS
CSS3制作酷炫的条纹背景
Nov 09 HTML / CSS
CSS3解析抖音LOGO制作的方法步骤
Apr 11 HTML / CSS
html5指南-5.使用web storage存储键值对的数据
Jan 07 HTML / CSS
详解canvas多边形(蜘蛛图)的画法示例
Jan 29 HTML / CSS
浅析图片上传及canvas压缩的流程
Jun 10 HTML / CSS
HTML5 层的叠加的实现
Jul 07 HTML / CSS
HTML5 SEO优化的一些建议
Aug 27 HTML / CSS
css 中多种边框的实现小窍门
Apr 07 HTML / CSS
HTML基础详解(下)
Oct 16 HTML / CSS
企业开发CSS命名BEM代码规范实践
Feb 12 HTML / CSS
CSS 鼠标点击拖拽效果的实现代码
Dec 24 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实现LOL数据远程获取
2014/06/10 PHP
php实现只保留mysql中最新1000条记录
2015/06/18 PHP
PHP中session跨子域的三种实现方法
2016/07/25 PHP
深入理解 PHP7 中全新的 zval 容器和引用计数机制
2018/10/15 PHP
js可突破windows弹退效果代码
2008/08/09 Javascript
javascript 最常用的10个自定义函数[推荐]
2009/12/26 Javascript
Javascript学习指南
2014/12/01 Javascript
原生JS实现响应式瀑布流布局
2015/04/02 Javascript
AngularJS实现与Java Web服务器交互操作示例【附demo源码下载】
2016/11/02 Javascript
如何用webpack4带你实现一个vue的打包的项目
2018/06/20 Javascript
Vue一个案例引发的递归组件的使用详解
2018/11/15 Javascript
js实现扫雷源代码
2020/11/27 Javascript
Vue中避免滥用this去读取data中数据
2021/03/02 Vue.js
Python实现子类调用父类的方法
2014/11/10 Python
Python利用前序和中序遍历结果重建二叉树的方法
2016/04/27 Python
python 显示数组全部元素的方法
2018/04/19 Python
python多进程提取处理大量文本的关键词方法
2018/06/05 Python
Flask框架实现的前端RSA加密与后端Python解密功能详解
2019/08/13 Python
python自动点赞功能的实现思路
2020/02/26 Python
Python实现猜年龄游戏代码实例
2020/03/25 Python
matplotlib绘制多子图共享鼠标光标的方法示例
2021/01/08 Python
python 制作本地应用搜索工具
2021/02/27 Python
使用CSS3实现input多选框自定义样式的方法示例
2019/07/19 HTML / CSS
意大利奢华内衣制造商:Cosabella
2017/08/29 全球购物
美国体育用品商店:Rally House(NCAA、NFL、MLB、NBA、NHL和MLS)
2018/01/03 全球购物
英国最受欢迎的在线隐形眼镜商店:VisionDirect.co.uk
2018/12/06 全球购物
英国DIY汽车维修配件网站:DIY Car Service Parts
2019/08/30 全球购物
中英文自我评价常用句型
2013/12/19 职场文书
模具数控专业自荐信
2014/01/27 职场文书
幼儿园教师教学反思
2014/02/06 职场文书
追悼会主持词
2014/03/20 职场文书
小学六年级学生评语
2014/04/22 职场文书
公司开业庆典策划方案
2014/06/04 职场文书
2016年会开场白台词
2015/06/01 职场文书
党支部考察鉴定意见
2015/06/02 职场文书
nginx实现发布静态资源的方法
2021/03/31 Servers