Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.00 KB | None | 0 0
  1. <template>
  2. <div v-if="shouldShowPagination">
  3. <div class="col-lg-12">
  4. <nav aria-label="Page navigation">
  5. <ul class="pagination justify-content-center">
  6. <li :class="(pagination.currentPage === 1) ? 'page-item disabled' : 'page-item'">
  7. <a href="javascript:void(0)"
  8. class="page-link"
  9. @click="pageClicked( pagination.currentPage - 1 )">
  10. Предыдущая
  11. </a>
  12. </li>
  13. <li class="page-item"
  14. v-for="page in this.pageLinks()"
  15. :key="page"
  16. :class="(isActive(page) || page === '...') ? 'page-item disabled' : 'page-item'">
  17. <a class="page-link"
  18. @click="pageClicked(page)"
  19. href="javascript:void(0)">
  20. {{ page }}
  21. </a>
  22. </li>
  23. <li :class="(pagination.currentPage === this.pagination.totalPages) ? 'page-item disabled': 'page-item'">
  24. <a href="javascript:void(0)"
  25. class="page-link"
  26. @click="pageClicked( pagination.currentPage + 1 )">
  27. Следующая
  28. </a>
  29. </li>
  30. </ul>
  31. </nav>
  32. </div>
  33. </div>
  34. </template>
  35. <script>
  36. export default {
  37. name: 'Pagination',
  38. props: ['pagination'],
  39. methods: {
  40. pageLinks: function () {
  41. if (this.pagination.totalPages === undefined) {
  42. return [];
  43. }
  44.  
  45. const arr = [];
  46. let preDots = false;
  47. let postDots = false;
  48.  
  49. for (let i = 1; i <= this.pagination.totalPages; i++) {
  50. if (this.pagination.totalPages <= 10) {
  51. arr.push(i);
  52. } else {
  53. if (i === 1) {
  54. arr.push(i);
  55. } else if (i === this.pagination.totalPages) {
  56. arr.push(i);
  57. } else if (
  58. (i > this.pagination.currentPage - 4 && i < this.pagination.currentPage + 4) ||
  59. (i < 4 && this.pagination.currentPage < 4) ||
  60. (i > this.pagination.totalPages - 4 && this.pagination.currentPage > this.pagination.totalPages - 4)) {
  61. arr.push(i);
  62. } else if (i < this.pagination.currentPage && !preDots) {
  63. arr.push('...');
  64. preDots = true;
  65. } else if (i > this.pagination.currentPage && !postDots) {
  66. arr.push('...');
  67. postDots = true;
  68. }
  69. }
  70. }
  71.  
  72. return arr;
  73. },
  74.  
  75. shouldShowPagination: function () {
  76. if (this.pagination.totalPages === undefined) {
  77. return false;
  78. }
  79.  
  80. if (this.pagination.count === 0) {
  81. return false;
  82. }
  83.  
  84. return this.pagination.totalPages > 1;
  85. },
  86.  
  87. isActive: function (page) {
  88. const currentPage = this.pagination.currentPage || 1;
  89.  
  90. return currentPage === page;
  91. },
  92.  
  93. pageClicked: function (page) {
  94. if (page === '...' ||
  95. page === this.pagination.currentPage ||
  96. page > this.pagination.totalPages ||
  97. page < 1) {
  98. return;
  99. }
  100.  
  101. this.$emit('pageChange', page);
  102. this.$scrollTo('#top_line', 450);
  103. },
  104. },
  105. };
  106. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement