[JQuery]分页插件jQuery pager plugin功能扩展

来源:互联网 发布:雷蛇鼠标mac驱动 编辑:程序博客网 时间:2024/04/28 02:36

原文地址:http://blog.csdn.net/starfd/article/details/25292019



http://blog.csdn.net/nz360/article/details/52326232  牛逼分页


http://www.jq-school.com/Detail.aspx?id=236 小k 自定义分页标签




http://www.cnkk.me/art/2016/04/146174745092807.html




http://m.blog.csdn.net/article/details?id=8581240 




http://www.jq22.com/jquery-info9832




http://www.jq22.com/jquery-plugins%E5%88%86%E9%A1%B5-1-jq




http://m.blog.csdn.net/article/details?id=25292019 


因为项目需要用到了这个插件,但这个插件本身的功能无法达到产品要求,所以对这个插件进行了一些扩展,以下是扩展点:

1、修改原插件只能传递总页码数部分,修正为传递PageSize以及RecordCount,由这两部分计算出总页码数

2、增加当前页码与总页码判断,如果当前页码大于总页码,则修正当前页码为总页码数(当前页码从1开始计算)

3、当总页码数超出9页时,增加快速跳转功能,可输入的最大值为总页码数

4、增加总页数以及总记录数显示

5、对原默认First,Prev,Next,Last部分的文本进行扩展,支持用指定文本替换此部分默认内容

以下是修改后的js,包含原js版本说明:

[javascript] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. /* 
  2. * jQuery pager plugin 
  3. * Version 1.0 (12/22/2008) 
  4. * @requires jQuery v1.2.6 or later 
  5. * 
  6. * Example at: http://jonpauldavies.github.com/JQuery/Pager/PagerDemo.html 
  7. * 
  8. * Copyright (c) 2008-2009 Jon Paul Davies 
  9. * Dual licensed under the MIT and GPL licenses: 
  10. * http://www.opensource.org/licenses/mit-license.php 
  11. * http://www.gnu.org/licenses/gpl.html 
  12.  
  13. * Read the related blog post and contact the author at http://www.j-dee.com/2008/12/22/jquery-pager-plugin/ 
  14. * 
  15. * This version is far from perfect and doesn't manage it's own state, therefore contributions are more than welcome! 
  16. * 
  17. * Usage: .pager({ pagenumber: 1, pagecount: 15, buttonClickCallback: PagerClickTest }); 
  18. * 
  19. * Where pagenumber is the visible page number 
  20. *       pagecount is the total number of pages to display 
  21. *       buttonClickCallback is the method to fire when a pager button is clicked. 
  22. * 
  23. * buttonClickCallback signiture is PagerClickTest = function(pageclickednumber)  
  24. * Where pageclickednumber is the number of the page clicked in the control. 
  25. * 
  26. * The included Pager.CSS file is a dependancy but can obviously tweaked to your wishes 
  27. * Tested in IE6 IE7 Firefox & Safari. Any browser strangeness, please report. 
  28. */  
  29. (function($) {  
  30.   
  31.     $.fn.pager = function(options) {  
  32.   
  33.         var opts = $.extend({}, $.fn.pager.defaults, options);  
  34.         var pgcount = getPageCount(opts.recordcount, opts.pagesize);  
  35.         return this.each(function() {  
  36.   
  37.             // empty out the destination element and then render out the pager with the supplied options  
  38.             $(this).empty().append(renderpager(parseInt(opts.pagenumber), pgcount, opts.recordcount, opts.buttonClickCallback,  
  39.         opts.firsttext, opts.prevtext, opts.nexttext, opts.lasttext, opts.recordtext, opts.gotext));  
  40.   
  41.             // specify correct cursor activity  
  42.             $(' li'this).mouseover(function() { document.body.style.cursor = "pointer"; }).mouseout(function() { document.body.style.cursor = "auto"; });  
  43.         });  
  44.     };  
  45.   
  46.     function getPageCount(totalCount, pageSize) {  
  47.         var pageCount = 0;  
  48.         pageCount = parseInt(totalCount / pageSize);  
  49.         if (totalCount % pageSize > 0) {  
  50.             pageCount++;  
  51.         }  
  52.         return pageCount;  
  53.     }  
  54.   
  55.     // render and return the pager with the supplied options  
  56.     function renderpager(pagenumber, pagecount, recordcount, buttonClickCallback, firsttext, prevtext, nexttext, lasttext, recordtext, gotext) {  
  57.   
  58.         // setup $pager to hold render  
  59.         var $pager = $('<ul class="pages"></ul>');  
  60.   
  61.         // add in the previous and next buttons  
  62.         $pager.append(renderButton('first', firsttext, pagenumber, pagecount, buttonClickCallback)).append(renderButton('prev', prevtext, pagenumber, pagecount, buttonClickCallback));  
  63.   
  64.         // pager currently only handles 10 viewable pages ( could be easily parameterized, maybe in next version ) so handle edge cases  
  65.         var startPoint = 1;  
  66.         var endPoint = 9;  
  67.         if (pagenumber > pagecount) {  
  68.             pagenumber = pagecount;  
  69.         }  
  70.         if (pagenumber > 4) {  
  71.             startPoint = pagenumber - 4;  
  72.             endPoint = pagenumber + 4;  
  73.         }  
  74.   
  75.         if (endPoint > pagecount) {  
  76.             startPoint = pagecount - 8;  
  77.             endPoint = pagecount;  
  78.         }  
  79.   
  80.         if (startPoint < 1) {  
  81.             startPoint = 1;  
  82.         }  
  83.   
  84.         // loop thru visible pages and render buttons  
  85.         for (var page = startPoint; page <= endPoint; page++) {  
  86.   
  87.             var currentButton = $('<li class="page-number">' + (page) + '</li>');  
  88.   
  89.             page == pagenumber ? currentButton.addClass('pgCurrent') : currentButton.click(function() { buttonClickCallback(this.firstChild.data); });  
  90.             currentButton.appendTo($pager);  
  91.         }  
  92.   
  93.         // render in the next and last buttons before returning the whole rendered control back.  
  94.         $pager.append(renderButton('next', nexttext, pagenumber, pagecount, buttonClickCallback)).append(renderButton('last', lasttext, pagenumber, pagecount, buttonClickCallback));  
  95.         if (pagecount > 9) {  
  96.             $pager.append('<li class="pgText"><input tyle="text" class="iptGo" /><span class="spGo">' + gotext + '</span></li>');  
  97.             $('.iptGo', $pager).change(function() {  
  98.                 var num = parseInt($(this).val());  
  99.                 if (num && num > 0) {  
  100.                     if (num > pagecount) {  
  101.                         num = pagecount;  
  102.                     }  
  103.                     $(this).val(num);  
  104.                 }  
  105.                 else {  
  106.                     $(this).val('');  
  107.                 }  
  108.             }).keyup(function() { $(this).change(); });  
  109.             $('.spGo', $pager).click(function() {  
  110.                 var num = $('.iptGo', $pager).val();  
  111.                 if (num != '') {  
  112.                     buttonClickCallback(num);  
  113.                 }  
  114.             });  
  115.         }  
  116.         if (recordtext != '') {  
  117.             $pager.append('<li class="pgText">' + recordtext.replace(/\{0\}/g, pagecount).replace(/\{1\}/g, recordcount) + '</li>');  
  118.         }  
  119.         return $pager;  
  120.     }  
  121.   
  122.     // renders and returns a 'specialized' button, ie 'next', 'previous' etc. rather than a page number button  
  123.     function renderButton(buttonLabel, buttonText, pagenumber, pagecount, buttonClickCallback) {  
  124.   
  125.         var $Button = $('<li class="pgNext">' + buttonText + '</li>');  
  126.   
  127.         var destPage = 1;  
  128.   
  129.         // work out destination page for required button type  
  130.         switch (buttonLabel) {  
  131.             case "first":  
  132.                 destPage = 1;  
  133.                 break;  
  134.             case "prev":  
  135.                 destPage = pagenumber - 1;  
  136.                 break;  
  137.             case "next":  
  138.                 destPage = pagenumber + 1;  
  139.                 break;  
  140.             case "last":  
  141.                 destPage = pagecount;  
  142.                 break;  
  143.         }  
  144.   
  145.         // disable and 'grey' out buttons if not needed.  
  146.         if (buttonLabel == "first" || buttonLabel == "prev") {  
  147.             pagenumber <= 1 ? $Button.addClass('pgEmpty') : $Button.click(function() { buttonClickCallback(destPage); });  
  148.         }  
  149.         else {  
  150.             pagenumber >= pagecount ? $Button.addClass('pgEmpty') : $Button.click(function() { buttonClickCallback(destPage); });  
  151.         }  
  152.   
  153.         return $Button;  
  154.     }  
  155.   
  156.     // pager defaults. hardly worth bothering with in this case but used as placeholder for expansion in the next version  
  157.     $.fn.pager.defaults = {  
  158.         pagenumber: 1,  
  159.         recordcount: 0,  
  160.         pagesize: 10,  
  161.         firsttext: 'first',//显示的第一页文本  
  162.         prevtext: 'prev',//显示的前一页文本  
  163.         nexttext: 'next',//显示的下一页文本  
  164.         lasttext: 'last',//显示的最后一页文本  
  165.         gotext: 'go',//显示的快速跳转文本  
  166.         recordtext: ''//显示记录数,为空时不显示,否则按照占位符显示文本,{0}表示总页数,{1}表示总记录数  
  167.     };  
  168.   
  169. })(jQuery);  

以下是该插件对应的css部分,.jqpager是要应用插件的容器

[css] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. .jqpager ul.pages {  
  2. display:block;  
  3. border:none;  
  4. text-transform:uppercase;  
  5. margin:2px 0 15px 2px;  
  6. padding:0;  
  7. }  
  8.   
  9. .jqpager ul.pages li {  
  10. list-style:none;  
  11. float:left;  
  12. border:1px solid #ccc;  
  13. text-decoration:none;  
  14. margin:0 5px 0 0;  
  15. padding:5px;  
  16. }  
  17.   
  18. .jqpager ul.pages li:hover {  
  19. border:1px solid #003f7e;  
  20. }  
  21.   
  22. .jqpager ul.pages li.pgEmpty {  
  23. border:1px solid #aaa;  
  24. color:#aaa;  
  25. cursor:default;  
  26. }  
  27.   
  28. .jqpager ul.pages li.pgText   
  29. {  
  30.     border:none;  
  31.     cursor:default;  
  32. }  
  33.   
  34. .jqpager ul.pages li.page-number  
  35. {  
  36.     min-width:15px;  
  37.     text-align:center;  
  38. }  
  39.   
  40. .jqpager ul.pages li.pgCurrent {  
  41. border:1px solid #003f7e;  
  42. color:#000;  
  43. font-weight:700;  
  44. background-color:#eee;  
  45. }  
  46.   
  47. .jqpager input.iptGo  
  48. {  
  49.     width:30px;  
  50.     border:1px solid #ccc;  
  51.     margin:-2px 0px 0px 0px;  
  52.     height:18px;  
  53.     vertical-align:middle;  
  54. }  
  55. .jqpager span.spGo  
  56. {  
  57.     cursor:pointer;  
  58.     margin-left:2px;  
  59. }  
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head>    <title>jQuery.pager.js Test</title>    <link href="Pager.css" rel="stylesheet" type="text/css" />    <script src="jquery-1.7.2.min.js" type="text/javascript"></script>    <script src="jquery.pager.js" type="text/javascript"></script>    <script type="text/javascript" language="javascript">        $(document).ready(function() {           $('#pager').pager({ pagenumber: 1,recordcount: 40,pagesize: 10,buttonClickCallback:PageClick,firsttext: '首页',prevtext: '前一页',nexttext: '下一页',lasttext: '尾页',recordtext: '共{0}页,{1}条记录'});        });        PageClick = function(pageclickednumber) {           $('#pager').pager({ pagenumber: pageclickednumber,recordcount: 40,pagesize: 10,buttonClickCallback:PageClick,firsttext: '首页',prevtext: '前一页',nexttext: '下一页',lasttext: '尾页',recordtext: '共{0}页,{1}条记录'});            $("#result").html("Clicked Page " + pageclickednumber);        }           </script></head><body><div class="jqpager"><h1 id="result">Click the pager below.</h1><div id="pager" ></div></div></body></html>



以下是最终的效果



0 0