前后端结合实现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; }
总结
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
暂无评论...
稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!
昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。
这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。
而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?
更新日志
2024年09月24日
2024年09月24日
- 纯音入心系列纯音乐《天籁古筝》1CD[MP3][331MB]
- 男女对唱典藏天碟《发烧对唱·那个季节里的歌DSD》10CD[WAV]
- 群星2010-歌林精选辑[歌林][WAV+CUE]
- TrioZimbalist-PianoTriosofWeinberg,Auerbach,Dvorak(2024)[24-44,1]wav
- 群星.1992-滚石第一流台湾歌·12王牌大车拼【滚石】【WAV+CUE】
- 林子祥.1984-创作歌集【华纳】【WAV+CUE】
- 周汤豪.2010-周汤豪【华纳】【FLAC分轨】
- Mozart-TheStringQuintets-AmadeusQuartet,CecilAronowitz(2017)[24-44,1][WAV+CUE]
- JamesWilliamsDennisIrwin-Focus(2024,Red)[24-48]FLAC
- 藤泽麻衣《空みあげて》[WAV]
- 纯音入心系列纯音乐《中国古筝经典名曲》1CD[MP3][1.3GB]
- 纯音入心系列纯音乐《古筝新奏:岁月静好与筝语,细水流年与筝同》1CD[MP3][846.9MB]
- 670《脆弱敏感小女生》[320K/MP3][27.53MB]
- 曾琳.2011-好好爱我【南方】【WAV+CUE】
- 范晓萱.1995-RAIN【福茂】【WAV+CUE】