前后端结合实现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 相关文章推荐
纯CSS实现颜色渐变效果(包含环形渐变、线性渐变、彩虹效果等)
May 07 HTML / CSS
css3 transform属性详解
Sep 30 HTML / CSS
使用css3实现的tab选项卡代码分享
Dec 09 HTML / CSS
css3 实现圆形旋转倒计时
Feb 24 HTML / CSS
10分钟入门CSS3 Animation
Dec 25 HTML / CSS
CSS3 实现发光边框特效
Nov 11 HTML / CSS
HTML5网页音乐播放器的示例代码
Nov 09 HTML / CSS
突袭HTML5之Javascript API扩展4—拖拽(Drag/Drop)概述
Jan 31 HTML / CSS
HTML5 Canvas 旋转风车绘制
Aug 18 HTML / CSS
AmazeUI 面板的实现示例
Aug 17 HTML / CSS
CSS3实现的水平标题菜单
Apr 14 HTML / CSS
CSS3 实现NES游戏机的示例代码
Apr 21 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
解决文件名解压后乱码的问题 将文件名进行转码的代码
2012/01/10 PHP
让CodeIgniter的ellipsize()支持中文截断的方法
2014/06/12 PHP
php导出CSV抽象类实例
2014/09/24 PHP
php显示指定目录下子目录的方法
2015/03/20 PHP
JavaScript 核心参考教程 内置对象
2009/10/13 Javascript
JSDoc 介绍使用规范JsDoc的使用介绍
2011/02/12 Javascript
IE6/7 and IE8/9/10(IE7模式)依次隐藏具有absolute或relative的父元素和子元素后再显示父元素
2011/07/31 Javascript
javascript 基础篇4 window对象,DOM
2012/03/14 Javascript
JavaScript高级程序设计(第3版)学习笔记9 js函数(下)
2012/10/11 Javascript
JS中Iframe之间传值及子页面与父页面应用
2013/03/11 Javascript
jQuery中slideUp()方法用法分析
2014/12/24 Javascript
jQuery实现文本框邮箱输入自动补全效果
2015/11/17 Javascript
通过sails和阿里大于实现短信验证
2017/01/04 Javascript
JS实现队列的先进先出功能示例
2017/05/10 Javascript
React Native实现进度条弹框的示例代码
2017/07/17 Javascript
node实现分片下载的示例代码
2018/10/17 Javascript
layui form表单提交后实现自动刷新
2019/10/25 Javascript
vue简单封装axios插件和接口的统一管理操作示例
2020/02/02 Javascript
JavaScript arguments.callee作用及替换方案详解
2020/09/02 Javascript
[50:28]2018DOTA2亚洲邀请赛 3.31 小组赛 A组 Newbee vs KG
2018/04/01 DOTA
[51:52]Liquid vs Secret 2019国际邀请赛淘汰赛 败者组 BO3 第二场 8.24
2019/09/10 DOTA
python中while循环语句用法简单实例
2015/05/07 Python
详解Python的Django框架中的Cookie相关处理
2015/07/22 Python
Python交互环境下实现输入代码
2018/06/22 Python
python3使用matplotlib绘制条形图
2020/03/25 Python
浅谈Python中(&amp;,|)和(and,or)之间的区别
2019/08/07 Python
在python中修改.properties文件的操作
2020/04/08 Python
python如何建立全零数组
2020/07/19 Python
攀岩、滑雪、徒步旅行装备:Black Diamond Equipment
2019/08/16 全球购物
AJAX都有哪些有点和缺点
2012/11/03 面试题
J2EE的优越性主要表现在哪些方面
2016/03/28 面试题
大学生思想汇报范文
2013/12/31 职场文书
《永远的白衣战士》教学反思
2014/04/25 职场文书
护士2014年终工作总结
2014/11/11 职场文书
离婚协议书范文2014(夫妻感情破裂)
2014/12/14 职场文书
遗失说明具结保证书
2015/02/26 职场文书