前后端结合实现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分页内容请搜索代码网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持代码网!
发表评论