gurumutant

Populate HTML Table from API

Sep 6th, 2026 (edited)
39,131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 1.53 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6.     <title>Document</title>
  7.     <script src="https://code.jquery.com/jquery-4.0.0.min.js" integrity="sha256-OaVG6prZf4v69dPg6PhVattBXkcOWQB62pdZ3ORyrao=" crossorigin="anonymous"></script>
  8.     <style>
  9.         table, th, td {
  10.             border: 1px solid powderblue;
  11.             border-collapse: collapse;
  12.             padding: 6px;
  13.         }
  14.  
  15.         table {
  16.             font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
  17.         }
  18.     </style>
  19. </head>
  20. <body>
  21.     <table id="naruto-chars">
  22.         <thead>
  23.             <tr>
  24.                 <th>ID</th>
  25.                 <th>Character Name</th>
  26.                 <th>Photo</th>
  27.             </tr>
  28.         </thead>
  29.         <!-- tbody cukup dikosongkan karena akan diisi dari JSON -->
  30.         <tbody></tbody>
  31.     </table>    
  32.    
  33.     <script>
  34.         // ambil JSON dari server API
  35.         $.getJSON('https://dattebayo-api.onrender.com/characters', function(data) {
  36.             // modifikasi tabel yang sudah disiapkan dengan
  37.             // menyisipkan baris dari JSON
  38.             $('#naruto-chars tbody').html(
  39.                 $.map(data.characters, char =>
  40.                     `<tr><td>${char.id}</td>
  41.                     <td>${char.name}</td>
  42.                     <td><img src="${char.images[0]}" width="100"></td>
  43.                     </tr>`
  44.                 ).join('')
  45.             );
  46.         });
  47.     </script>
  48. </body>
  49. </html>
Advertisement
Add Comment
Please, Sign In to add comment