夏邑12345_前端

pagination.js 9.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /**
  2. * name: tm.pagination
  3. * Version: 1.0.0 beta
  4. */
  5. angular.module('tm.pagination', []).directive('tmPagination',[function(){
  6. return {
  7. restrict: 'EA',
  8. template: '<div class="page-list clearfix">' +
  9. '<ul class="pagination clearfix" ng-show="conf.totalItems > 0">' +
  10. '<li ng-class="{disabled: conf.currentPage == 1}" ng-click="prevPage()"><span>&laquo;</span></li>' +
  11. '<li ng-repeat="item in pageList track by $index" ng-class="{active: item == conf.currentPage, separate: item == \'...\'}" ' +
  12. 'ng-click="changeCurrentPage(item)">' +
  13. '<span>{{ item }}</span>' +
  14. '</li>' +
  15. '<li ng-class="{disabled: conf.currentPage == conf.numberOfPages}" ng-click="nextPage()"><span>&raquo;</span></li>' +
  16. '</ul>' +
  17. // '<div class="page-total" ng-show="conf.totalItems > 0">' +
  18. // '每页<select ng-model="conf.itemsPerPage" ng-options="option for option in conf.perPageOptions " ng-change="changeItemsPerPage()"></select>' +
  19. // '/共<strong>{{ conf.totalItems }}</strong>条 ' +
  20. // '跳转至<input type="text" ng-model="jumpPageNum" ng-keyup="jumpPageKeyUp($event)"/>' +
  21. // '</div>' +
  22. // '<div class="no-items" ng-show="conf.totalItems <= 0">暂无数据</div>' +
  23. '</div>',
  24. replace: true,
  25. scope: {
  26. conf: '='
  27. },
  28. link: function(scope, element, attrs) {
  29. var conf = scope.conf;
  30. // 默认分页长度
  31. var defaultPagesLength = 9;
  32. // 默认分页选项可调整每页显示的条数
  33. // var defaultPerPageOptions = [10, 15, 20, 30, 50];
  34. // 默认每页的个数
  35. var defaultPerPage = 15;
  36. // 获取分页长度
  37. if(conf.pagesLength) {
  38. // 判断一下分页长度
  39. conf.pagesLength = parseInt(conf.pagesLength, 10);
  40. if(!conf.pagesLength) {
  41. conf.pagesLength = defaultPagesLength;
  42. }
  43. // 分页长度必须为奇数,如果传偶数时,自动处理
  44. if(conf.pagesLength % 2 === 0) {
  45. conf.pagesLength += 1;
  46. }
  47. } else {
  48. conf.pagesLength = defaultPagesLength
  49. }
  50. // 分页选项可调整每页显示的条数
  51. // if(!conf.perPageOptions){
  52. // conf.perPageOptions = defaultPagesLength;
  53. // }
  54. // pageList数组
  55. function getPagination(newValue, oldValue) {
  56. // conf.currentPage
  57. if(conf.currentPage) {
  58. conf.currentPage = parseInt(scope.conf.currentPage, 10);
  59. }
  60. if(!conf.currentPage) {
  61. conf.currentPage = 1;
  62. }
  63. // conf.totalItems
  64. if(conf.totalItems) {
  65. conf.totalItems = parseInt(conf.totalItems, 10);
  66. }
  67. // conf.totalItems
  68. if(!conf.totalItems) {
  69. conf.totalItems = 0;
  70. return;
  71. }
  72. // conf.itemsPerPage
  73. if(conf.itemsPerPage) {
  74. conf.itemsPerPage = parseInt(conf.itemsPerPage, 10);
  75. }
  76. if(!conf.itemsPerPage) {
  77. conf.itemsPerPage = defaultPerPage;
  78. }
  79. // numberOfPages
  80. conf.numberOfPages = Math.ceil(conf.totalItems/conf.itemsPerPage);
  81. // 如果分页总数>0,并且当前页大于分页总数
  82. if(scope.conf.numberOfPages > 0 && scope.conf.currentPage > scope.conf.numberOfPages){
  83. scope.conf.currentPage = scope.conf.numberOfPages;
  84. }
  85. // 如果itemsPerPage在不在perPageOptions数组中,就把itemsPerPage加入这个数组中
  86. // var perPageOptionsLength = scope.conf.perPageOptions.length;
  87. // 定义状态
  88. // var perPageOptionsStatus;
  89. // for(var i = 0; i < perPageOptionsLength; i++){
  90. // if(conf.perPageOptions[i] == conf.itemsPerPage){
  91. // perPageOptionsStatus = true;
  92. // }
  93. // }
  94. // 如果itemsPerPage在不在perPageOptions数组中,就把itemsPerPage加入这个数组中
  95. // if(!perPageOptionsStatus){
  96. // conf.perPageOptions.push(conf.itemsPerPage);
  97. // }
  98. // 对选项进行sort
  99. // conf.perPageOptions.sort(function(a, b) {return a - b});
  100. // 页码相关
  101. scope.pageList = [];
  102. if(conf.numberOfPages <= conf.pagesLength){
  103. // 判断总页数如果小于等于分页的长度,若小于则直接显示
  104. for(i =1; i <= conf.numberOfPages; i++){
  105. scope.pageList.push(i);
  106. }
  107. }else{
  108. // 总页数大于分页长度(此时分为三种情况:1.左边没有...2.右边没有...3.左右都有...)
  109. // 计算中心偏移量
  110. var offset = (conf.pagesLength - 1) / 2;
  111. if(conf.currentPage <= offset){
  112. // 左边没有...
  113. for(i = 1; i <= offset + 1; i++){
  114. scope.pageList.push(i);
  115. }
  116. scope.pageList.push('...');
  117. scope.pageList.push(conf.numberOfPages);
  118. }else if(conf.currentPage > conf.numberOfPages - offset){
  119. scope.pageList.push(1);
  120. scope.pageList.push('...');
  121. for(i = offset + 1; i >= 1; i--){
  122. scope.pageList.push(conf.numberOfPages - i);
  123. }
  124. scope.pageList.push(conf.numberOfPages);
  125. }else{
  126. // 最后一种情况,两边都有...
  127. scope.pageList.push(1);
  128. scope.pageList.push('...');
  129. for(i = Math.ceil(offset / 2) ; i >= 1; i--){
  130. scope.pageList.push(conf.currentPage - i);
  131. }
  132. scope.pageList.push(conf.currentPage);
  133. for(i = 1; i <= offset / 2; i++){
  134. scope.pageList.push(conf.currentPage + i);
  135. }
  136. scope.pageList.push('...');
  137. scope.pageList.push(conf.numberOfPages);
  138. }
  139. }
  140. scope.$parent.conf = conf;
  141. }
  142. // prevPage
  143. scope.prevPage = function() {
  144. if(conf.currentPage > 1){
  145. conf.currentPage -= 1;
  146. }
  147. getPagination();
  148. if(conf.onChange) {
  149. conf.onChange();
  150. }
  151. };
  152. // nextPage
  153. scope.nextPage = function() {
  154. if(conf.currentPage < conf.numberOfPages){
  155. conf.currentPage += 1;
  156. }
  157. getPagination();
  158. if(conf.onChange) {
  159. conf.onChange();
  160. }
  161. };
  162. // 变更当前页
  163. scope.changeCurrentPage = function(item) {
  164. if(item == '...'){
  165. return;
  166. }else{
  167. conf.currentPage = item;
  168. getPagination();
  169. // conf.onChange()函数
  170. if(conf.onChange) {
  171. conf.onChange();
  172. }
  173. }
  174. };
  175. // 修改每页展示的条数
  176. scope.changeItemsPerPage = function() {
  177. // 一发展示条数变更,当前页将重置为1
  178. conf.currentPage = 1;
  179. getPagination();
  180. // conf.onChange()函数
  181. if(conf.onChange) {
  182. conf.onChange();
  183. }
  184. };
  185. // 跳转页
  186. scope.jumpToPage = function() {
  187. num = scope.jumpPageNum;
  188. if(num.match(/\d+/)) {
  189. num = parseInt(num, 10);
  190. if(num && num != conf.currentPage) {
  191. if(num > conf.numberOfPages) {
  192. num = conf.numberOfPages;
  193. }
  194. // 跳转
  195. conf.currentPage = num;
  196. getPagination();
  197. // conf.onChange()函数
  198. if(conf.onChange) {
  199. conf.onChange();
  200. }
  201. scope.jumpPageNum = '';
  202. }
  203. }
  204. };
  205. scope.jumpPageKeyUp = function(e) {
  206. var keycode = window.event ? e.keyCode :e.which;
  207. if(keycode == 13) {
  208. scope.jumpToPage();
  209. }
  210. }
  211. scope.$watch('conf.totalItems', function(value, oldValue) {
  212. // 在无值或值相等的时候,去执行onChange事件
  213. if(!value || value == oldValue) {
  214. if(conf.onChange) {
  215. conf.onChange();
  216. }
  217. }
  218. getPagination();
  219. })
  220. }
  221. };
  222. }]);