基于jQuery封装的分页组件
程序员文章站
2023-09-06 16:10:46
由于项目需要实现分页效果,上jquery插件库找了下,但是木有找到自己想要的效果,于是自己封装了个分页组件。
思路:
主要是初始化时基于原型建立的分页模板然后绑定动态事...
由于项目需要实现分页效果,上jquery插件库找了下,但是木有找到自己想要的效果,于是自己封装了个分页组件。
思路:
主要是初始化时基于原型建立的分页模板然后绑定动态事件并实现刷新dom的分页效果。
1.page.init.css
@charset "utf=8"; *{ box-sizing: border-box; padding: 0; margin: 0; } .page{ font-size: 13px; text-align: right; } .page .page_to{ display: inline-block; max-width: 250px; } .page .page_to li{ display: inline-block; width: auto; height: auto; border: 1px solid #ddd; padding:5px 10px; border-left-width: 0; color: #323232; cursor: pointer; } .page .page_to li.page_hide{ display: none; } .page .page_to li:hover{ color: #32c2cd; background-color: #f4f4f4; border-color: #dddddd; } .page .page_to li:first-child{ border-left-width: 1px; } .page .page_jump{ display: inline-block; width: 180px; } .page .page_jump input.page_jump_input{ width: 52px; height: 28px; text-align: center; text-decoration: none; background-color: #fff; border: 1px solid #ddd; margin:0 4px; } .page .page_jump input.page_jump_btn{ display: inline-block; padding: 7px 20px; margin-left: 5px; font-size: 14px; font-weight: 400; line-height: 1.42857143; text-align: center; white-space: nowrap; vertical-align: middle; -ms-touch-action: manipulation; touch-action: manipulation; cursor: pointer; -webkit-user-select: none; -moz-user-select: none; -ms-user-select: none; user-select: none; border: 1px solid transparent; border-radius: 4px; background-color: #00bb9c; color: #ffffff; font-weight: bold; }
2.pageinit.js
/** * created: 2017/6/20. * author: aaron * address: http://www.cnblogs.com/aaron-pan/ */ (function($,window,undefined){ var curpage='', //跳转是否有值 jumpval='', //从dom中重新获取数据总数/总页数 lists='', totals='', //是否返回值 istrue=false; var page=function(opts){ this.settings= $.extend({},page.defaults,opts); curpage=this.settings.initpage; totals=this.settings.totalpages; jumpval=this.settings.inputval; this.init(); }; //默认配置 page.defaults={ container:'.page', setpos:'body', totalpages:null, totallists:null, initpage:1, inputval:1, callback:null }; page.prototype={ init:function(){ this.create(); }, create:function(){ var _template='<div class="page">'+ '<span class="page_details">'+ '共<span class="page_num">'+this.settings.totallists+'</span>条记录,'+ '第<span class="page_current">'+curpage+'</span>/'+ '<span class="page_size">'+this.settings.totalpages+'</span>页'+ '</span>'+ '<div class="page_to">'+ '<ul class="flex_parent">'+ '<li class="page_first flex_child">首页</li>'+ '<li class="page_pre page_hide flex_child">« 上一页</li>'+ '<li class="page_next flex_child">下一页 »</li>'+ '<li class="page_last flex_child">末页</li>'+ '</ul>'+ '</div>'+ '<div class="page_jump">'+ '<span>第:<input type="number" class="page_jump_input" value="'+this.settings.inputval+'">页</span>'+ '<input type="button" class="page_jump_btn" value="go">'+ '</div>'+ '</div>'; $(this.settings.setpos).append(_template); this.refreshdom(); this.bindevent(); }, bindevent:function(){ var _this=this; //跳转首页 $(this.settings.container).on("click",".page_first",function(){ lists=$(_this.settings.container).find(".page_num").text(); totals=$(_this.settings.container).find(".page_size").text(); if($.isfunction(_this.settings.callback)){ curpage=1; istrue=_this.settings.callback(1); if(istrue){ _this.refreshdom(); $(_this.settings.container).find(".page_current").text(1); $(_this.settings.container).find(".page_jump_input").val(curpage); } } }); //跳转上一页 $(this.settings.container).on("click",".page_pre",function(){ lists=$(_this.settings.container).find(".page_num").text(); totals=$(_this.settings.container).find(".page_size").text(); if($.isfunction(_this.settings.callback)){ if(curpage>1){ curpage=curpage-1; istrue=_this.settings.callback(curpage); if(istrue){ _this.refreshdom(); $(_this.settings.container).find(".page_current").text(curpage); $(_this.settings.container).find(".page_jump_input").val(curpage); } } } }); //跳转下一页 $(this.settings.container).on("click",".page_next",function(){ lists=$(_this.settings.container).find(".page_num").text(); totals=$(_this.settings.container).find(".page_size").text(); if($.isfunction(_this.settings.callback)){ if(curpage<totals){ curpage=curpage+1; istrue=_this.settings.callback(curpage); if(istrue){ _this.refreshdom(); $(_this.settings.container).find(".page_current").text(curpage); $(_this.settings.container).find(".page_jump_input").val(curpage); } } } }); //跳转末页 $(this.settings.container).on("click",".page_last",function(){ lists=$(_this.settings.container).find(".page_num").text(); totals=$(_this.settings.container).find(".page_size").text(); if($.isfunction(_this.settings.callback)){ curpage=totals; istrue=_this.settings.callback(curpage); if(istrue){ _this.refreshdom(); $(_this.settings.container).find(".page_current").text(totals); $(_this.settings.container).find(".page_jump_input").val(curpage); } } }); //go跳转 $(this.settings.container).on("click",".page_jump_btn",function(){ lists=$(_this.settings.container).find(".page_num").text(); totals=$(_this.settings.container).find(".page_size").text(); if($.isfunction(_this.settings.callback)){ jumpval=number($(_this.settings.container).find("input.page_jump_input").val()); console.log('跳转的页数:'+jumpval+';跳转之前的页数:'+curpage); if(jumpval>=1 && jumpval <=totals){ curpage=jumpval; istrue=_this.settings.callback(curpage); if(istrue){ _this.refreshdom(); $(_this.settings.container).find(".page_current").text(curpage); } }else{ jumpval=curpage; } } }); }, refreshdom:function(){ $(this.settings.container).find("li.flex_child").removeclass("page_hide"); if(number(totals)==1){ $(this.settings.container).find(".page_pre").addclass("page_hide"); $(this.settings.container).find(".page_next").addclass("page_hide"); } else if(number(totals)==2){ if(number(curpage)==1){ $(this.settings.container).find(".page_pre").addclass("page_hide"); }else{ $(this.settings.container).find(".page_next").addclass("page_hide"); } } else if(number(curpage)==1 && number(totals)>2){ $(this.settings.container).find(".page_pre").addclass("page_hide"); } else if(number(curpage)==number(totals) && number(totals)>2){ $(this.settings.container).find(".page_next").addclass("page_hide"); } } }; var pageinit=function(opts){ return new page(opts); }; window.pageinit= $.pageinit=pageinit; })(jquery,window,undefined);
3.组件调用
通过 window.pageinit= $.pageinit=pageinit 可完成分页组件初始化。
暴露出来的接口分别为:
1.container:dom的容器,默认.page
2.setpos:dom放置的html位置,默认body
3.totalpages:默认的页数,必填,默认null
4.totallists:默认的数据总数,必填,默认null
5.initpage:当前页,默认第一页
6.inputval:跳转页,默认第一页
7.callback:执行的回调函数,必填,默认null
<!doctype html> <html> <head lang="en"> <meta charset="utf-8"> <title>基于jquery封装的分页组件</title> <link rel="stylesheet" href="css/page.init.css"> </head> <body> <script src="https://cdn.bootcss.com/jquery/2.2.4/jquery.js"></script> <script src="js/pageinit.js"></script> <script> $.pageinit( { container:'.page',//容器:默认page //setpos:'body',//放置位置:默认body totalpages:10,//总页数:必填 totallists:100,//数据总数:必填 initpage:1,//初始页码:默认1 inputval:1,//设置跳转的input值:默认1 //要执行的函数:默认null,必须为fn且返回true则可执行分页,false则不执行 callback:function(n){ var flag=true; console.log(n); return flag; } } ); </script> </body> </html>
效果:
通过callback接口,添加自己所需要执行的function函数,并且需要return true时才回执行动态的dom渲染。
更多精彩内容请点击:jquery分页功能汇总进行学习。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。