Advertisement
Legeradda

Countrylist.vue

Jan 16th, 2021
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.22 KB | None | 0 0
  1. <template>
  2. <div class="container">
  3. <!-- pagination -->
  4. <div class="container mb-4">
  5. <nav aria-label="Page navigation example">
  6. <ul class="pagination justify-content-center">
  7. <li class="page-item">
  8. <button class="page-link" @click.prevent="previous()">
  9. Previous
  10. </button>
  11. </li>
  12. <li class="page-item">
  13. <button class="page-link" @click.prevent="next()">Next</button>
  14. </li>
  15. </ul>
  16. </nav>
  17. </div>
  18. <!-- /pagination -->
  19.  
  20. <div class="row row-cols-1 row-cols-sm-2 row-cols-md-3 g-3">
  21. <!-- a single row from an array -->
  22. <div
  23. class="col"
  24. v-for="(country, index) in countries"
  25. :key="index"
  26. v-bind:value="countries.name"
  27. >
  28. <div class="card shadow-sm">
  29. <img class="card-img-top" :src="country.flag" alt="Card image cap" />
  30. <div class="card-body">
  31. <strong class="card-text" v-on:click="handleClick">{{
  32. country.name
  33. }}</strong>
  34. <div class="d-flex justify-content-between align-items-center mt-2">
  35. <div class="btn-group"></div>
  36. </div>
  37. </div>
  38. </div>
  39. </div>
  40. <!-- /a single row from an array -->
  41. </div>
  42. <!-- pagination -->
  43. <div class="container mt-4">
  44. <nav aria-label="Page navigation example">
  45. <ul class="pagination justify-content-center">
  46. <li class="page-item">
  47. <button class="page-link" @click.prevent="previous()">
  48. Previous
  49. </button>
  50. </li>
  51. <li class="page-item">
  52. <button class="page-link" @click.prevent="next()">Next</button>
  53. </li>
  54. </ul>
  55. </nav>
  56. </div>
  57. <!-- /pagination -->
  58. </div>
  59. </template>
  60.  
  61. <script>
  62. export default {
  63. data() {
  64. return {
  65. totalCounties: 250,
  66. first: 0,
  67. last: 20,
  68. totalInPerPage: 20,
  69. countries: [],
  70. count: 0,
  71. //detailsInfo: false,
  72. //btnShow: false,
  73. };
  74. },
  75. mounted() {
  76. this.fetchData();
  77. },
  78. methods: {
  79. fetchData() {
  80. fetch("https://restcountries.eu/rest/v2/all")
  81. .then((res) => res.json())
  82. .then((data) => (this.countries = data.slice(this.first, this.last)))
  83. .catch((err) => console.log(err.message));
  84. },
  85. previous() {
  86. this.last = this.first;
  87. this.first = this.first - this.totalInPerPage;
  88. if (this.first >= 0) {
  89. this.fetchData();
  90. } else {
  91. this.first = 0;
  92. this.last = this.totalInPerPage;
  93. this.fetchData();
  94. }
  95. },
  96. next() {
  97. this.first = this.last;
  98. this.last = this.last + this.totalInPerPage;
  99. if (this.last <= this.totalCounties) {
  100. this.fetchData();
  101. } else {
  102. this.first = this.totalCounties - this.totalInPerPage;
  103. this.last = this.totalCounties;
  104. this.fetchData();
  105. }
  106. },
  107. handleClick() {
  108. //console.log("[response]", JSON.stringify(this.countries));
  109. },
  110. },
  111. };
  112. </script>
  113.  
  114. <style scoped>
  115. .card-img-top {
  116. width: 100%;
  117. height: 15vw;
  118. object-fit: cover;
  119. }
  120. </style>
  121.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement