Legeradda

Untitled

Jan 16th, 2021
636
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <script>
  2. export default {
  3.   data() {
  4.     return {
  5.       totalCounties: 250,
  6.       first: 0,
  7.       last: 20,
  8.       totalInPerPage: 20,
  9.       countries: [],
  10.       country: [],
  11.       historyCountries: [],
  12.     };
  13.   },
  14.   mounted() {
  15.     this.fetchData();
  16.   },
  17.   methods: {
  18.     fetchData() {
  19.       fetch("https://restcountries.eu/rest/v2/all")
  20.         .then((res) => res.json())
  21.         .then((data) => (this.countries = data.slice(this.first, this.last)))
  22.         .catch((err) => console.log(err.message));
  23.     },
  24.     previous() {
  25.       this.last = this.first;
  26.       this.first = this.first - this.totalInPerPage;
  27.       if (this.first >= 0) {
  28.         this.fetchData();
  29.       } else {
  30.         this.first = 0;
  31.         this.last = this.totalInPerPage;
  32.         this.fetchData();
  33.       }
  34.     },
  35.     next() {
  36.       this.first = this.last;
  37.       this.last = this.last + this.totalInPerPage;
  38.       if (this.last <= this.totalCounties) {
  39.         this.fetchData();
  40.       } else {
  41.         this.first = this.totalCounties - this.totalInPerPage;
  42.         this.last = this.totalCounties;
  43.         this.fetchData();
  44.       }
  45.     },
  46.     handleClick(country) {
  47.       console.log("Clicked on: " + country.name);
  48.     },
  49.   },
  50. };
  51. </script>
Advertisement
Add Comment
Please, Sign In to add comment