css
.container { display: flex; flex-direction: column; max-width: 1200px; min-width: 800px; align-items: center; row-gap: 10px; margin-top: 30px; }
h1 { font-size: 1.6em; margin-bottom: 1em; }
.container>button { width: 500px; padding: 10px 0; }
.containerul { display: flex; flex-wrap: wrap; }
.containerulli { width: 25%; padding: 10px; text-align: center; margin: 20px 0; }
.containerimg { width: 100% }
.name { padding: 10px 0; font-weight: bold; }
.price { color: #999; font-size: 14px; }
html
<div class="container">
<h1>์ํ๋ชฉ๋ก</h1>
<button>์ํ๋ชฉ๋ก ๋ถ๋ฌ์ค๊ธฐ</button>
<ul>
</ul>
</div>
js
// import products from "https://sat2llite.github.io/data/db.json" assert { type: "json" };
//import products from "../../db.json" assert {type:'json'}
//assert {type:'json'} -์ธ๋ถํ์ผ์ด json์ด๋ผ๊ณ ํ์คํ๊ฒ ๋ช
์
let products;
// fetch ๊ธฐ๋ณธ ๋ฌธ๋ฒ
async function getJson() {
// return fetch("https://sat2llite.github.io/data/db.json")
// .then((response) => response.json)
// .then(products => products)
const response = await fetch('https://sat2llite.github.io/data/db.json');
products = await response.json();
return products;
}
getJson()
.then((products) => console.log(products));
const button = document.querySelector("button");
//li๋ง๋ค์ด์ ul์ ๋ฃ์ด์ฃผ๋ ํจ์
const createItem = (product) => {
const ul = document.querySelector("ul");
const li = document.createElement("li");
const img = document.createElement("img");
const p = document.createElement("p");
const span = document.createElement("span");
//๊ฐ๊ตญ์ ๋ง๋ ์ซ์ ์์์ ์ง์ํ๋ ๊ฐ์ฒด์ ์์ฑ์
const price = new Intl.NumberFormat("ko-KR", {
style: "currency", //ํตํ๋จ์
currency: "KRW", //์ํ
}).format(product.price); //ํฌ๋งท์ ๋ฐ๊ฟ ๋ฐ์ดํฐ
img.setAttribute("src", product.img);
li.id = product.id;
p.className = "name"; //css
p.innerHTML = product.name;
span.className = "price";
span.innerText = price;
li.append(img, p, span);
ul.append(li);
};
//๋ง๋ li๋ค์ด ๋ฐ๋ณต๋๊ฒ
const importData = () => {
products.data.map((product) => {
//๊ณ์ ์ถ๊ฐ๋๋ ๊ฒ์ ๋ฐฉ์ง(๋์ผํ์์ด๋๊ฐ์ด ์ด๋ฏธ ์์๋๋ ์๋X)
if (!document.getElementById(product.id)) {
createItem(product);
}
});
};
button.addEventListener("click", importData);
css
body { display: flex; justify-content: center; font-family: Verdana, Geneva, Tahoma, sans-serif; }
.container { display: flex; flex-direction: column; max-width: 700px; min-width: 500px; align-items: center; row-gap: 10px; margin-top: 30px; }
h1 { font-size: 1.6em; margin-bottom: 1em; }
.container>button { width: 500px; padding: 10px 0; }
.containerul { display: flex; flex-direction: column; row-gap: 20px; width: 100%; margin-bottom: 30px; }
.containerulli { display: flex; align-items: center; column-gap: 10px; padding: 10px; background: #d6d6d6; border-radius: 16px; }
.containerimg { width: 60px; }
.name { padding: 10px 0; font-weight: bold; width: 70%; }
.price { color: #999; font-size: 14px; color: #f01d1d; }
input[type="checkbox"] { width: 16px; height: 16px; }
.total { font-size: 20px; }
.total_price { color: #1d21f0; font-size: 26px; }
html
<div class="container">
<h1>์ํ๋ชฉ๋ก</h1>
<button class="mainBtn">์ํ๋ชฉ๋ก ๋ถ๋ฌ์ค๊ธฐ</button>
<ul>
</ul>
<div class="total">
<span>Total : </span>
<span class="total_price">โฉ0</span>
</div>
</div>
js
// import products from "https://sat2llite.github.io/data/db.json" assert { type: "json" };
//import products from "../../db.json" assert {type:'json'}
//assert {type:'json'} -์ธ๋ถํ์ผ์ด json์ด๋ผ๊ณ ํ์คํ๊ฒ ๋ช
์
let products; // products ์ ์ฒด
let myProduct; // products.data
let selected = []; // ์ฒดํฌ๋ ์์ดํ
์ ์ ์ฅํ ๋ฐฐ์ด
// fetch ๊ธฐ๋ณธ ๋ฌธ๋ฒ
async function getJson() {
// return fetch("https://sat2llite.github.io/data/db.json")
// .then((response) => response.json)
// .then(products => products)
const response = await fetch(
"https://mlmlmlml00700700700.github.io/data/db.json"
);
products = await response.json();
return products;
}
getJson().then((products) => importData(products));
const button = document.querySelector("button");
// ๊ณ์ฐ๊ฐ์ ํ๋ฉด์ ๋ณด์ฌ์ฃผ๋ ํจ์ ์ ์
const printTotal = (price) => {
const spanResult = document.querySelector(".total_price");
//๊ฐ๊ตญ์ ๋ง๋ ์ซ์ ์์์ ์ง์ํ๋ ๊ฐ์ฒด์ ์์ฑ์
const priceFormat = new Intl.NumberFormat("ko-KR", {
style: "currency", //ํตํ๋จ์
currency: "KRW", //์ํ
}).format(price); //ํฌ๋งท์ ๋ฐ๊ฟ ๋ฐ์ดํฐ
spanResult.innerText = priceFormat;
}
// ๊ณ์ฐํด์ฃผ๋ ํจ์ ์ ์
const calculate = () => {
const result = selected.reduce((acc, current) => { // acc - ์ด ํฉ๊ณ, current - ๋ฐฐ์ด์ ์์ดํ
return acc + current.price; // += ์จ๋ ๋จ
}, 0);
// console.log(result);
printTotal(result); // ํ๋ฉด์ ๋ณด์ฌ์ฃผ๋ ํจ์ ํธ์ถ
};
// ์ฒดํฌ๋ฐ์ค๋ฅผ ์ฒดํฌํ๋ฉด ์คํ ๋๋ ํจ์
const addCart = (event) => {
// console.log(event.target.parentElement.id);
// ๊ตฌ์กฐ๋ถํด
const { id } = event.target.parentElement; // ์์ด๋ (์ด๋ค li๋ฅผ ์ ํํ๋์ง)
const { checked } = event.target; // ์ ํ(true/false)
console.log(id, checked);
if (checked) {
// ์ฒดํฌ ๋์์ ๊ฒฝ์ฐ
myProduct.forEach((aa) => {
if (aa.id === parseInt(id)) {
// ๋ฌธ์์ด์ ์ ์๊ฐ์ผ๋ก ๋ณํ ( == )
selected.push(aa);
}
});
} else {
// ์ฒดํฌ๊ฐ ํด์ ๋๋ฉด ๋ฐฐ์ด์์ ์ญ์
selected = selected.filter((bb) => {
return bb.id !== parseInt(id);
});
}
console.log(selected);
calculate(); // ๊ณ์ฐํด์ฃผ๋ ํจ์ ํธ์ถ
};
//li๋ง๋ค์ด์ ul์ ๋ฃ์ด์ฃผ๋ ํจ์
const createItem = (product) => {
const ul = document.querySelector("ul");
const li = document.createElement("li");
const img = document.createElement("img");
const p = document.createElement("p");
const span = document.createElement("span");
const check = document.createElement("input");
//๊ฐ๊ตญ์ ๋ง๋ ์ซ์ ์์์ ์ง์ํ๋ ๊ฐ์ฒด์ ์์ฑ์
const price = new Intl.NumberFormat("ko-KR", {
style: "currency", //ํตํ๋จ์
currency: "KRW", //์ํ
}).format(product.price); //ํฌ๋งท์ ๋ฐ๊ฟ ๋ฐ์ดํฐ
img.setAttribute("src", product.img);
li.id = product.id;
p.className = "name"; //css
p.innerHTML = product.name;
span.className = "price";
span.innerText = price;
// check(input)์ ํ์
์ถ๊ฐ, ์ด๋ฒคํธ๋ฆฌ์ค๋ ์ถ๊ฐ
check.setAttribute("type", "checkbox");
check.addEventListener("change", addCart); // ์ฒดํฌ๋ฐ์ค์ ์ฒดํฌ๋๋ฉด addCart ํจ์ ์คํ
li.append(check, img, p, span);
ul.append(li);
};
//๋ง๋ li๋ค์ด ๋ฐ๋ณต๋๊ฒ
const importData = () => {
myProduct = products.data;
myProduct.map((product) => {
//๊ณ์ ์ถ๊ฐ๋๋ ๊ฒ์ ๋ฐฉ์ง(๋์ผํ์์ด๋๊ฐ์ด ์ด๋ฏธ ์์๋๋ ์๋X)
if (!document.getElementById(product.id)) {
createItem(product);
}
});
};
// button.addEventListener("click", importData);