H2: CodeSandBox

Loome CodeSandboxis Vanilla JavaScript projekti, mis kuvab JSON kujul autode andmed HTML tabelina.

  1. Avame CodeSandbox.io ja loome uue “Vanilla” malli põhise projekti
  2. index.js failis kirjeldame JSON massiivi myjson, kus iga element sisaldab ühe auto andmeid (värv, tumendatud aknad, rataste arv, pilt, katusekoormus, meelelahutus, lisavarustus)
  3. map() ja join() abil genereerime myjson massiivist HTML tabeli ning lisame selle leheküljele document.getElementById(“app”).innerHTML kaudu

index.html

<!DOCTYPE html>
<html>
  <head>
    <title>JavaScript Sandbox</title>
    <meta charset="UTF-8" />
  </head>

  <body>
    <div id="app"></div>

    <script src="./index.mjs" type="module"></script>
  </body>
</html>

index.mjs

import "./styles.css";

const myjson = [
  {
    Car: {
      Color: "Rose red",
      "Tinted windows": false,
      Wheels: 4,
      Image:
        "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRNFJo3hJ9y9CJacbJA7yc23ATLAlaFsLtGXj9Ozp9a_A&s=10",
      "Roof load": "none",
      Entertainment: [
        "FM radio",
        "MP3, MP4 and MKV player",
        "Harman/Kardon speakers",
      ],
      Accessories: ["satellite navigation", "cruise control"],
    },
  },
  {
    Car: {
      Color: "Dark blue",
      "Tinted windows": true,
      Wheels: 4,
      Image:
        "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRNFJo3hJ9y9CJacbJA7yc23ATLAlaFsLtGXj9Ozp9a_A&s=10",
      "Roof load": "Thule roof rack",
      Entertainment: [
        "FM radio",
        "Apple CarPlay/Android Auto",
        "Bowers & Wilkins Premium Sound speakers",
      ],
      Accessories: ["autonomous driving system", "luggage cover"],
    },
  },
  {
    Car: {
      Color: "White",
      "Tinted windows": true,
      Wheels: 4,
      Image:
        "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTeAMRmI0YM76Ms2wVy0-09RxTZYwKuExwUGuJ86wXkgLUHmqeokgzPG-Vc&s=10",
      "Roof load": "none",
      Entertainment: [
        "Radio",
        "Android Auto",
        "Bowers & Wilkins Premium Sound speakers",
      ],
      Accessories: ["autonomous driving system", "luggage cover"],
    },
  },
];

document.getElementById("app").innerHTML = `
<div id="json">
  <h1>Car properties</h1>
  <table>
  <tr><td>Nimi</td>
  <td>Color</td>
  <td>Tinted windows</td>
  <td>Wheels</td>
  <td>Image</td>
  <td>Roof load</td>
  <td>Entertainment</td>
  <td>Accessories</td></tr>
  <tbody>
  ${myjson
    .map(
      (item, i) => `
      
      <tr><td>
    <h2>Car ${i + 1}</h2></td>
    <td>
    <p> ${item.Car.Color}</p></td>
    <td>
    <p> ${item.Car["Tinted windows"]}</p></td>
    <td> <p> ${item.Car.Wheels}</p></td>
    <td><img src="${myjson[i].Car.Image}" alt="${
        myjson[i].Car.Color
      } car" width="200"></td>
      <td><p>${item.Car["Roof load"]}</p></td>
      <td><p> ${item.Car.Entertainment.join(", ")}</p></td>
      <td><p> ${item.Car.Accessories.join(", ")}</p></td></tr>
     
  `
    )
    .join("")}
    </tbody>
    </table>
</div>
`;

styles.css

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: #f5f5f5;
  margin: 0;
  padding: 20px;
  color: #222;
}

#json {
  max-width: 600px;
  margin: 0 auto;
}

h1 {
  font-size: 24px;
  margin-bottom: 20px;
}

h2 {
  font-size: 18px;
  margin-top: 30px;
  margin-bottom: 10px;
  padding-bottom: 6px;
  border-bottom: 2px solid #ddd;
}

p {
  margin: 6px 0;
  line-height: 1.5;
}
table {
  width: 100%;
  border-collapse: collapse;
  margin-top: 20px;
  background: #fff;
}

th,
td {
  border: 1px solid #ddd;
  padding: 10px 12px;
  text-align: left;
  vertical-align: top;
}

th {
  background: #eee;
  font-weight: 600;
}

tbody tr:nth-child(even) {
  background: #fafafa;
}