Built files from Bizgaze WebServer
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

jquery.pagination.js 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. jQuery.fn.pagination = function (maxentries, itemsperpage, opts) {
  2. opts = jQuery.extend({
  3. items_per_page: itemsperpage,
  4. num_display_entries: 4,
  5. current_page: 0,
  6. num_edge_entries: 0,
  7. link_to: "javascript:void(0)",
  8. prev_text: "Prev",
  9. next_text: "Next",
  10. ellipse_text: "...",
  11. prev_show_always: true,
  12. next_show_always: true,
  13. callback: function () { return false; }
  14. }, opts || {});
  15. return this.each(function () {
  16. /**
  17. * Calculate the maximum number of pages
  18. */
  19. function numPages() {
  20. return Math.ceil(maxentries / opts.items_per_page);
  21. }
  22. /**
  23. * Calculate start and end point of pagination links depending on
  24. * current_page and num_display_entries.
  25. * @return {Array}
  26. */
  27. function getInterval() {
  28. var ne_half = Math.ceil(opts.num_display_entries / 2);
  29. var np = numPages();
  30. var upper_limit = np - opts.num_display_entries;
  31. var start = current_page > ne_half ? Math.max(Math.min(current_page - ne_half, upper_limit), 0) : 0;
  32. var end = current_page > ne_half ? Math.min(current_page + ne_half, np) : Math.min(opts.num_display_entries, np);
  33. return [start, end];
  34. }
  35. /**
  36. * This is the event handling function for the pagination links.
  37. * @param {int} page_id The new page number
  38. */
  39. function pageSelected(page_id, evt) {
  40. current_page = page_id;
  41. drawLinks();
  42. var continuePropagation = opts.callback(page_id, panel);
  43. if (!continuePropagation) {
  44. if (evt.stopPropagation) {
  45. evt.stopPropagation();
  46. }
  47. else {
  48. evt.cancelBubble = true;
  49. }
  50. }
  51. return continuePropagation;
  52. }
  53. /**
  54. * This function inserts the pagination links into the container element
  55. */
  56. function drawLinks() {
  57. panel.empty();
  58. var list = jQuery("<ul class='" + panel.attr("class") + "'></ul>");
  59. panel.append(list);
  60. var interval = getInterval();
  61. var np = numPages();
  62. // This helper function returns a handler function that calls pageSelected with the right page_id
  63. var getClickHandler = function (page_id) {
  64. return function (evt) { return pageSelected(page_id, evt); }
  65. }
  66. // Helper function for generating a single link (or a span tag if it's the current page)
  67. var appendItem = function (page_id, appendopts) {
  68. page_id = page_id < 0 ? 0 : (page_id < np ? page_id : np - 1); // Normalize page id to sane value
  69. appendopts = jQuery.extend({ text: page_id + 1, classes: "" }, appendopts || {});
  70. if (page_id == current_page) {
  71. var clazz = appendopts.side ? 'disabled' : 'active';
  72. var lstItem = jQuery("<li class='" + clazz + " page-item'><a class='page-link'>" + (appendopts.text) + "</a></li>")
  73. }
  74. else {
  75. var a = jQuery("<a class='page-link'>" + (appendopts.text) + "</a>")
  76. .attr('href', opts.link_to.replace(/__id__/, page_id));;
  77. var lstItem = jQuery("<li class='page-item'></li>")
  78. .bind("click", getClickHandler(page_id));
  79. lstItem.append(a);
  80. }
  81. if (appendopts.classes) { lstItem.addClass(appendopts.classes); }
  82. list.append(lstItem);
  83. }
  84. // Generate "Previous"-Link
  85. if (opts.prev_text && (current_page > 0 || opts.prev_show_always)) {
  86. appendItem(current_page - 1, { text: opts.prev_text, side: true });
  87. }
  88. // Generate starting points
  89. if (interval[0] > 0 && opts.num_edge_entries > 0) {
  90. var end = Math.min(opts.num_edge_entries, interval[0]);
  91. for (var i = 0; i < end; i++) {
  92. appendItem(i);
  93. }
  94. if (opts.num_edge_entries < interval[0] && opts.ellipse_text) {
  95. jQuery("<li class='disabled page-item'>" + opts.ellipse_text + "</li>").appendTo(list);
  96. }
  97. }
  98. // Generate interval links
  99. for (var i = interval[0]; i < interval[1]; i++) {
  100. appendItem(i);
  101. }
  102. // Generate ending points
  103. if (interval[1] < np && opts.num_edge_entries > 0) {
  104. if (np - opts.num_edge_entries > interval[1] && opts.ellipse_text) {
  105. jQuery("<li class='disabled page-item'>" + opts.ellipse_text + "</li>").appendTo(list);
  106. }
  107. var begin = Math.max(np - opts.num_edge_entries, interval[1]);
  108. for (var i = begin; i < np; i++) {
  109. appendItem(i);
  110. }
  111. }
  112. // Generate "Next"-Link
  113. if (opts.next_text && (current_page < np - 1 || opts.next_show_always)) {
  114. appendItem(current_page + 1, { text: opts.next_text, side: true });
  115. }
  116. }
  117. // Extract current_page from options
  118. var current_page = opts.current_page;
  119. // Create a sane value for maxentries and items_per_page
  120. maxentries = (!maxentries || maxentries < 0) ? 1 : maxentries;
  121. opts.items_per_page = (!opts.items_per_page || opts.items_per_page < 0) ? 1 : opts.items_per_page;
  122. // Store DOM element for easy access from all inner functions
  123. var panel = jQuery(this);
  124. // Attach control functions to the DOM element
  125. this.selectPage = function (page_id) { pageSelected(page_id); }
  126. this.prevPage = function () {
  127. if (current_page > 0) {
  128. pageSelected(current_page - 1);
  129. return true;
  130. }
  131. else {
  132. return false;
  133. }
  134. }
  135. this.nextPage = function () {
  136. if (current_page < numPages() - 1) {
  137. pageSelected(current_page + 1);
  138. return true;
  139. }
  140. else {
  141. return false;
  142. }
  143. }
  144. // When all initialisation is done, draw the links
  145. drawLinks();
  146. // call callback function
  147. //opts.callback(current_page, this);
  148. });
  149. }