jquery.pager.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. $.fn.pager = function(options) {
  31. var opts = $.extend({}, $.fn.pager.defaults, options);
  32. return this.each(function() {
  33. // empty out the destination element and then render out the pager with the supplied options
  34. $(this).empty().append(renderpager(parseInt(options.totalcounts),parseInt(options.pagesize),parseInt(options.pagenumber), parseInt(options.pagecount), options.buttonClickCallback));
  35. // specify correct cursor activity
  36. $('.pages li').mouseover(function() { document.body.style.cursor = "pointer"; }).mouseout(function() { document.body.style.cursor = "auto"; });
  37. });
  38. };
  39. // render and return the pager with the supplied options
  40. function renderpager(totalcounts,pagesize,pagenumber, pagecount, buttonClickCallback) {
  41. var ispgEmpty = ""; //如果只有一页,就不展示分页菜单,只居中展示共多少条记录
  42. if(pagecount <= 1 || totalcounts == 0){
  43. //ispgEmpty = "pgEmpty";
  44. ispgEmpty = "style='width:160px; margin:0 auto;'";
  45. //$(".pager_container").css("width","150px");
  46. } else if(pagecount ==2 ){
  47. //ispgEmpty = "pgEmpty";
  48. ispgEmpty = "style='width:300px; margin:0 auto;'";
  49. //$(".pager_container").css("width","150px");
  50. }else{
  51. ///$(".pager_container").css("width","");
  52. ispgEmpty = "";
  53. }
  54. // setup $pager to hold render
  55. //var $pager = $("<ul class='pages "+ispgEmpty+"'></ul>");
  56. //var $pgHeader = $("<p class='pgHeader'><span class="+ispgEmpty+">转到</span></p>");
  57. var $pager = $("<ul class='pages' "+ispgEmpty+"></ul>");
  58. var $pgHeader = $("<p class='pgHeader'><span>转到</span></p>");
  59. // add in the previous and next buttons
  60. $pager.append(renderButton('《', totalcounts,pagesize,pagenumber, pagecount, buttonClickCallback)).append(renderButton('上一页', totalcounts,pagesize,pagenumber, pagecount, buttonClickCallback));
  61. // pager currently only handles 10 viewable pages ( could be easily parameterized, maybe in next version ) so handle edge cases
  62. var startPoint = 1;
  63. var endPoint = 5;
  64. if (pagenumber > 2) {
  65. startPoint = pagenumber - 2;
  66. endPoint = pagenumber + 2;
  67. }
  68. if (endPoint > pagecount) {
  69. startPoint = pagecount - 4;
  70. endPoint = pagecount;
  71. }
  72. if (startPoint < 1) {
  73. startPoint = 1;
  74. }
  75. // loop thru visible pages and render buttons
  76. for (var page = startPoint; page <= endPoint; page++) {
  77. var currentButton = $('<li class="page-number">' + (page) + '</li>');
  78. page == pagenumber ? currentButton.addClass('pgCurrent') : currentButton.click(function() { buttonClickCallback(this.firstChild.data); });
  79. currentButton.appendTo($pager);
  80. }
  81. // render in the next and last buttons before returning the whole rendered control back.
  82. $pager.append(renderButton('下一页', totalcounts,pagesize,pagenumber, pagecount, buttonClickCallback)).append(renderButton('》', totalcounts,pagesize,pagenumber, pagecount, buttonClickCallback));
  83. if($("#pagesize").val()==null){
  84. $pgHeader.append( renderGotoPagenum(pagenumber,ispgEmpty,totalcounts,pagesize,pagenumber, pagecount, buttonClickCallback))
  85. .append("<span class="+ispgEmpty+">页</span><span class="+ispgEmpty+">&nbsp;/&nbsp;共&nbsp;"+pagecount +"&nbsp;页&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;每页显示&nbsp;</span>")
  86. .append("<font class="+ispgEmpty+">"+pagesize+"</font><span class="+ispgEmpty+">条</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;共&nbsp;<font class=\"dis\">"+totalcounts+"</font>&nbsp;条");
  87. }else{
  88. $pgHeader.append( renderGotoPagenum(pagenumber,ispgEmpty,totalcounts,pagesize,pagenumber, pagecount, buttonClickCallback))
  89. .append("<span class="+ispgEmpty+">页</span><span class="+ispgEmpty+">&nbsp;/&nbsp;共&nbsp;"+pagecount +"&nbsp;页&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;每页显示&nbsp;</span>")
  90. .append(renderSelectCount( totalcounts,ispgEmpty ))
  91. .append("</font><span class="+ispgEmpty+">条</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;共&nbsp;<font class=\"dis\">"+totalcounts+"</font>&nbsp;条");
  92. }
  93. return $pgHeader.before($pager);
  94. }
  95. // renders and returns a 'specialized' button, ie 'next', 'previous' etc. rather than a page number button
  96. function renderButton(buttonLabel, totalcounts,pagesize,pagenumber, pagecount, buttonClickCallback) {
  97. var $Button = $('<li class="pgNext">' + buttonLabel + '</li>');
  98. var destPage = 1;
  99. // work out destination page for required button type
  100. switch (buttonLabel) {
  101. case "《":
  102. destPage = 1;
  103. break;
  104. case "上一页":
  105. destPage = pagenumber - 1;
  106. break;
  107. case "下一页":
  108. destPage = pagenumber + 1;
  109. break;
  110. case "》":
  111. destPage = pagecount;
  112. break;
  113. }
  114. // disable and 'grey' out buttons if not needed.
  115. if (buttonLabel == "《" || buttonLabel == "上一页") {
  116. pagenumber <= 1 ? $Button.addClass('pgEmpty') : $Button.click(function() { buttonClickCallback(destPage); });
  117. }
  118. else {
  119. pagenumber >= pagecount ? $Button.addClass('pgEmpty') : $Button.click(function() { buttonClickCallback(destPage); });
  120. }
  121. return $Button;
  122. }
  123. //根据输入的input的值,转到某个页面
  124. function renderGotoPagenum(gopagenumber, ispgEmpty, totalcounts, pagesize, pagenumber, pagecount, buttonClickCallback) {
  125. var $Input = $("<input id='gopagenum' type='text' value='"+gopagenumber+"' class='"+ispgEmpty+"'>");
  126. $Input.change(function() {
  127. var gonum=$('#gopagenum').val();
  128. var num_reg = /^[0-9]*$/; //必须为数字
  129. if(gonum!="" & num_reg.test(gonum)){
  130. gopagenumber = gonum;
  131. }else{
  132. $('#gopagenum').css({"color":"#555","font-size":"11px"});
  133. $('#gopagenum').val("输入数字");
  134. return false;
  135. }
  136. if(gonum <= 1){
  137. gopagenumber = 1;
  138. }else if(gonum >= pagecount){
  139. gopagenumber = pagecount;
  140. }else{
  141. gopagenumber = gonum;
  142. }
  143. buttonClickCallback(gopagenumber);
  144. });
  145. $Input.focus(function() { $('#gopagenum').val(""); $('#gopagenum').css("color","#f03800"); });
  146. return $Input;
  147. }
  148. //初始化select的值:每页显示该值的数目列表内容
  149. function renderSelectCount(totalcounts, ispgEmpty) {
  150. var $Select = $('<select id=\"selectcount\" class="'+ispgEmpty+'"></select>');
  151. $Select.empty();
  152. //添加下拉框的option
  153. var options = "";
  154. var size = $("#defaultsize").val();
  155. var pagesize=size;
  156. var selectval = $("#pagesize").val();
  157. for(var i=2; i<=8; i++) {
  158. if(selectval==pagesize){
  159. options += "<option value="+pagesize+" selected>"+pagesize+"</option>";
  160. }else{
  161. options += "<option value="+pagesize+">"+pagesize+"</option>";
  162. }
  163. if( totalcounts < pagesize ){ //如果列表总数小于下拉列表值,其他的option则不显示
  164. pagesize=totalcounts;
  165. break;
  166. }else{
  167. pagesize=size*(i+1); //这里控制下拉列表的值的大小
  168. }
  169. }
  170. $Select.append(options);
  171. //改变下拉列表的值
  172. $Select.change(function() {
  173. selectval = $Select.find("option:selected").val();
  174. $("#pagesize").val(selectval);
  175. //控制跳转的页是否存在
  176. var pagenum = $('#gopagenum').val();
  177. if( totalcounts%selectval == 0 ){
  178. pagenum = totalcounts/selectval;
  179. }else{
  180. pagenum = parseInt(totalcounts/selectval)+1;
  181. }
  182. if( $('#pagenumber').val() > pagenum ){
  183. $('#pagenumber').val(pagenum);
  184. }
  185. init();
  186. });
  187. return $Select;
  188. }
  189. // pager defaults. hardly worth bothering with in this case but used as placeholder for expansion in the next version
  190. $.fn.pager.defaults = {
  191. totalcounts:1,
  192. pagesize:1,
  193. pagenumber: 1,
  194. pagecount: 1
  195. };
  196. })(jQuery);