ํฐ์คํ ๋ฆฌ ๋ทฐ
[movie PJ_day4] ์ํ ๊ฒ์ ํ์ด์ง ๊ตฌํ - ๊ฒ์ ๊ธฐ๋ฅ ๊ตฌํ
์ฑ._. 2025. 1. 13. 22:10์ด์ ์ํฉ
10์ผ์ ๊ณผ์

์ด๋ ค์ํ๋ ํ์๋ค์ ์ํด ํํฐ๋๊ป์ ํต์ฌ ๊ตฌํ ๊ธฐ๋ฅ๋ค์ ์ค๋ช ํด์ฃผ์ จ๊ณ
์ฃผ๋ง๋์ ์ด์ฌํ ์ดํด๋ณด์๋ค.
๊ฐ์ธ๊ณผ์ tip
[๋ผ๋ ๋ง๋ค๊ธฐ]
- ์์๊ฐ ์๋ ๋ฆฌ์คํธ์ธ ulํ๊ทธ์ ๊ทธ ๋ด๋ถ์ liํ๊ทธ ์์ฑ -> ๋ผ๋ ๋ง๋ค๊ธฐ
- DOM์์ ul ํ๊ทธ์ ์ ๊ทผํ์ฌ innerHTML์ ์ฝ์ ํ๊ธฐ
[๋ฐ์ดํฐ ๊ฐ์ ธ์ค๊ธฐ]
- fetch ํจ์ ์ฌ์ฉํ๊ธฐ
1-1. (์ด๋๊ฐ์ ์ ์๋์ด์๋) fetchํจ์๋ ‘promise’๋ผ๋ ๊ฐ์ฒด๋ฅผ return(๋ฐํ)ํ๋ค.
1-2. Promise ๊ฐ์ฒด๋ ์ฌ๋ฌ ์์ฑ, ๋ฉ์๋ ์ค์ ‘then’์ ํฌํจํ๋ค.
1-3. Then ๋ฉ์๋๋ ํจ์๋ฅผ ์ธํ(์ธ์)์ผ๋ก ๊ฐ๋๋ค.
1-4. ์ด ํจ์์๋ promise ๊ฐ์ฒด์ return ๊ฐ์ ๋ฃ์ด์ค๋ค.
์ฝ๋ฐฑํจ์ ์์์๋ res.json()์ ํตํด์ JS์์ ํ์ฉ ๊ฐ๋ฅํ ๊ฐ์ฒดํํ๋ก ๋ฐ๊ฟ์ค๋ค.
1-5. json() ๋ promise๋ฅผ return ํ๊ธฐ ๋๋ฌธ์ ๋ .then์ผ๋ก ๋ฐ์ ์ ์๋ค.
1-6. ๊ฒฐ๊ตญ JS์์ ํ์ฉ๊ฐ๋ฅํ array ํํ๋ก ๊ฒฐ๊ณผ๊ฐ์ ์ป์ ์ ์๋ค.
[๋ฐ์ดํฐ ํ์ฉํ๊ธฐ]
- array(๋ฐฐ์ด) ํํ๋ก ๋ฐ์์จ 20๊ฐ์ ๊ฐ์ ํ์ฉํ๋ค.
1-1. .innerHTML๋ฅผ ํตํด ulํ๊ทธ ์์ 20๊ฐ์ liํ๊ทธ๋ก ๊ฐ์ ๋ํ๋ธ๋ค. - liํ๊ทธ ๋น ๋ฌถ์์(let emptyStr = “”) ๋ง๋ค๊ณ for๋ฌธ ๋ด๋ถ์ liํ๊ทธ๋ฅผ ๋ง๋ค๊ธฐ
+) <li>๋ด์ data-id="${res[i].id}" ๋ ๋์ค์ ๋ช ๋ฒ์งธ id์ธ์ง ํ์ธํ๊ธฐ ์ํ ๊ฒ์ผ๋ก ๋ฑํ ํ์ ์๊ธดํ๋ค..!
+) emptyStr์ ๋์ ํ li ํ๊ทธ๋ ``์ผ๋ก ๊ฐ์ธ์ค๋ค.(temp_html๊ณผ ๊ฐ์ ๋ฐฉ์) - emptyStr์ ulํ๊ทธ์ ์ฐ๊ฒฐํ๊ธฐ
6-1. for๋ฌธ์ด ๋๋๊ณ .innerHTML์ ํตํด ๋ด์ฉ์ ๋ฃ์ด์ค๋ค.
(resultUl.innerHTML = emptyStr)
[๊ฒ์ ๊ธฐ๋ฅ ์คํํ๊ธฐ]
- UI ๋ง๋ค๊ธฐ : inputํ๊ทธ์ buttonํ๊ทธ
- ๋ฒํผ์ ์ด๋ฒคํธ๋ฆฌ์ค๋ ๋ถ์ฌํ๊ธฐ
- input์ ์ ๋ ฅ๋ ๊ฐ์ ๊ฐ์ ธ์ค๊ธฐ => .value
- ๊ทธ ๊ฐ์ผ๋ก list๋ฅผ ํํฐ๋ง
*๊ฒ์์ ์ํด์๋ 20๊ฐ์ ์ ๋ณด๊ฐ ์ ์ฅ๋ ๊ณณ์ ์ ๊ทผ์ ํด์ผ ํจ*
=> script ์ ์ฒด์์ ๋ฐ์ดํฐ๋ฅผ ๊ด๋ฆฌํ ์ ์๋ ๋ณ์๋ก ์ ์ธํ๊ธฐ
+)[ํจ์ ๋๋๊ธฐ]
1. ๋ฐ์ดํฐ๋ฅผ ์ฝ์ด์ค๋ ํจ์
2. ๋ฐ์ดํฐ๋ฅผ ๋ฟ๋ ค์ฃผ๋ ํจ์ (displayPosts)
5. ๋ฒํผ์ ๋๋ ์ ๋ ๊ฒ์ ๋ด์ฉ์ ํํฐ๋งํ๊ธฐ
[filter ํจ์]
- ์ธ์๋ก ํจ์๋ฅผ ๊ฐ๋๋ค.
- ์ฝ๋ฐฑํจ์์ return์๋ ์กฐ๊ฑด์ ๋ถ์ฌํ๋ค.
filter(function(์ฌ๊ธฐ){
return ์ฌ๊ธฐ >=5;
});
๐ ๊ตฌํํ ์ฝ๋
const moviesUl = document.querySelector("#movies");
const btn = document.querySelector(".btn");
const searchInput = document.querySelector("#searchInput");
const modal = document.querySelector(".modal");
const close = document.querySelector(".close");
let postArray = [];
console.log(postArray);
function fetchData() {
fetch(
"https://api.themoviedb.org/3/movie/popular?api_key=${key}&language=ko-KR&page=1"
)
.then(function (res) {
return res.json();
})
.then(function (๊ฒฐ๊ณผ) {
console.log(๊ฒฐ๊ณผ);
let postArray = ๊ฒฐ๊ณผ.results;
displayPost(๊ฒฐ๊ณผ);
});
}
fetchData();
function displayPost(posts) {
emptyStr = "";
for (let i = 0; i < posts.results.length; i++) {
emptyStr += `<li class="item" data-id="${posts.results[i].id}">${posts.results[i].title}</li>`;
}
moviesUl.innerHTML = emptyStr;
}
btn.addEventListener("click", function () {
const keyword = searchInput.value;
const filterPosts = postArray.filter(function (์ํ) {
return ์ํ.title.includes(keyword);
});
console.log(filterPosts);
displayPost(filterPosts);
});
moviesUl.addEventListener("click", function (e) {
console.log(e.target);
// ์๋กญ๊ฒ fetching ํ๋ ๋ก์ง(item ํ๋์ ๋ํด์)
modal.style.display = "block";
});
close.addEventListener("click", function () {
modal.style.display = "none";
});
ํฌ๊ฒ fetchData() / displayPost() / btn.addEvent / moviesUl.addEvent
๋ก ๋๋์ด์ ๋ณด๋ฉด ๋๋ค.
๋ฌธ์ ์ํฉ ๋ฐ ์ถ๋ก
โ๋ฌธ์ 1. Filter ๋ฉ์๋๊ฐ ์๋ํ์ง ์์ -> postArray ๊ฐ ๋ฐฐ์ด์ด ์๋๊ฐ?
โ๋ฌธ์ 2.postArray = []; ๊ฐ ์ ์ญ๋ณ์๋ก์ ์ ์ธ์ด ์ ๋๋ ๋ฏ ํจ
โ๋ฌธ์ 3. console.log(postArray); ๋ฅผ ํ์ผ๋ ๋น ๋ฐฐ์ด์ด ์ฐํ
โ๋ฌธ์ 4. displayPost ๋ด๋ถ์ for๋ฌธ์์ .results๋ฅผ ์ ๊ฑฐ์ ํ๋ฉด์ ๋ฆฌ์คํธ๊ฐ ์ฌ๋ผ์ง
๋ฌธ์ ํด์
ํํฐ๋๊ป์ ๊ตฌํํ์๋ API ๋งํฌ์ ๋ด๊ฐ ์ฌ์ฉํ๋ ๊ฒ์ด ๋ค๋ฅด๊ธฐ ๋๋ฌธ์ ์ํฉ์ ๋ง๊ฒ ๋ณํํ์ง๋ง,
์ฌ๋ฌ ๋ฒ ์๋ํด๋ ์ ํ๋ฆฌ๋ ๋ถ๋ถ์ ์ฌ์ญค๋ดค๋ค.
๊ทธ ๋ต์,
๐๋ฌธ์ 2.
=> postArray์ ๋ฐฐ์ด ๊ฐ์ ํ์ธํ๋ ค๋ฉด ์ง๊ธ ์์ค์์๋ ์ด๋ ค์.
๋งฅ๋ฝ์ ์ดํด๋ณด๊ณ ๋ด๊ฒจ์๋ค๊ณ ๊ฐ์ ํ์ฌ ์งํํด์ผ ํจ.
๐๋ฌธ์ 3.
=> fetchData()์ ์ธํ์ผ๋ก ‘๊ฒฐ๊ณผ’๊ฐ ์๋ ‘postArray’ ์ฆ, ๊ฐ์ฒด ‘๊ฒฐ๊ณผ’ ์ค results์ ๊ฐ์ ๋ฃ์ด ํ๋ฉด์ ์ ๋ชฉ ๋ฆฌ์คํธ๋ฅผ ๋์ด๋ค.
&
difplayPost์ ์ธํ(posts)๋ ๊ฒฐ๊ณผ์ results๋ง ๋ด์ ๋ฐฐ์ด์ด๊ธฐ ๋๋ฌธ์ ๋ด๋ถ for๋ฌธ์์ .results๋ฅผ ์ ๊ฑฐํด์ผ ํจ.
๐๋ฌธ์ 4.
=> postArray = ๊ฒฐ๊ณผ.results ๋ก ์ฌ์ ์ธํ๊ธฐ์ ๋ฆฌ์คํธ๊ฐ ํ๋ฉด์ ์ ๋ธ.
์ค์ํ ๊ฒ์ ์ด๊ณณ์ ๊ณณ์์ ์ฌ์ฉ๋ displayPost์ ์ง์คํ์ฌ
์ด๋ ๊ณณ์์ ์ด๋ป๊ฒ ์ฌ์ฉ๋๋์ง, ์ธํ์ผ๋ก ์ด๋ค ๊ฒ์ด ๋ค์ด์ค๊ณ ์์ํ์ผ๋ก ์ด๋ค ๊ฒ์ด ๋๊ฐ๋ ์ง์ ๋ํด ์ดํดํ๋ ๊ฒ์ด๋ค.
๋ฌธ์ ํด๊ฒฐ
๐ ๊ฒ์ ๊ธฐ๋ฅ ๊ตฌํ ์๋ฃ
๋ค์ ๋จ๊ณ์ธ '๋ชจ๋ฌ' ๊ธฐ๋ฅ ๊ตฌํ ์ง์ !
//main2.js : ์๋ฐ์คํฌ๋ฆฝํธ ๊ธฐ๋ฅ ๊ตฌํ ๋ก์ง์.
const btn = document.querySelector(".btn");
const searchInput = document.querySelector("#searchInput");
const modal = document.querySelector(".modal");
const close = document.querySelector(".close");
const baseImgUrl = "https://image.tmdb.org/t/p/w500";
const cardSection = document.querySelector(".cards");
let postArray = [];
console.log(postArray); // ๋น ๋ฐฐ์ด์ธ ๊ฒ ๋ง์ผ๋ฉฐ, ์ง๊ธ ์์ค์์๋ ํ์ธํ๊ธฐ ์ด๋ ค์
function fetchData() {
fetch(
"https://api.themoviedb.org/3/movie/popular?api_key=${key}&language=ko-KR&page=1"
)
.then(function (res) {
return res.json();
})
.then(function (๊ฒฐ๊ณผ) {
console.log(๊ฒฐ๊ณผ);
postArray = ๊ฒฐ๊ณผ.results;
// input : ๊ฐ์ฒด '๊ฒฐ๊ณผ' ์ค results์ ๊ฐ (๋ฐฐ์ด)
displayPost(postArray);
});
}
fetchData();
// input : ๊ฒฐ๊ณผ์ results๋ง ๋ด์ ๋ฐฐ์ด
function displayPost(posts) {
temp_html = "";
for (let i = 0; i < posts.length; i++) {
temp_html += `
<div data-id="${posts[i].id}" class="mvCard">
<img id="poster" src="${
baseImgUrl + posts[i].poster_path
} class="card-img-top alt="poster">
<p>${posts[i].title}</p>
<p>ํ์ : ${posts[i].vote_average}<span id="mvaver"></span></p>
</div>`;
}
cardSection.innerHTML = temp_html;
}
btn.addEventListener("click", function () {
const keyword = searchInput.value;
const filterPosts = postArray.filter(function (์ํ) {
return ์ํ.title.includes(keyword);
});
console.log(filterPosts);
displayPost(filterPosts);
});
cardSection.addEventListener("click", function (e) {
console.log(e.target.closest(".mvCard"));
modal.style.display = "block";
});
close.addEventListener("click", function () {
modal.style.display = "none";
});
closest(".mvCard");
'Forntend > Trouble Shooting' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| Live Server ์ค๋ฅ ๋ฐ ํด๊ฒฐ๋ฐฉ๋ฒ - vscode ์ด๊ธฐํ(๋ ๋ฌด์์) (0) | 2025.01.19 |
|---|---|
| ๋ถ๋งํฌ ๊ธฐ๋ฅ ๊ตฌํํ๊ธฐ(1) - ์ด๋ฒคํธ ์์ (0) | 2025.01.15 |
| ๋ชจ๋ฌ์ฐฝ ๋ซ๊ธฐ ๋ฒํผ ์ค๋ฅ - innerHTML ์ฌ์ฉ ์ ์ด๋ฒคํธ๋ฆฌ์ค๋ ์ค๋ฅ (0) | 2025.01.15 |
| [movie PJ_day5] ์นด๋์ id๋ฅผ ์ด๋ป๊ฒ ๊ฐ์ ธ์ค์ง? - closest(), getAttribute() (0) | 2025.01.14 |
| for๋ฌธ์ด ๋จ์ง ์๋๋ค. ์??? (0) | 2025.01.12 |