No Description

paging.js 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. (function($, window, document, undefined) {
  2. //定义分页类
  3. function Paging(element, options) {
  4. this.element = element;
  5. //传入形参
  6. this.options = {
  7. pageNo: options.pageNo||1,
  8. totalPage: options.totalPage,
  9. totalSize:options.totalSize,
  10. callback:options.callback
  11. };
  12. //根据形参初始化分页html和css代码
  13. this.init();
  14. }
  15. //对Paging的实例对象添加公共的属性和方法
  16. Paging.prototype = {
  17. constructor: Paging,
  18. init: function() {
  19. this.creatHtml();
  20. this.bindEvent();
  21. },
  22. creatHtml: function() {
  23. var me = this;
  24. var content = "";
  25. var current = me.options.pageNo;
  26. var total = me.options.totalPage;
  27. var totalNum = me.options.totalSize;
  28. content += "<a id=\"firstPage\">首页</a><a id='prePage'>上一页</a>";
  29. //总页数大于6时候
  30. if(total > 6) {
  31. //当前页数小于5时显示省略号
  32. if(current < 5) {
  33. for(var i = 1; i < 6; i++) {
  34. if(current == i) {
  35. content += "<a class='current'>" + i + "</a>";
  36. } else {
  37. content += "<a>" + i + "</a>";
  38. }
  39. }
  40. content += ". . .";
  41. content += "<a>"+total+"</a>";
  42. } else {
  43. //判断页码在末尾的时候
  44. if(current < total - 3) {
  45. for(var i = current - 2; i < current + 3; i++) {
  46. if(current == i) {
  47. content += "<a class='current'>" + i + "</a>";
  48. } else {
  49. content += "<a>" + i + "</a>";
  50. }
  51. }
  52. content += ". . .";
  53. content += "<a>"+total+"</a>";
  54. //页码在中间部分时候
  55. } else {
  56. content += "<a>1</a>";
  57. content += ". . .";
  58. for(var i = total - 4; i < total + 1; i++) {
  59. if(current == i) {
  60. content += "<a class='current'>" + i + "</a>";
  61. } else {
  62. content += "<a>" + i + "</a>";
  63. }
  64. }
  65. }
  66. }
  67. //页面总数小于6的时候
  68. } else {
  69. for(var i = 1; i < total + 1; i++) {
  70. if(current == i) {
  71. content += "<a class='current'>" + i + "</a>";
  72. } else {
  73. content += "<a>" + i + "</a>";
  74. }
  75. }
  76. }
  77. content += "<a id='nextPage'>下一页</a>";
  78. content += "<a id=\"lastPage\">尾页</a>";
  79. content += "<span class='totalPages'> 共<span>"+total+"</span>页 </span>";
  80. content += "<span class='totalSize'> 共<span>"+totalNum+"</span>条记录 </span>";
  81. me.element.html(content);
  82. },
  83. //添加页面操作事件
  84. bindEvent: function() {
  85. var me = this;
  86. me.element.off('click', 'a');
  87. me.element.on('click', 'a', function() {
  88. var num = $(this).html();
  89. var id=$(this).attr("id");
  90. if(id == "prePage") {
  91. if(me.options.pageNo == 1) {
  92. me.options.pageNo = 1;
  93. } else {
  94. me.options.pageNo = +me.options.pageNo - 1;
  95. }
  96. } else if(id == "nextPage") {
  97. if(me.options.pageNo == me.options.totalPage) {
  98. me.options.pageNo = me.options.totalPage
  99. } else {
  100. me.options.pageNo = +me.options.pageNo + 1;
  101. }
  102. } else if(id =="firstPage") {
  103. me.options.pageNo = 1;
  104. } else if(id =="lastPage") {
  105. me.options.pageNo = me.options.totalPage;
  106. }else{
  107. me.options.pageNo = +num;
  108. }
  109. me.creatHtml();
  110. if(me.options.callback) {
  111. me.options.callback(me.options.pageNo);
  112. }
  113. });
  114. }
  115. };
  116. //通过jQuery对象初始化分页对象
  117. $.fn.paging = function(options) {
  118. return new Paging($(this), options);
  119. }
  120. })(jQuery, window, document);