pagination.vue 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <template>
  2. <div>
  3. <ul class="pagination">
  4. <li :class="{'disabled': current == 1}"><a href="javascript:;" @click="setCurrent(current - 1)"> {{prePage}} </a></li>
  5. <li :class="{'disabled': current == 1}"><a href="javascript:;" @click="setCurrent(1)"> {{homePage}} </a></li>
  6. <li v-for="(p,index) in grouplist" :class="{'active': current == p.val}" :key="index">
  7. <a href="javascript:;" @click="setCurrent(p.val)"> {{ p.text }} </a>
  8. </li>
  9. <li :class="{'disabled': current == page}"><a href="javascript:;" @click="setCurrent(page)"> {{lastPage}} </a></li>
  10. <li :class="{'disabled': current == page}"><a href="javascript:;" @click="setCurrent(current + 1)"> {{nextPage}}</a></li>
  11. </ul>
  12. </div>
  13. </template>
  14. <script>
  15. export default{
  16. name: 'pagination',
  17. data(){
  18. return {
  19. prePage: '<',
  20. nextPage: '>',
  21. homePage: '<<',
  22. lastPage: '>>',
  23. current: this.currentPage
  24. }
  25. },
  26. props: {
  27. total: {// 数据总条数
  28. type: Number,
  29. default: 0
  30. },
  31. display: {// 每页显示条数
  32. type: Number,
  33. default: 8
  34. },
  35. currentPage: {// 当前页码
  36. type: Number,
  37. default: 1
  38. },
  39. pagegroup: {// 分页条数
  40. type: Number,
  41. default: 5,
  42. coerce: function (v) {
  43. v = v > 0 ? v : 5;
  44. return v % 2 === 1 ? v : v + 1;
  45. }
  46. }
  47. },
  48. computed: {
  49. page () { // 总页数
  50. return Math.ceil(this.total / this.display);
  51. },
  52. grouplist: function () { // 获取分页页码
  53. var len = this.page,
  54. temp = [],
  55. list = [],
  56. count = Math.floor(this.pagegroup / 2),
  57. center = this.current;
  58. if (len <= this.pagegroup) {
  59. while (len--) {
  60. temp.push({text: this.page - len, val: this.page - len});
  61. };
  62. return temp;
  63. }
  64. while (len--) {
  65. temp.push(this.page - len);
  66. };
  67. var idx = temp.indexOf(center);
  68. (idx < count) && ( center = center + count - idx);
  69. (this.current > this.page - count) && ( center = this.page - count);
  70. temp = temp.splice(center - count - 1, this.pagegroup);
  71. do {
  72. var t = temp.shift();
  73. list.push({
  74. text: t,
  75. val: t
  76. });
  77. } while (temp.length);
  78. if (this.page > this.pagegroup) {
  79. (this.current > count + 1) && list.unshift({text: '...', val: list[0].val - 1});
  80. (this.current < this.page - count) && list.push({text: '...', val: list[list.length - 1].val + 1});
  81. }
  82. return list;
  83. }
  84. },
  85. methods: {
  86. setCurrent (idx) {
  87. if (this.current != idx && idx > 0 && idx < this.page + 1) {
  88. this.current = idx;
  89. this.$emit('pagechange', this.current);
  90. }
  91. }
  92. }
  93. }
  94. </script>
  95. <style scoped>
  96. ul.pagination {
  97. overflow: hidden;
  98. width: 100%;
  99. }
  100. ul.pagination li {
  101. display: inline-block;
  102. margin: 0 5px;
  103. width: 38px;
  104. height: 38px;
  105. text-align: center;
  106. }
  107. ul.pagination li a {
  108. display: inline-block;
  109. width: 38px;
  110. height: 38px;
  111. background: #fff;
  112. border: 2px solid #000;
  113. color: #000;
  114. font-size: 14px;
  115. line-height: 34px;
  116. text-align: center;
  117. box-sizing: border-box;
  118. }
  119. ul.pagination li:first-child,ul.pagination li:nth-child(2),ul.pagination li:last-child,ul.pagination li:nth-last-child(2) {
  120. font-family: "Simsun";
  121. font-weight: bold;
  122. }
  123. ul.pagination li.active a,ul.pagination li:hover a {
  124. background: #000;
  125. color: #feeabd;
  126. }
  127. </style>