Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

basePagination.vue 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <template>
  2. <div>
  3. <nav>
  4. <ul class="pagination">
  5. <li class="page-item" v-if="isPreviousButtonDisabled" @click="previousPage">
  6. <a class="page-link" aria-label="Previous">
  7. <span aria-hidden="true">&laquo;</span>
  8. <span class="sr-only">Previous</span>
  9. </a>
  10. </li>
  11. <li class="page-item" v-for="item in paginationItems" :key="item">
  12. <BasePaginationItem
  13. :pageNumber="item"
  14. :isCurrentPage="item === currentPage"
  15. @loadPage="onLoadPage"
  16. />
  17. </li>
  18. <li class="page-item" v-if="isNextButtonDisabled" @click="nextPage">
  19. <a class="page-link" aria-label="Next">
  20. <span aria-hidden="true">&raquo;</span>
  21. <span class="sr-only">Next</span>
  22. </a>
  23. </li>
  24. </ul>
  25. </nav>
  26. </div>
  27. </template>
  28. <script>
  29. import BasePaginationItem from "./basePaginationItem.vue";
  30. export default {
  31. components: {
  32. BasePaginationItem
  33. },
  34. props: {
  35. visiblePagesCount: {
  36. type: Number,
  37. default: 5
  38. },
  39. currentPage: {
  40. type: Number,
  41. required: true
  42. },
  43. pageCount: {
  44. type: Number,
  45. required: true
  46. }
  47. },
  48. computed: {
  49. isPreviousButtonDisabled() {
  50. return this.currentPage !== 1;
  51. },
  52. isNextButtonDisabled() {
  53. return this.currentPage !== this.pageCount;
  54. },
  55. paginationItems() {
  56. const { currentPage } = this;
  57. const { pageCount } = this;
  58. const { visiblePagesCount } = this;
  59. let pagintationItemsArray = [];
  60. if (pageCount <= visiblePagesCount) {
  61. let count = 1;
  62. while (count <= pageCount) {
  63. pagintationItemsArray.push(count);
  64. count += 1;
  65. }
  66. return pagintationItemsArray;
  67. }
  68. const visiblePagesThreshold = (visiblePagesCount - 1) / 2;
  69. pagintationItemsArray = Array(this.visiblePagesCount - 1).fill(0);
  70. if (currentPage <= visiblePagesThreshold + 1) {
  71. pagintationItemsArray[0] = 1;
  72. const pagintationItems = pagintationItemsArray.map(
  73. (paginationTrigger, index) => pagintationItemsArray[0] + index
  74. );
  75. pagintationItems.push(pageCount);
  76. return pagintationItems;
  77. }
  78. if (currentPage >= pageCount - visiblePagesThreshold + 1) {
  79. const pagintationItems = pagintationItemsArray.map(
  80. (paginationTrigger, index) => pageCount - index
  81. );
  82. pagintationItems.reverse().unshift(1);
  83. return pagintationItems;
  84. }
  85. pagintationItemsArray[0] = currentPage - visiblePagesThreshold + 1;
  86. const pagintationItems = pagintationItemsArray.map(
  87. (paginationTrigger, index) => pagintationItemsArray[0] + index
  88. );
  89. pagintationItems.unshift(1);
  90. pagintationItems[pagintationItems.length - 1] = pageCount;
  91. return pagintationItems;
  92. }
  93. },
  94. methods: {
  95. onLoadPage(value) {
  96. this.$emit("loadPage", value);
  97. },
  98. nextPage() {
  99. this.$emit("nextPage");
  100. },
  101. previousPage() {
  102. this.$emit("previousPage");
  103. }
  104. }
  105. };
  106. </script>