yahyaaa

api.js dan competition.html

Aug 2nd, 2020
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //contoh api.js
  2.  
  3. const base_url = "https://api.football-data.org/v2/";
  4. const token = "your token";
  5. const fetchAPI = url => {
  6.   return fetch(url, {
  7.     headers: {
  8.       'X-Auth-Token': token
  9.     }
  10.   })
  11.   .then(res => {
  12.     if (res.status !== 200) {
  13.       console.log("Error: " + res.status);
  14.       return Promise.reject(new Error(res.statusText))
  15.     } else {
  16.       return Promise.resolve(res)
  17.     }
  18.   })
  19.   .then(res => res.json())
  20.   .catch(err => {
  21.     console.log(err)
  22.   })
  23. };
  24.  
  25. const getCompetitions = () => {
  26.   fetchAPI(base_url+"competitions?plan=TIER_ONE")
  27.     .then(data => {
  28.       showCompetitions(data);
  29.     })
  30.     .catch(error => {
  31.       console.log(error)
  32.     })
  33. }
  34.  
  35. const showCompetitions = data =>{
  36.   let competitionsHTML = document.getElementById("listcompetition"); // listcompetition ini berasal dari id yang berada di html
  37.   data.competitions.forEach(function(competition) {
  38.     competitionsHTML.innerHTML += ` //kenapa menggunakan += karena ini untuk looping sebanyak data yang didapat
  39.     <tr onclick="window.location.href='./competition.html?id=${competition.id}'">
  40.       <td>${competition.area.name}</td>
  41.       <td>${competition.name}</td>
  42.       <td>${competition.plan}</td>
  43.     </tr>`;
  44.        
  45.   });
  46. }
  47.  
  48.  
  49.  
  50. // file pages/competition.html
  51.  
  52.                 <table class="table highlight centered">
  53.                   <thead>
  54.                     <tr class="gradient-45deg-light-blue-cyan">
  55.                       <th>Country</th>
  56.                       <th>Name</th>
  57.                       <th>Plan</th>
  58.                     </tr>
  59.                   </thead>
  60.                   <tbody id="listcompetition"></tbody> // id ini yang nanti kita gunakan sebagai identitas nya
  61.                 </table>
Add Comment
Please, Sign In to add comment