TablePage.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <template>
  2. <div class="table_page" v-show="totalPage > 0">
  3. <div class="fisrtPage" @click="jumpPage(1)" v-show="currentPage > 1">
  4. 首页
  5. </div>
  6. <div class="previousPage" @click="jumpPage(2)" v-show="currentPage > 1">
  7. 上一页
  8. </div>
  9. <div
  10. v-for="(item, index) in pageArr"
  11. :key="index"
  12. :class="{ current_page: currentPage == item }"
  13. @click="chagePage(item)"
  14. >
  15. {{ item }}
  16. </div>
  17. <div class="nextPage" @click="jumpPage(3)" v-show="currentPage < totalPage">
  18. 下一页
  19. </div>
  20. <div class="lastPage" @click="jumpPage(4)" v-show="currentPage < totalPage">
  21. 尾页
  22. </div>
  23. </div>
  24. </template>
  25. <script>
  26. export default {
  27. props: {
  28. // 共有多少页
  29. totalPage: {
  30. type: Number,
  31. default: 0,
  32. },
  33. // 当前在第几页
  34. currentPage: {
  35. type: Number,
  36. default: 1,
  37. },
  38. },
  39. computed: {
  40. // 生成表格的导航
  41. pageArr() {
  42. let arr = [];
  43. if (this.totalPage <= 10) {
  44. // 最多展示十个,总页面小于等于10,全部展示
  45. for (let i = 1; i <= this.totalPage; i++) {
  46. arr.push(i);
  47. }
  48. } else {
  49. // 点击的是1~5, 展示1~10
  50. if (this.currentPage - 5 <= 0) {
  51. for (let i = 1; i <= 10; i++) {
  52. arr.push(i);
  53. }
  54. } else {
  55. // 点击 大于5,同时后面的页面至少有五个五个,展示
  56. if (this.totalPage - this.currentPage > 4) {
  57. for (let i = this.currentPage - 4; i <= this.currentPage + 5; i++) {
  58. arr.push(i);
  59. }
  60. } else {
  61. // 点击 大于五,同时后面的页面数不足五个
  62. for (let i = this.totalPage - 9; i <= this.totalPage; i++) {
  63. arr.push(i);
  64. }
  65. }
  66. }
  67. }
  68. return arr;
  69. },
  70. },
  71. methods: {
  72. chagePage: function (page) {
  73. this.$emit("change_page", page);
  74. },
  75. jumpPage: function (item) {
  76. this.$emit("jump_page", item);
  77. },
  78. },
  79. };
  80. </script>
  81. <style scoped lang="less">
  82. .table_page {
  83. height: 30px;
  84. display: flex;
  85. border-left: 1px solid #ccc;
  86. div {
  87. width: 32px;
  88. background-color: #fff;
  89. line-height: 30px;
  90. border-bottom: 1px solid #ccc;
  91. border-right: 1px solid #ccc;
  92. text-align: center;
  93. &:hover {
  94. cursor: pointer;
  95. }
  96. }
  97. .current_page {
  98. color: #999;
  99. background-color: #f5f5f5;
  100. }
  101. .fisrtPage {
  102. width: 50px;
  103. }
  104. .previousPage {
  105. width: 60px;
  106. }
  107. .nextPage {
  108. width: 60px;
  109. }
  110. .lastPage {
  111. width: 50px;
  112. }
  113. }
  114. </style>