ms.page.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. //后台分页js
  2. (function($){
  3. var methods = {
  4. init: function(options) {
  5. var o = $.extend({
  6. items: 1,//总数量
  7. itemsOnPage: 1,//每页显示数量
  8. pages: 1,//总页数
  9. displayedPages: 5,//显示页数
  10. edges: 3,//边界显示页数
  11. currentPage: 1,
  12. hrefTextPrefix: '#page-',//分页链接地址的的前缀
  13. hrefTextSuffix: '',
  14. prevText: '上一页',//上一页显示文字
  15. nextText: '下一页',
  16. ellipseText: '…',
  17. cssStyle: 'light-theme',//分页使用的样式
  18. labelMap: [],//分页显示的信息
  19. selectOnClick: true,
  20. onPageClick: function(pageNumber, event) {
  21. //点击分页进行的操作
  22. },
  23. onInit: function() {
  24. //初始化时进行的操作
  25. }
  26. }, options || {});
  27. var self = this;
  28. o.pages = o.pages ? o.pages : Math.ceil(o.items / o.itemsOnPage) ? Math.ceil(o.items / o.itemsOnPage) : 1;
  29. o.currentPage = o.currentPage - 1;
  30. o.halfDisplayed = o.displayedPages / 2;
  31. this.each(function() {
  32. self.addClass(o.cssStyle).data('pagination', o);
  33. methods._draw.call(self);
  34. });
  35. o.onInit();
  36. return this;
  37. },
  38. selectPage: function(page) {
  39. methods._selectPage.call(this, page - 1);
  40. return this;
  41. },
  42. prevPage: function() {
  43. var o = this.data('pagination');
  44. if (o.currentPage > 0) {
  45. methods._selectPage.call(this, o.currentPage - 1);
  46. }
  47. return this;
  48. },
  49. nextPage: function() {
  50. var o = this.data('pagination');
  51. if (o.currentPage < o.pages - 1) {
  52. methods._selectPage.call(this, o.currentPage + 1);
  53. }
  54. return this;
  55. },
  56. getPagesCount: function() {
  57. return this.data('pagination').pages;
  58. },
  59. getCurrentPage: function () {
  60. return this.data('pagination').currentPage + 1;
  61. },
  62. destroy: function(){
  63. this.empty();
  64. return this;
  65. },
  66. drawPage: function (page) {
  67. var o = this.data('pagination');
  68. o.currentPage = page - 1;
  69. this.data('pagination', o);
  70. methods._draw.call(this);
  71. return this;
  72. },
  73. redraw: function(){
  74. methods._draw.call(this);
  75. return this;
  76. },
  77. disable: function(){
  78. var o = this.data('pagination');
  79. o.disabled = true;
  80. this.data('pagination', o);
  81. methods._draw.call(this);
  82. return this;
  83. },
  84. enable: function(){
  85. var o = this.data('pagination');
  86. o.disabled = false;
  87. this.data('pagination', o);
  88. methods._draw.call(this);
  89. return this;
  90. },
  91. updateItems: function (newItems) {
  92. var o = this.data('pagination');
  93. o.items = newItems;
  94. o.pages = methods._getPages(o);
  95. this.data('pagination', o);
  96. methods._draw.call(this);
  97. },
  98. updateItemsOnPage: function (itemsOnPage) {
  99. var o = this.data('pagination');
  100. o.itemsOnPage = itemsOnPage;
  101. o.pages = methods._getPages(o);
  102. this.data('pagination', o);
  103. methods._selectPage.call(this, 0);
  104. return this;
  105. },
  106. _draw: function() {
  107. var o = this.data('pagination'),
  108. interval = methods._getInterval(o),
  109. i,
  110. tagName;
  111. methods.destroy.call(this);
  112. tagName = (typeof this.prop === 'function') ? this.prop('tagName') : this.attr('tagName');
  113. var $panel = tagName === 'UL' ? this : $('<ul></ul>').appendTo(this);
  114. //上一页
  115. if (o.prevText) {
  116. methods._appendItem.call(this, o.currentPage - 1, {text: o.prevText, classes: 'prev'});
  117. }
  118. // 开始位置的分页
  119. if (interval.start > 0 && o.edges > 0) {
  120. var end = Math.min(o.edges, interval.start);
  121. for (i = 0; i < end; i++) {
  122. methods._appendItem.call(this, i);
  123. }
  124. if (o.edges < interval.start && (interval.start - o.edges != 1)) {
  125. $panel.append('<li class="disabled"><a>' + o.ellipseText + '</a></span></li>');
  126. } else if (interval.start - o.edges == 1) {
  127. methods._appendItem.call(this, o.edges);
  128. }
  129. }
  130. // 中间段的分页显示
  131. for (i = interval.start; i < interval.end; i++) {
  132. methods._appendItem.call(this, i);
  133. }
  134. //结束的分页显示
  135. if (interval.end < o.pages && o.edges > 0) {
  136. if (o.pages - o.edges > interval.end && (o.pages - o.edges - interval.end != 1)) {
  137. $panel.append('<li class="disabled"><a>' + o.ellipseText + '</a></li>');
  138. } else if (o.pages - o.edges - interval.end == 1) {
  139. methods._appendItem.call(this, interval.end++);
  140. }
  141. var begin = Math.max(o.pages - o.edges, interval.end);
  142. for (i = begin; i < o.pages; i++) {
  143. methods._appendItem.call(this, i);
  144. }
  145. }
  146. // Generate Next link
  147. if (o.nextText) {
  148. methods._appendItem.call(this, o.currentPage + 1, {text: o.nextText, classes: 'next'});
  149. }
  150. },
  151. //获取总页数
  152. _getPages: function(o) {
  153. var pages = Math.ceil(o.items / o.itemsOnPage);
  154. return pages || 1;
  155. },
  156. //获取中间显示的页数
  157. _getInterval: function(o) {
  158. return {
  159. start: Math.ceil(o.currentPage > o.halfDisplayed ? Math.max(Math.min(o.currentPage - o.halfDisplayed, (o.pages - o.displayedPages)), 0) : 0),
  160. end: Math.ceil(o.currentPage > o.halfDisplayed ? Math.min(o.currentPage + o.halfDisplayed, o.pages) : Math.min(o.displayedPages, o.pages))
  161. };
  162. },
  163. _appendItem: function(pageIndex, opts) {
  164. var self = this, options, $link, o = self.data('pagination'), $linkWrapper = $('<li></li>'), $ul = self.find('ul');
  165. pageIndex = pageIndex < 0 ? 0 : (pageIndex < o.pages ? pageIndex : o.pages - 1);
  166. options = {
  167. text: pageIndex + 1,
  168. classes: ''
  169. };
  170. if (o.labelMap.length && o.labelMap[pageIndex]) {
  171. options.text = o.labelMap[pageIndex];
  172. }
  173. options = $.extend(options, opts || {});
  174. if (pageIndex == o.currentPage || o.disabled) {
  175. if (o.disabled) {
  176. $linkWrapper.addClass('disabled');
  177. } else {
  178. if (!options.classes) {
  179. $linkWrapper.addClass('active');
  180. }
  181. }
  182. $link = $('<a target="_self">' + (options.text) + '</a>');
  183. } else {
  184. $link = $('<a target="_self" href="' + o.hrefTextPrefix + (pageIndex + 1) + o.hrefTextSuffix + '" class="page-link">' + (options.text) + '</a>');
  185. $link.click(function(event){
  186. return methods._selectPage.call(self, pageIndex, event);
  187. });
  188. }
  189. if (options.classes) {
  190. $link.addClass(options.classes);
  191. }
  192. $linkWrapper.append($link);
  193. if ($ul.length) {
  194. $ul.append($linkWrapper);
  195. } else {
  196. self.append($linkWrapper);
  197. }
  198. },
  199. _selectPage: function(pageIndex, event) {
  200. var o = this.data('pagination');
  201. o.currentPage = pageIndex;
  202. if (o.selectOnClick) {
  203. methods._draw.call(this);
  204. }
  205. return o.onPageClick(pageIndex + 1, event);
  206. }
  207. };
  208. $.fn.pagination = function(method) {
  209. // Method calling logic
  210. if (methods[method] && method.charAt(0) != '_') {
  211. return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
  212. } else if (typeof method === 'object' || !method) {
  213. return methods.init.apply(this, arguments);
  214. } else {
  215. $.error('Method ' + method + ' does not exist on jQuery.pagination');
  216. }
  217. };
  218. })(jQuery);