-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
81 lines (65 loc) · 2.9 KB
/
Copy pathscript.js
File metadata and controls
81 lines (65 loc) · 2.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
let keranjangBelanja = []; // Array untuk menyimpan item-item yang telah dibeli
fetch("https://dummyjson.com/products?limit=10")
.then((response) => response.json())
.then((data) => {
const produkContainer = document.getElementById("produk-container");
produkContainer.innerHTML = ""; // Kosongkan kontainer sebelum menambahkan produk baru
data.products.forEach((produk) => {
const item = document.createElement("div");
item.classList.add("item");
item.innerHTML = `
<img src="${produk.thumbnail}" alt="${produk.title}">
<h2>${produk.title}</h2>
<p>${produk.description}</p>
<p><strong>Rp ${produk.price.toLocaleString()}</strong></p>
<p class="rating">⭐ ${produk.rating}</p>
<button class="btn-beli" data-id="${produk.id}">Beli</button>
`;
produkContainer.appendChild(item);
});
// Tambahkan event listener setelah produk ditambahkan ke DOM
const buttons = document.querySelectorAll(".btn-beli");
buttons.forEach(button => {
button.addEventListener("click", (event) => {
const produkId = event.target.getAttribute("data-id");
const produk = data.products.find(p => p.id == produkId);
const produkInCart = keranjangBelanja.find(p => p.id == produkId);
if (produkInCart) {
produkInCart.jumlah++;
} else {
produk.jumlah = 1;
keranjangBelanja.push(produk);
}
alert("Produk berhasil ditambahkan ke keranjang!");
// Tampilkan keranjang belanja
tampilkanKeranjangBelanja();
});
});
})
.catch((error) => console.error("Gagal mengambil data:", error));
// Event listener untuk tombol "Lihat Keranjang Belanja"
document.getElementById("viewCart").addEventListener("click", () => {
document.getElementById("produk-container").style.display = "none";
document.getElementById("keranjang-container").style.display = "block";
});
// Fungsi untuk menampilkan keranjang belanja
function tampilkanKeranjangBelanja() {
const keranjangTableBody = document.querySelector("#keranjang-table tbody");
keranjangTableBody.innerHTML = ""; // Kosongkan kontainer sebelum menambahkan item baru
let totalSemuaHarga = 0;
keranjangBelanja.forEach((item) => {
const row = document.createElement("tr");
const totalHarga = item.price * item.jumlah;
totalSemuaHarga += totalHarga;
row.innerHTML = `
<td><img src="${item.thumbnail}" alt="${item.title}" width="50"></td>
<td>${item.title}</td>
<td>Rp ${item.price.toLocaleString()}</td>
<td>${item.jumlah}</td>
<td>${item.rating}</td>
<td>Rp ${totalHarga.toLocaleString()}</td>
`;
keranjangTableBody.appendChild(row);
});
document.getElementById("total-semua-harga").textContent = `Rp ${totalSemuaHarga.toLocaleString()}`;
}