pagination.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. define(['jquery'], function ($) {
  2. var Pagination = function (options) {
  3. // 分页容器
  4. this.elem = $(options.elem || 'body');
  5. // 记录总数
  6. this.total = options.total || 0;
  7. // 每页显示最大记录数
  8. this.size = options.size || 10;
  9. // 当前页码
  10. this.number = options.number || 1;
  11. // 分页切换事件
  12. this.callback = options.callback || function (number) {
  13. console.info('current page is %d', number);
  14. };
  15. // 计算页码总数
  16. this.count = Math.ceil(this.total / this.size);
  17. if (this.count === 1) {
  18. this.elem.find('>.pagination').remove();
  19. return false;
  20. }
  21. // 初始化
  22. this.init();
  23. };
  24. Pagination.prototype = {
  25. // 初始化
  26. init: function () {
  27. // 追加分页到容器中
  28. this.elem.find('>.pagination').remove();
  29. this.elem.append('<div class="pagination"></div>');
  30. this.outer = this.elem.find('.pagination');
  31. this.renderPage();
  32. this.changePage();
  33. },
  34. firstPage: '<a href="javascript:;" title="首页" class="pagination_item gc_body_dw btn-page" to="f">首页</a>',
  35. prevPage: '<a href="javascript:;" title="上一页" class="pagination_item gc_body_dw btn-page" to="p">上一页</a>',
  36. nextPage: '<a href="javascript:;" title="下一页" class="pagination_item gc_body_dw btn-page" to="n">下一页</a>',
  37. lastPage: '<a href="javascript:;" title="尾页" class="pagination_item gc_body_dw btn-page" to="l">尾页</a>',
  38. renderPage: function () {
  39. this.outer.html('');
  40. if (this.number > 1) {
  41. this.outer.append(this.firstPage);
  42. this.outer.append(this.prevPage);
  43. }
  44. var start = this.number - 2;
  45. start = start < 1 ? 1 : start;
  46. var end = start + 5;
  47. if (end > this.count) {
  48. end = this.count + 1;
  49. start = end - 5 < 1 ? 1 : end - 5;
  50. }
  51. for (var i = start; i < end; i ++) {
  52. var cls = this.number === i ? ' active' : '';
  53. this.outer.append('<a href="javascript:;" title="第' + i + '页" class="pagination_item' + cls + '" to="' + i + '">' + i + '</a>');
  54. }
  55. if (end > 5 && start < this.count - 4) {
  56. this.outer.append('<a>...</a>');
  57. }
  58. if (this.number < this.count) {
  59. this.outer.append(this.nextPage);
  60. this.outer.append(this.lastPage);
  61. }
  62. },
  63. changePage: function () {
  64. this.outer.on('click', '.pagination_item', function (event) {
  65. var toPage = $(event.target).attr('to');
  66. switch (toPage) {
  67. case 'f':
  68. this.number = 1;
  69. break;
  70. case 'p':
  71. this.number -= 1;
  72. break;
  73. case 'n':
  74. this.number += 1;
  75. break;
  76. case 'l':
  77. this.number = this.count;
  78. break;
  79. default:
  80. if (this.number === toPage * 1) {
  81. return false
  82. }
  83. this.number = toPage * 1;
  84. }
  85. this.renderPage();
  86. this.callback(this.number);
  87. }.bind(this));
  88. }
  89. }
  90. return function (options) {
  91. new Pagination(options);
  92. };
  93. });