tm.pagination.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /**
  2. * name: tm.pagination
  3. * Version: 0.0.2
  4. */
  5. angular.module('tm.pagination', []).directive('tmPagination',[function(){
  6. return {
  7. restrict: 'EA',
  8. template: '<div class="page-list">' +
  9. '<ul class="pagination" 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. '第<input type="text" ng-model="jumpPageNum" ng-keyup="jumpToPage($event)"/>页 ' +
  19. '每页<select ng-model="conf.itemsPerPage" ng-options="option for option in conf.perPageOptions " ng-change="changeItemsPerPage()"></select>' +
  20. '/共<strong>{{ conf.totalItems }}</strong>条' +
  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. // 变更当前页
  30. scope.changeCurrentPage = function(item){
  31. if(item == '...'){
  32. return;
  33. }else{
  34. scope.conf.currentPage = item;
  35. }
  36. };
  37. // 定义分页的长度必须为奇数 (default:9)
  38. scope.conf.pagesLength = parseInt(scope.conf.pagesLength) ? parseInt(scope.conf.pagesLength) : 9 ;
  39. if(scope.conf.pagesLength % 2 === 0){
  40. // 如果不是奇数的时候处理一下
  41. scope.conf.pagesLength = scope.conf.pagesLength -1;
  42. }
  43. // conf.erPageOptions
  44. if(!scope.conf.perPageOptions){
  45. scope.conf.perPageOptions = [10, 15, 20, 30, 50];
  46. }
  47. // pageList数组
  48. function getPagination(){
  49. // conf.currentPage
  50. scope.conf.currentPage = parseInt(scope.conf.currentPage) ? parseInt(scope.conf.currentPage) : 1;
  51. // conf.totalItems
  52. scope.conf.totalItems = parseInt(scope.conf.totalItems);
  53. // conf.itemsPerPage (default:15)
  54. // 先判断一下本地存储中有没有这个值
  55. if(scope.conf.rememberPerPage){
  56. if(!parseInt(localStorage[scope.conf.rememberPerPage])){
  57. localStorage[scope.conf.rememberPerPage] = parseInt(scope.conf.itemsPerPage) ? parseInt(scope.conf.itemsPerPage) : 15;
  58. }
  59. scope.conf.itemsPerPage = parseInt(localStorage[scope.conf.rememberPerPage]);
  60. }else{
  61. scope.conf.itemsPerPage = parseInt(scope.conf.itemsPerPage) ? parseInt(scope.conf.itemsPerPage) : 15;
  62. }
  63. // numberOfPages
  64. scope.conf.numberOfPages = Math.ceil(scope.conf.totalItems/scope.conf.itemsPerPage);
  65. // judge currentPage > scope.numberOfPages
  66. if(scope.conf.currentPage < 1){
  67. scope.conf.currentPage = 1;
  68. }
  69. if(scope.conf.currentPage > scope.conf.numberOfPages){
  70. scope.conf.currentPage = scope.conf.numberOfPages;
  71. }
  72. // jumpPageNum
  73. scope.jumpPageNum = scope.conf.currentPage;
  74. // 如果itemsPerPage在不在perPageOptions数组中,就把itemsPerPage加入这个数组中
  75. var perPageOptionsLength = scope.conf.perPageOptions.length;
  76. // 定义状态
  77. var perPageOptionsStatus;
  78. for(var i = 0; i < perPageOptionsLength; i++){
  79. if(scope.conf.perPageOptions[i] == scope.conf.itemsPerPage){
  80. perPageOptionsStatus = true;
  81. }
  82. }
  83. // 如果itemsPerPage在不在perPageOptions数组中,就把itemsPerPage加入这个数组中
  84. if(!perPageOptionsStatus){
  85. scope.conf.perPageOptions.push(scope.conf.itemsPerPage);
  86. }
  87. // 对选项进行sort
  88. scope.conf.perPageOptions.sort(function(a, b){return a-b});
  89. scope.pageList = [];
  90. if(scope.conf.numberOfPages <= scope.conf.pagesLength){
  91. // 判断总页数如果小于等于分页的长度,若小于则直接显示
  92. for(i =1; i <= scope.conf.numberOfPages; i++){
  93. scope.pageList.push(i);
  94. }
  95. }else{
  96. // 总页数大于分页长度(此时分为三种情况:1.左边没有...2.右边没有...3.左右都有...)
  97. // 计算中心偏移量
  98. var offset = (scope.conf.pagesLength - 1)/2;
  99. if(scope.conf.currentPage <= offset){
  100. // 左边没有...
  101. for(i =1; i <= offset +1; i++){
  102. scope.pageList.push(i);
  103. }
  104. scope.pageList.push('...');
  105. scope.pageList.push(scope.conf.numberOfPages);
  106. }else if(scope.conf.currentPage > scope.conf.numberOfPages - offset){
  107. scope.pageList.push(1);
  108. scope.pageList.push('...');
  109. for(i = offset + 1; i >= 1; i--){
  110. scope.pageList.push(scope.conf.numberOfPages - i);
  111. }
  112. scope.pageList.push(scope.conf.numberOfPages);
  113. }else{
  114. // 最后一种情况,两边都有...
  115. scope.pageList.push(1);
  116. scope.pageList.push('...');
  117. for(i = Math.ceil(offset/2) ; i >= 1; i--){
  118. scope.pageList.push(scope.conf.currentPage - i);
  119. }
  120. scope.pageList.push(scope.conf.currentPage);
  121. for(i = 1; i <= offset/2; i++){
  122. scope.pageList.push(scope.conf.currentPage + i);
  123. }
  124. scope.pageList.push('...');
  125. scope.pageList.push(scope.conf.numberOfPages);
  126. }
  127. }
  128. if(scope.conf.onChange){
  129. scope.conf.onChange();
  130. }
  131. scope.$parent.conf = scope.conf;
  132. }
  133. // prevPage
  134. scope.prevPage = function(){
  135. if(scope.conf.currentPage > 1){
  136. scope.conf.currentPage -= 1;
  137. }
  138. };
  139. // nextPage
  140. scope.nextPage = function(){
  141. if(scope.conf.currentPage < scope.conf.numberOfPages){
  142. scope.conf.currentPage += 1;
  143. }
  144. };
  145. // 跳转页
  146. scope.jumpToPage = function(){
  147. scope.jumpPageNum = scope.jumpPageNum.replace(/[^0-9]/g,'');
  148. if(scope.jumpPageNum !== ''){
  149. scope.conf.currentPage = scope.jumpPageNum;
  150. }
  151. };
  152. // 修改每页显示的条数
  153. scope.changeItemsPerPage = function(){
  154. // 清除本地存储的值方便重新设置
  155. if(scope.conf.rememberPerPage){
  156. localStorage.removeItem(scope.conf.rememberPerPage);
  157. }
  158. };
  159. scope.$watch(function(){
  160. var newValue = scope.conf.currentPage + ' ' + scope.conf.totalItems + ' ';
  161. // 如果直接watch perPage变化的时候,因为记住功能的原因,所以一开始可能调用两次。
  162. //所以用了如下方式处理
  163. if(scope.conf.rememberPerPage){
  164. // 由于记住的时候需要特别处理一下,不然可能造成反复请求
  165. // 之所以不监控localStorage[scope.conf.rememberPerPage]是因为在删除的时候会undefind
  166. // 然后又一次请求
  167. if(localStorage[scope.conf.rememberPerPage]){
  168. newValue += localStorage[scope.conf.rememberPerPage];
  169. }else{
  170. newValue += scope.conf.itemsPerPage;
  171. }
  172. }else{
  173. newValue += scope.conf.itemsPerPage;
  174. }
  175. return newValue;
  176. }, getPagination);
  177. }
  178. };
  179. }]);