• 设为首页
  • 收藏本站
  • 积分充值
  • VIP赞助
  • 手机版
  • 微博
  • 微信
    微信公众号 添加方式:
    1:搜索微信号(888888
    2:扫描左侧二维码
  • 快捷导航
    福建二哥 门户 查看主题

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

    发布者: 天下网吧 | 发布时间: 2025-6-16 12:35| 查看数: 142| 评论数: 0|帖子模式

    前后端结合实现amazeUI分页,代码如下所示;
    借鉴
    1. 本文在博客https://blog.csdn.net/brave_coder/article/details/52367124的基础上实现的,非常感谢大佬的分享。
    复制代码
    前端实现
    1、引入paginator.js
    1. (function ($) {
    2.     $.fn.paginator = function (options) {
    3.         //this指向当前的选择器
    4.         var config = {
    5.             url: "",
    6.             pageParent: "",
    7.             totalBars: -1,
    8.             limit: -1,
    9.             offset: 1,
    10.             callback: null
    11.         }
    12.         //合并参数
    13.         var opts = $.extend(config, options);

    14.         opts.totalBars = Math.ceil(opts.totalBars / opts.limit);
    15.         //计算按钮的总个数

    16.         //获取offset参数
    17.         var queryString = function (url) {
    18.             var offset = (url.split("?")[1]).split("=")[1];
    19.             return parseInt(offset);
    20.         }

    21.         //ajax核心方法,用于分页的数据操作
    22.         var ajaxCore = function (offset, fn) {
    23.             $.ajax({
    24.                 "url": opts.url,
    25.                 "data": {
    26.                     "offset": offset,
    27.                     "limit": opts.limit
    28.                 },
    29.                 "dataType": "JSON",
    30.                 "method": "POST",
    31.                 "success": fn
    32.             });
    33.         }

    34.         //重新装配分页按钮
    35.         var pageCore = function (offset) {
    36.             if (opts.offset == offset) {
    37.                 return;
    38.             } //如果是当前页面,那么就什么事都不用干了!
    39.             else {
    40.                 ajaxCore(offset, opts.callback);
    41.                 $(opts.pageParent).empty();
    42.                 //否则,清空所有的节点,重新向DOM插入新的分页按钮
    43.                 var output = "";
    44.                 var nextBar = offset == opts.totalBars ? "<li class="am-disabled"><a yxhref="javascript:;">»</a></li>" : "<li><a yxhref="" + opts.url + (offset + 1) + "">»</a></li>";
    45.                 var preBar = offset == 1 ? "<li class="am-disabled"><a yxhref="javascript:;">«</a></li>" : "<li><a yxhref="" + opts.url + (offset - 1) + "">«</a></li>";
    46.                 //组装向上一个节点和下一页节点
    47.                 if (opts.totalBars > 7) {
    48.                     if (offset < 5) {
    49.                         output += preBar;
    50.                         for (var i = 1; i <= 5; i++) {
    51.                             if (i == offset) {
    52.                                 output += "<li class="am-active"><a yxhref="" + opts.url + offset + "">" + offset + "</a></li>";
    53.                             } else {
    54.                                 output += "<li><a yxhref="" + opts.url + i + "">" + i + "</a></li>";
    55.                             }
    56.                         }
    57.                         output += "<li><span>...</span></li>";
    58.                         output += "<li><a yxhref="" + opts.url + (opts.totalBars) + "">" + (opts.totalBars) + "</a></li>" + nextBar;
    59.                     } else if (offset >= 5 && offset <= opts.totalBars - 4) {
    60.                         //当页面大于7个的时候,那么在第五个和倒数第五个时,执行
    61.                         output += preBar;
    62.                         output += "<li><a yxhref="" + opts.url + 1 + "">" + 1 + "</a></li>";
    63.                         //第一个
    64.                         output += "<li><span>...</span></li>"; //省略号

    65.                         output += "<li><a yxhref="" + opts.url + (offset - 1) + "">" + (offset - 1) + "</a></li>";

    66.                         output += "<li class="am-active"><a  yxhref="" + opts.url + offset + "">" + offset + "</a></li>";

    67.                         output += "<li><a yxhref="" + opts.url + (offset + 1) + "">" + (offset + 1) + "</a></li>";

    68.                         output += "<li><span>...</span></li>"; //省略号;

    69.                         output += "<li><a yxhref="" + opts.url + (opts.totalBars) + "">" + (opts.totalBars) + "</a></li>"; //尾页

    70.                         output += nextBar;

    71.                     } else if (offset > opts.totalBars - 4 && offset <= opts.totalBars) {
    72.                         //当页面位于倒数第四个时候
    73.                         output += preBar;
    74.                         output += "<li><a yxhref="" + opts.url + 1 + "">" + 1 + "</a></li>" + "<li><span>...</span></li>";

    75.                         for (var j = 4; j >= 0; j--) {
    76.                             if (opts.totalBars - j == offset) {
    77.                                 output += "<li class="am-active"><a yxhref="" + opts.url + (opts.totalBars - j) + "">" + (opts.totalBars - j) + "</a></li>";
    78.                             } else {
    79.                                 output += "<li><a yxhref="" + opts.url + (opts.totalBars - j) + "">" + (opts.totalBars - j) + "</a></li>";
    80.                             }
    81.                         }
    82.                         output += nextBar;
    83.                     } else {
    84.                         console.log("分页数据出错!");
    85.                         return;
    86.                     }
    87.                 } else {
    88.                     output += preBar;
    89.                     for (var i = 1; i <= opts.totalBars; i++) {
    90.                         if (i == offset) {
    91.                             output += "<li class="am-active"><a yxhref="" + opts.url + offset + "">" + offset+ "</a></li>";
    92.                         } else {
    93.                             output += "<li><a yxhref="" + opts.url + i + "">" + i+ "</a></li>";
    94.                         }
    95.                     }
    96.                     output += nextBar;
    97.                 }
    98.                 $(opts.pageParent).append(output);
    99.                 opts.offset = offset; //将偏移量赋值给config里面的offset
    100.             }
    101.         }

    102.         //清理函数,防止多绑定事件和重新计算分页
    103.         var clear = function () {
    104.             $(opts.pageParent).empty().undelegate();
    105.         }


    106.         //初始化装配分页按钮
    107.         var init = function (fn) {
    108.             if (typeof (fn) != "function") {
    109.                 console.log("将不能正确的执行回调函数");
    110.             } else {
    111.                 opts.callback = fn;
    112.             }
    113.             clear();
    114.             ajaxCore(1, opts.callback);//执行初始化ajax方法
    115.             var preBar = "<li class="am-disabled"><a yxhref="javascript:;">«</a></li>";
    116.             //上一页,(禁用的效果)
    117.             //如果只有一页,那么禁用下一页
    118.             var nextBar = opts.totalBars > 1 ? "<li><a yxhref="" + opts.url + 2 + "">»</a></li>" : "<li class="am-disabled"><a yxhref="javascript:;">»</a></li>";
    119.             //最后一页
    120.             var output = "<li class="am-active"><a yxhref="" + opts.url + 1 + "">1</a></li>";

    121.             if (opts.totalBars <= 7) {
    122.                 for (var i = 1; i < opts.totalBars; i++) {
    123.                     output += "<li><a yxhref="" + opts.url + (i + 1) + "">" + (i + 1) + "</a></li>";
    124.                 }
    125.             } else {
    126.                 for (var j = 1; j < 5; j++) {
    127.                     output += "<li><a yxhref="" + opts.url + (j + 1) + "">" + (j + 1) + "</a></li>";
    128.                 }
    129.                 output += "<li><span>...</span></li>";
    130.                 output += "<li><a yxhref="" + opts.url + (opts.totalBars) + "">" + (opts.totalBars) + "</a></li>";
    131.             }
    132.             $(opts.pageParent).delegate("a","click", function () {
    133.                 var offset = queryString($(this).attr("yxhref"));
    134.                 console.log("ok");
    135.                 pageCore(offset);
    136.             });
    137.             $(opts.pageParent).append(preBar + output + nextBar);
    138.         };
    139.         init(opts.callback);//初始化分页引擎
    140.     }
    141. }(window.jQuery))
    复制代码
    2、获取总页数,再获取分页
    1. $.ajax({
    2.         type: "GET",
    3.         url: selectSendNumberNumsByContURL,//获取总数
    4.         data: {},
    5.         dataType: "json",
    6.         success: function(data){

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

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

    15.                         //清空DOM节点
    16.                         
    17.                         //动态加dom节点
    18.                     }
    19.                 });
    20.             }else{

    21.             }
    22.         },
    23.         error: function (err) {

    24.         }
    25.     });
    复制代码
    后端实现(分页)
    这里是controller,拿到offset(第几页)参数、limit(每页多少数量),再写SQL实现分页就好了。
    1. @RequestMapping(value = "/selectNumberCheckByCont", method = RequestMethod.POST)
    2.     @ResponseBody
    3.     public List<ReturnUtils> selectNumberCheckByCont(HttpServletRequest request,
    4.                                                      HttpServletResponse response) throws Exception {

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

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

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

    12.         return list;
    13.     }
    复制代码
    总结
    到此这篇关于前后端结合实现amazeUI分页的文章就介绍到这了,更多相关amazeUI分页内容请搜索脚本之家以前的文章或继续浏览下面的相关文章,希望大家以后多多支持脚本之家!

    来源:https://www.jb51.net/html5/741754.html
    免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

    最新评论

    QQ Archiver 手机版 小黑屋 福建二哥 ( 闽ICP备2022004717号|闽公网安备35052402000345号 )

    Powered by Discuz! X3.5 © 2001-2023

    快速回复 返回顶部 返回列表