pagination.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * jQuery Pagination
  3. * Author: Austin Wulf (@austinwulf)
  4. *
  5. * Call the paginate method on an array
  6. * of elements. Accepts # of items per page
  7. * as an argument. Defaults to 5.
  8. *
  9. * Example:
  10. * $(selector).paginate(3);
  11. *
  12. * Released under the MIT License.
  13. *
  14. * v 1.0
  15. */
  16. (function($){
  17. var paginate = {
  18. startPos: function(pageNumber, perPage) {
  19. // determine what array position to start from
  20. // based on current page and # per page
  21. return pageNumber * perPage;
  22. },
  23. getPage: function(items, startPos, perPage) {
  24. // declare an empty array to hold our page items
  25. var page = [];
  26. // only get items after the starting position
  27. items = items.slice(startPos, items.length);
  28. // loop remaining items until max per page
  29. for (var i=0; i < perPage; i++) {
  30. page.push(items[i]); }
  31. return page;
  32. },
  33. totalPages: function(items, perPage) {
  34. // determine total number of pages
  35. return Math.ceil(items.length / perPage);
  36. },
  37. createBtns: function(totalPages, currentPage) {
  38. // create buttons to manipulate current page
  39. var pagination = $('<div class="pagination" />');
  40. // add a "first" button
  41. pagination.append('<span class="pagination-button">&laquo;</span>');
  42. // add pages inbetween
  43. for (var i=1; i <= totalPages; i++) {
  44. // truncate list when too large
  45. if (totalPages > 5 && currentPage !== i) {
  46. // if on first two pages
  47. if (currentPage === 1 || currentPage === 2) {
  48. // show first 5 pages
  49. if (i > 5) continue;
  50. // if on last two pages
  51. } else if (currentPage === totalPages || currentPage === totalPages - 1) {
  52. // show last 5 pages
  53. if (i < totalPages - 4) continue;
  54. // otherwise show 5 pages w/ current in middle
  55. } else {
  56. if (i < currentPage - 2 || i > currentPage + 2) {
  57. continue; }
  58. }
  59. }
  60. // markup for page button
  61. var pageBtn = $('<span class="pagination-button page-num" />');
  62. // add active class for current page
  63. if (i == currentPage) {
  64. pageBtn.addClass('active'); }
  65. // set text to the page number
  66. pageBtn.text(i);
  67. // add button to the container
  68. pagination.append(pageBtn);
  69. }
  70. // add a "last" button
  71. pagination.append($('<span class="pagination-button">&raquo;</span>'));
  72. return pagination;
  73. },
  74. createPage: function(items, currentPage, perPage) {
  75. // remove pagination from the page
  76. $('.pagination').remove();
  77. // set context for the items
  78. var container = items.parent(),
  79. // detach items from the page and cast as array
  80. items = items.detach().toArray(),
  81. // get start position and select items for page
  82. startPos = this.startPos(currentPage - 1, perPage),
  83. page = this.getPage(items, startPos, perPage);
  84. // loop items and readd to page
  85. $.each(page, function(){
  86. // prevent empty items that return as Window
  87. if (this.window === undefined) {
  88. container.append($(this)); }
  89. });
  90. // prep pagination buttons and add to page
  91. var totalPages = this.totalPages(items, perPage),
  92. pageButtons = this.createBtns(totalPages, currentPage);
  93. container.after(pageButtons);
  94. }
  95. };
  96. // stuff it all into a jQuery method!
  97. $.fn.paginate = function(perPage) {
  98. var items = $(this);
  99. // default perPage to 5
  100. if (isNaN(perPage) || perPage === undefined) {
  101. perPage = 5; }
  102. // don't fire if fewer items than perPage
  103. if (items.length <= perPage) {
  104. return true; }
  105. // ensure items stay in the same DOM position
  106. if (items.length !== items.parent()[0].children.length) {
  107. items.wrapAll('<div class="pagination-items" />');
  108. }
  109. // paginate the items starting at page 1
  110. paginate.createPage(items, 1, perPage);
  111. // handle click events on the buttons
  112. $(document).on('click', '.pagination-button', function(e) {
  113. // get current page from active button
  114. var currentPage = parseInt($('.pagination-button.active').text(), 10),
  115. newPage = currentPage,
  116. totalPages = paginate.totalPages(items, perPage),
  117. target = $(e.target);
  118. // get numbered page
  119. newPage = parseInt(target.text(), 10);
  120. if (target.text() == '«') newPage = 1;
  121. if (target.text() == '»') newPage = totalPages;
  122. // ensure newPage is in available range
  123. if (newPage > 0 && newPage <= totalPages) {
  124. paginate.createPage(items, newPage, perPage); }
  125. });
  126. };
  127. })(jQuery);
  128. /* This part is just for the demo,
  129. not actually part of the plugin */
  130. $('.article-loop').paginate(2);