jquery提供很方法的js插件開發函數,關於jquery插件的開發教程,能夠參考博主的上一篇博文《jQuery插件開發精品教程》,這裏就再也不重複。javascript
因爲項目中常常會使用到分頁顯示列表數據,這裏就會用到關於一組分頁按鈕,以前的分頁按鈕是經過jsp的自定義標籤來實現的一組分頁按鈕,關於jsp的自定義標籤流程,能夠參考《週記(jsp自定義標籤)》。接觸了jquery插件開發後,覺的js插件,無論是使用仍是管理,或者是二次開發,都比jsp的自定義標籤使用起來方便,這裏就經過js的插件形勢編寫分頁按鈕組。css
具體js插件代碼以下:html
- ;(function($,window,document,undefined){
-
- var SimplePaging = function(ele,opt){
- this.$element = ele;
- this.defaults = {
- 'test':"aaa",
-
- 'cPage':1,
- 'tPage':1,
- 'clickFun':null,
-
- 'space':10,
- 'showLast':true,
- 'showNext':true,
- 'div':{
- 'float':'right',
- 'font-size':'14px',
- '-moz-user-select': 'none',
- '-webkit-user-select':'none',
- '-ms-user-select':'none',
- '-khtml-user-select':'none',
- 'user-select':'none'
- },
- 'btn':{
- 'display':'inline-block',
- 'position': 'relative',
- 'padding':'0 5px',
- 'letter-spacing':'0px',
- 'cursor':'pointer',
- },
- 'btnOver':{
- 'top':'-1px',
- },
- 'btnOut':{
- 'top':'0px',
- },
- 'btnAction':{
- 'display':'inline-block',
- 'position': 'relative',
- 'padding':'0 5px',
- 'letter-spacing':'0px',
- 'color':'#EA8010',
- 'top':'-1px',
- 'cursor':'default',
- },
- };
- this.options = $.extend({},this.defaults,opt);
- };
-
-
- SimplePaging.prototype = {
- test:function(){
- this.$element.html(this.options.test);
- return this.$element;
- },
- writePageBtn:function(){
- var cPage = this.options.cPage;
- var tPage = this.options.tPage;
- var space = this.options.space;
- var funName = this.options.clickFun;
- var c = Math.ceil(cPage/space);
-
- var id = this.$element.prop("id");
- var pageDiv = '';
-
- if(this.options.showLast){
- pageDiv = pageDiv + '<span class="pageSpan_'+id+'" onclick="'+funName+'(1)">|<</span>';
- }
-
- if(this.options.showNext){
- if(cPage>1){
- pageDiv = pageDiv + '<span class="pageSpan_'+id+'" onclick="'+funName+'('+(cPage-1)+')"><<</span>';
- }else{
- pageDiv = pageDiv + '<span class="pageSpan_'+id+'" onclick="javascript:confirm(\'當前已爲第一頁\');"><<</span>';
- }
- }
-
- for(var i=1;i<c;i++){
- pageDiv = pageDiv + '<span class="pageSpan_'+id+'" onclick="'+funName+'('+(i*space)+')">'+(i*space)+'</span>';
- }
-
- for(var i=(c-1)*space+1;i<=c*space&&i<=tPage;i++){
-
- if(cPage==i){
- pageDiv = pageDiv + '<span class="actionPageSpan_'+id+'">'+i+'</span>';
- }else{
- pageDiv = pageDiv + '<span class="pageSpan_'+id+'" onclick="'+funName+'('+i+')">'+i+'</span>';
- }
- }
-
- for(var i=c+1;i*space<=tPage;i++){
- pageDiv = pageDiv + '<span class="pageSpan_'+id+'" onclick="'+funName+'('+(i*space)+')">'+(i*space)+'</span>';
- }
-
- if(this.options.showNext){
- if(cPage<tPage){
- pageDiv = pageDiv + '<span class="pageSpan_'+id+'" onclick="'+funName+'('+(cPage+1)+')">>></span>';
- }else{
- pageDiv = pageDiv + '<span class="pageSpan_'+id+'" onclick="javascript:confirm(\'當前已爲最後一頁\');">>></span>';
- }
- }
-
- if(this.options.showLast){
- pageDiv = pageDiv + '<span class="pageSpan_'+id+'" onclick="'+funName+'('+tPage+')">>|</span>';
- }
- this.$element.html(pageDiv);
-
-
- var div = $.extend({},this.defaults.div,this.options.div);
- this.$element.css(div);
- var btn = $.extend({},this.defaults.btn,this.options.btn);
- this.$element.find(".pageSpan_"+id).css(btn);
- var obj = this;
- this.$element.find(".pageSpan_"+id).hover(function(){
- var btnOver = $.extend({},obj.defaults.btnOver,obj.options.btnOver);
- $(this).css(btnOver);
- },function(){
- var btnOut = $.extend({},obj.defaults.btnOut,obj.options.btnOut);
- $(this).css(btnOut);
- });
- this.$element.find(".actionPageSpan_"+id).css(this.options.btnAction);
- }
- };
-
-
- $.fn.simplePaging = function(options){
- var simplePaging = new SimplePaging(this,options);
- return simplePaging.writePageBtn();
- };
-
- })(jQuery,window,document);
注意:該js插件是基於jquery開發的,使用時需先導入jqueryjava
簡單使用代碼:jquery
- <html>
- <script src="jquery-1.10.2.js" ></script>
- <script src="simplePaging.js"></script>
- <script type="text/javascript">
-
- function load(){
- var param = {
- "cPage":1,
- "tPage":54,
- "clickFun":"test", //該函數接受一個cPage參數
- "showLast":false,
- "div":{"float":"left"}
- };
- $("#testDiv").simplePaging(param);
- }
-
- function test(cPage){
- //業務代碼塊
-
- //分頁按鈕組控制塊
- var param = {
- "cPage":cPage,
- "tPage":54,
- "clickFun":"test",
- "showLast":false,
- "div":{"float":"left"}
- };
- $("#testDiv").simplePaging(param);
- }
-
- </script>
- <body onload="load()">
- <div id="testDiv">
-
- </div>
- </body>
- </html>
效果:web
插件參數說明:jquery插件
- this.defaults = {
- 'test':"aaa",
-
- 'cPage':1,
- 'tPage':1,
- 'clickFun':null,
-
- 'space':10,
- 'showLast':true,
- 'showNext':true,
- 'div':{ },
- 'btn':{ },
- 'btnOver':{ },
- 'btnOut':{ },
- 'btnAction':{},
- };
實際項目中的效果圖jsp