pages.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. (function ($) {
  2. //修改地址id
  3. function pageurl(id) {
  4. var urlSearch = location.href;
  5. var unformatStr = changeURLArg(urlSearch, "page", id);
  6. if (!!(window.history && history.pushState)) {
  7. history.replaceState(null, "", unformatStr);
  8. }
  9. }
  10. function changeURLArg(url, arg, arg_val) {
  11. var pattern = arg + '=([^&]*)';
  12. var replaceText = arg + '=' + arg_val;
  13. if (url.match(pattern)) {
  14. var tmp = '/(' + arg + '=)([^&]*)/gi';
  15. tmp = url.replace(eval(tmp), replaceText);
  16. return tmp;
  17. } else {
  18. if (url.match('[\?]')) {
  19. return url + '&' + replaceText;
  20. } else {
  21. return url + '?' + replaceText;
  22. }
  23. }
  24. }
  25. function getUrlParms(name) {
  26. var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
  27. var r = window.location.search.substr(1).match(reg);
  28. if (r != null)
  29. return unescape(r[2]);
  30. return "1";
  31. }
  32. //获取地址id
  33. var getid = getUrlParms("page");
  34. var methods = {
  35. init: function (options) {
  36. var o = $.extend({
  37. items: 1,
  38. itemsOnPage: 1,
  39. pages: 0,
  40. displayedPages: 5,
  41. edges: '',
  42. currentPage: getid,
  43. prevText: '上一页',
  44. nextText: '下一页',
  45. ellipseText: '…',
  46. cssStyle: '',
  47. labelMap: [],
  48. selectOnClick: true,
  49. onPageClick: function (pageNumber, event) {
  50. //点击加载
  51. },
  52. onInit: function (getid) {
  53. //初始页面
  54. }
  55. }, options || {});
  56. var self = this;
  57. o.pages = o.pages ? o.pages : Math.ceil(o.items / o.itemsOnPage) ? Math.ceil(o.items / o.itemsOnPage) : 1;
  58. o.currentPage = o.currentPage - 1;
  59. o.halfDisplayed = o.displayedPages / 2;
  60. this.each(function () {
  61. self.addClass(o.cssStyle + ' simple-pagination').data('pagination', o);
  62. methods._draw.call(self);
  63. });
  64. o.onInit(getid);
  65. return this;
  66. },
  67. selectPage: function (page) {
  68. methods._selectPage.call(this, page - 1);
  69. return this;
  70. },
  71. prevPage: function () {
  72. var o = this.data('pagination');
  73. if (o.currentPage > 0) {
  74. methods._selectPage.call(this, o.currentPage - 1);
  75. }
  76. return this;
  77. },
  78. nextPage: function () {
  79. var o = this.data('pagination');
  80. if (o.currentPage < o.pages - 1) {
  81. methods._selectPage.call(this, o.currentPage + 1);
  82. }
  83. return this;
  84. },
  85. getPagesCount: function () {
  86. return this.data('pagination').pages;
  87. },
  88. getCurrentPage: function () {
  89. return this.data('pagination').currentPage + 1;
  90. },
  91. destroy: function () {
  92. this.empty();
  93. return this;
  94. },
  95. drawPage: function (page) {
  96. var o = this.data('pagination');
  97. o.currentPage = page - 1;
  98. this.data('pagination', o);
  99. methods._draw.call(this);
  100. return this;
  101. },
  102. redraw: function () {
  103. methods._draw.call(this);
  104. return this;
  105. },
  106. disable: function () {
  107. var o = this.data('pagination');
  108. o.disabled = true;
  109. this.data('pagination', o);
  110. methods._draw.call(this);
  111. return this;
  112. },
  113. enable: function () {
  114. var o = this.data('pagination');
  115. o.disabled = false;
  116. this.data('pagination', o);
  117. methods._draw.call(this);
  118. return this;
  119. },
  120. updateItems: function (newItems) {
  121. var o = this.data('pagination');
  122. o.items = newItems;
  123. o.pages = methods._getPages(o);
  124. this.data('pagination', o);
  125. methods._draw.call(this);
  126. },
  127. updateItemsOnPage: function (itemsOnPage) {
  128. var o = this.data('pagination');
  129. o.itemsOnPage = itemsOnPage;
  130. o.pages = methods._getPages(o);
  131. this.data('pagination', o);
  132. methods._selectPage.call(this, 0);
  133. return this;
  134. },
  135. _draw: function () {
  136. var o = this.data('pagination'),
  137. interval = methods._getInterval(o),
  138. i,
  139. tagName;
  140. methods.destroy.call(this);
  141. tagName = (typeof this.prop === 'function') ? this.prop('tagName') : this.attr('tagName');
  142. var $panel = tagName === 'UL' ? this : $('<ul class="pagination"></ul>').appendTo(this);
  143. // Generate Prev link
  144. if (o.prevText) {
  145. methods._appendItem.call(this, o.currentPage - 1, {text: o.prevText, classes: 'prev'});
  146. }
  147. // Generate start edges
  148. if (interval.start > 0 && o.edges > 0) {
  149. var end = Math.min(o.edges, interval.start);
  150. for (i = 0; i < end; i++) {
  151. methods._appendItem.call(this, i);
  152. }
  153. if (o.edges < interval.start && (interval.start - o.edges !== 1)) {
  154. $panel.append('<li class="page-item disabled"><span class="page-link">' + o.ellipseText + '</span></li>');
  155. } else if (interval.start - o.edges === 1) {
  156. methods._appendItem.call(this, o.edges);
  157. }
  158. }
  159. // Generate interval links
  160. for (i = interval.start; i < interval.end; i++) {
  161. methods._appendItem.call(this, i);
  162. }
  163. // Generate end edges
  164. if (interval.end < o.pages && o.edges > 0) {
  165. if (o.pages - o.edges > interval.end && (o.pages - o.edges - interval.end !== 1)) {
  166. $panel.append('<li class="page-item disabled"><span class="page-link">' + o.ellipseText + '</span></li>');
  167. } else if (o.pages - o.edges - interval.end === 1) {
  168. methods._appendItem.call(this, interval.end++);
  169. }
  170. var begin = Math.max(o.pages - o.edges, interval.end);
  171. for (i = begin; i < o.pages; i++) {
  172. methods._appendItem.call(this, i);
  173. }
  174. }
  175. // Generate Next link
  176. if (o.nextText) {
  177. methods._appendItem.call(this, o.currentPage + 1, {text: o.nextText, classes: 'next'});
  178. }
  179. },
  180. _getPages: function (o) {
  181. var pages = Math.ceil(o.items / o.itemsOnPage);
  182. return pages || 1;
  183. },
  184. _getInterval: function (o) {
  185. return {
  186. start: Math.ceil(o.currentPage > o.halfDisplayed ? Math.max(Math.min(o.currentPage - o.halfDisplayed, (o.pages - o.displayedPages)), 0) : 0),
  187. end: Math.ceil(o.currentPage > o.halfDisplayed ? Math.min(o.currentPage + o.halfDisplayed, o.pages) : Math.min(o.displayedPages, o.pages))
  188. };
  189. },
  190. _appendItem: function (pageIndex, opts) {
  191. var self = this, options, $link, o = self.data('pagination'),
  192. $linkWrapper = $('<li class="page-item"></li>'), $ul = self.find('ul');
  193. pageIndex = pageIndex < 0 ? 0 : (pageIndex < o.pages ? pageIndex : o.pages - 1);
  194. options = {
  195. text: pageIndex + 1,
  196. classes: ''
  197. };
  198. if (o.labelMap.length && o.labelMap[pageIndex]) {
  199. options.text = o.labelMap[pageIndex];
  200. }
  201. options = $.extend(options, opts || {});
  202. if (pageIndex === o.currentPage || o.disabled) {
  203. if (o.disabled) {
  204. $linkWrapper.addClass('disabled');
  205. } else if (options.classes !== "prev" && options.classes !== "next"){
  206. $linkWrapper.addClass('active');
  207. }
  208. $link = $('<span class="page-link">' + (options.text) + '</span>');
  209. } else {
  210. $link = $('<span class="page-link">' + (options.text) + '</span>');
  211. $link.click(function (event) {
  212. return methods._selectPage.call(self, pageIndex, event);
  213. });
  214. }
  215. if (options.classes) {
  216. $link.addClass(options.classes);
  217. }
  218. $linkWrapper.append($link);
  219. if ($ul.length) {
  220. $ul.append($linkWrapper);
  221. } else {
  222. self.append($linkWrapper);
  223. }
  224. },
  225. _selectPage: function (pageIndex, event) {
  226. var o = this.data('pagination');
  227. o.currentPage = pageIndex;
  228. if (o.selectOnClick) {
  229. methods._draw.call(this);
  230. }
  231. pageurl(pageIndex + 1);
  232. return o.onPageClick(pageIndex + 1, event);
  233. }
  234. };
  235. $.fn.pagination = function (method) {
  236. // Method calling logic
  237. if (methods[method] && method.charAt(0) !== '_') {
  238. return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
  239. } else if (typeof method === 'object' || !method) {
  240. return methods.init.apply(this, arguments);
  241. } else {
  242. $.error('Method ' + method + ' does not exist on jQuery.pagination');
  243. }
  244. };
  245. })(jQuery);
  246. /* www.jq22.com */