728x90
๋ฐ์ํ
๐ก ์ค์ต ํฌ์ธํธ!

- ์ค๋์ ์ํ ์ง์ด ๋ฐ ์ํ ์ ๋ ฌ ๊ธฐ๋ฅ์ ๋ง๋ค์ด ๋ณด์๋ค.
- ์๋ ๊ฐ์์์ ๊ฐ์ฌ๋์ด ์งํํ ๋ฐฉ์๋๋ก๋ ํด๋ณธ ํ, "์ด๋ฐ ๋ฐฉ์์ผ๋ก ํ๋ฉด ์ฝ๋๋ฅผ ์ข ๋ ์ค์ผ ์ ์์๊น" ๋ผ๋ ์๊ฐ์ผ๋ก ์๋ก์ด ๋ฐฉ์์ผ๋ก ์งํํด๋ณด์๋ค. (์์ง ๋ฐฐ์ฐ๋ ๊ณผ์ ์ด๋ผ.. ๋ญ๊ฐ ๋ ๋์ ๋ฐฉ์์ธ์ง๋ ๋ชจ๋ฅด๊ฒ ๊ณ ๊ณ ๋ฏผํด๋ดค๋ค๋ ๋ฐ์ ์์๋ฅผ ๋๋ค ใ ใ )
- ์ฐ์ ์ํ ์ง์ด์ ๊ฒฝ์ฐ, html ํ์ผ์ ๋ ์ด์์์ ํ๋์ฝ๋ฉ์ ํด๋๊ณ forEach ํจ์๋ฅผ ์ฌ์ฉํ์ฌ ๋ฐ์ดํฐ๋ฐ์ธ๋ฉ ํ, ์ง์ด ๊ธฐ๋ฅ์ ๋ง๋ค์ด๋๊ฐ๋ฉด์ ํ๋์ฝ๋ฉํ ๋ถ๋ถ์ ์ง์๋๊ฐ๋ค.
- ๊ทธ ๋ค์ ์์ฃผ ์ฌ์ฉ๋๋ JavaScript ๋ฌธ๋ฒ์ธ sort, filter ๋ฅผ ์ฌ์ฉํ์ฌ ๊ฐ๋๋ค์ ์ ๋ ฌ, ๊ฐ๊ฒฉ์ ์ ๋ ฌ, 6๋ง์ ์ดํ ๋ณด๊ธฐ(ํํฐ) ๊ธฐ๋ฅ์ ๋ฃ์ด๋ณด์๋ค.
- ๋ง์ง๋ง์ผ๋ก ์ฒซํ๋ฉด์ผ๋ก ๋๋๋ฆฌ๋ ์๋๋๋ก ๊ธฐ๋ฅ์ ์ถ๊ฐํ๋ค.
- ์ถํ์๋ ์ฌ์ฉ์๊ฐ ์ํ๋ ๊ฐ๊ฒฉ๋๋ฅผ ์ ๋ ฅํ๋ฉด ๊ฑฐ๊ธฐ์ ๋ง์ถฐ ํํฐ๋ง๋๋๋ก ๊ตฌํํด๋ด์ผ๊ฒ ๋ค.
๐ ์ํ ์ง์ด
๐ค ์ด๋ฐ JavaScript
// ์ํ ๋ฐ์ดํฐ
var products = [
{ id : 0, price : 70000, title : 'Blossom Dress' },
{ id : 1, price : 50000, title : 'Springfield Shirt' },
{ id : 2, price : 60000, title : 'Black Monastery' }
]
- ๋ฐ์ดํฐ ๋ฐ์ธ๋ฉ์ ํด์ผํ๋ ๋ฐ์ดํฐ ์๋ฃ์ด๋ค.
๐ค ์ด๋ฐ HTML
<div class="card-group container">
<div class="card">
<img src="https://via.placeholder.com/600">
<div class="card-body">
<h5>Card title</h5>
<p>๊ฐ๊ฒฉ : 70000</p>
<button class="btn btn-danger">์ฃผ๋ฌธํ๊ธฐ</button>
</div>
</div>
<div class="card">
<img src="https://via.placeholder.com/600">
<div class="card-body">
<h5>Card title</h5>
<p>๊ฐ๊ฒฉ : 70000</p>
<button class="btn btn-danger">์ฃผ๋ฌธํ๊ธฐ</button>
</div>
</div>
<div class="card">
<img src="https://via.placeholder.com/600">
<div class="card-body">
<h5>Card title</h5>
<p>๊ฐ๊ฒฉ : 70000</p>
<button class="btn btn-danger">์ฃผ๋ฌธํ๊ธฐ</button>
</div>
</div>
<div class="container">
<button class="btn btn-danger mt-3" id="sortAbc">๊ฐ๋๋ค์ ์ ๋ ฌ</button>
<button class="btn btn-danger mt-3" id="sortPrice">๊ฐ๊ฒฉ์ ์ ๋ ฌ</button>
<button class="btn btn-danger mt-3" id="filterPrice">6๋ง์ ์ดํ ๋ณด๊ธฐ</button>
<button class="btn btn-danger mt-3" id="revert">์๋๋๋ก</button>
</div>
</div>
- ์ฒ์์ ํ๋์ฝ๋ฉ๋ html ํ์ผ์ ๋ณด๋ฉด ์ ์ ์๋ฏ์ด card ํด๋์ค๊ฐ ์ํ์ ๊ฐ์๋งํผ ๋ฐ๋ณต๋๋ ํํ๋ฅผ ๋๊ณ ์๋ค.
- ๋ฐ๋ผ์, card ํด๋์ค๋ฅผ ํ ํ๋ฆฟํํ ํ ํ์ํ ๋ฐ์ดํฐ๋ฅผ ๋ฃ์ด์ card-group container ํด๋์ค์ ๋ถ์ฌ์ฃผ๋ฉด ๋ ๊ฒ ๊ฐ์๋ค.
- ๊ทธ๋ ๊ฒ ์ํ ๊ฐ์๋งํผ ๋ง๋ค๋ฉด ์นด๋ ๋ ์ด์์ ๋ง๋ค๊ธฐ ๋!
๐ค ํ๋ฐ JavaScript
// ํจ์๋ก ๋ฐ๊พธ๊ธฐ ์
products.forEach(item => {
let template = `
<div class="card">
<img src="https://via.placeholder.com/600">
<div class="card-body">
<h5 class="title">${item.title}</h5>
<p class="price">๊ฐ๊ฒฉ : ${item.price}</p>
<button class="btn btn-danger">์ฃผ๋ฌธํ๊ธฐ</button>
</div>
</div>
`
$('#items').append(template);
});
// ํจ์๋ก ๋ฐ๊พผ ํ
function layout(items){
items.forEach(item => {
let template = `
<div class="card">
<img src="https://via.placeholder.com/600">
<div class="card-body">
<h5 class="title">${item.title}</h5>
<p class="price">๊ฐ๊ฒฉ : ${item.price}</p>
<button class="btn btn-danger">์ฃผ๋ฌธํ๊ธฐ</button>
</div>
</div>
`
$('#items').append(template);
});
}
- ์ผ๋จ ์์ฑํด์ผํ๋ card ํด๋์ค๊ฐ ํ๋๋ผ๊ณ ์๊ฐ์ ํด๋ณด์.
- card ํด๋์ค ๋ด์ฉ ์ ์ฒด๋ฅผ template ์ด๋ผ๋ ๋ณ์์ ๋ด๊ณ , ๊ทธ ๋ณ์๋ฅผ card-group container ํด๋์ค์ append ํด์ฃผ๋ฉด ๋๋ค. (ํธ์์ card-group container ํด๋์ค ๋์ items ๋ผ๋ id ๋ฅผ ์ถ๊ฐํ์ฌ ์ฌ์ฉํ์๋ค.)
- ๊ทธ๋ ๋ค๋ฉด ์์ฑํด์ผํ๋ card ํด๋์ค๊ฐ ์ฌ๋ฌ ๊ฐ, ์ฆ ๋ฐ์ดํฐ ๊ฐ์๋งํผ์ด๋ผ๋ฉด? for ๋ฌธ์ด๋ forEach ๋ฌธ ๋ฑ์ ์ด์ฉํ๋ฉด ๋๋ค.
- ๋๋ forEach ๋ฌธ์ ์ฌ์ฉํ๋ค. ๋ฐ์ดํฐ ๋ฐ์ธ๋ฉ์ด ํ์ํ products ๋ฐฐ์ด ๋ด์ ์๋ฃ๋ค์ item ์ด๋ผ๋ ํ๋ผ๋ฏธํฐ๋ก ๋ฐ์ ํ๋์ฉ ๋๋ฉด์, ํ์ดํ์ด ๋ค์ด๊ฐ ๋ถ๋ถ๊ณผ ๊ฐ๊ฒฉ์ด ๋ค์ด๊ฐ ๋ถ๋ถ์ ๋ฐ์ธ๋ฉํด์ฃผ๋ฉด์ ๋ ์ด์์์ ์์ฑํ ์ ์๊ฒ ํด์ฃผ์๋ค.
- ์ฒ์์๋ ํจ์๋ก ๋ง๋ค ์๊ฐ์ ์์๋๋ฐ.. ์ ๋ ฌ ๊ธฐ๋ฅ์ด๋ ํํฐ ๊ธฐ๋ฅ์ ์ถ๊ฐํ๋ฉด์ ๊ณ์ ๋ ์ด์์์ ๋์ผํ๊ฒ ์ ์ฉํด์ผ ํ๋ ์ผ์ด ๋ฐ์ํ๋ค.
- ๋ฐ๋ณตํด์ ์จ์ผ ํ๋ค๋ฉด ๊น๋ํ๊ฒ ํจ์๋ก ๋ง๋๋ ๊ฒ ๋ซ์ง ์์๊น ํด์ layout ์ด๋ผ๋ ์ด๋ฆ์ ๋ถ์ฌ ํจ์๋ก ๋ง๋ค์ด๋ณด์๋ค.
- layout ํจ์์ ํ๋ผ๋ฏธํฐ๋ products ๋ก ํ๋ฉด ๊ดํ ๋ด๊ฐ ํท๊ฐ๋ฆด ๊ฒ ๊ฐ์์ items ๋ก ํ๋ค.
- ์ฌ๊ธฐ๊น์ง ์์ฑํ ํ ํ๋์ฝ๋ฉํ๋ ๋ถ๋ถ๋ค์ ๋ชจ๋ ์ง์์ฃผ์๋ค. ๊ทธ๋ฌ๋ฉด ์ฒซํ๋ฉด์ ๋ฒํผ๋ค๋ง ๋ฌ๋ค.
- ํจ์๋ก ๋ฐ๊พธ๊ธฐ ์ ์๋ ๋ฐ๋ก ๋ ์ด์์์ด ๋จ์ง๋ง, ํจ์๋ก ๋ฐ๊พธ์ด์ฃผ์๊ธฐ ๋๋ฌธ์ ํจ์๋ฅผ ์คํํด์ผ ๋ ์ด์์์ด ์ ๋๋ก ์ ์ฉ๋๋ค.
- ๊ทธ๋ฌ๋ฏ๋ก ์ด๊ธฐํ๋ฉด์ด๋ผ๋ ์ฃผ์์ ๋ฌ๊ณ layout(products); ๋ผ๊ณ ์ฝ๋๋ฅผ ๋ฃ์ด์ฃผ์๋ค.
๐ค ํ๋ฐ HTML
<div class="container">
<div class="card-group container" id="items"></div>
<div class="container">
<button class="btn btn-danger mt-3" id="sortAbc">๊ฐ๋๋ค์ ์ ๋ ฌ</button>
<button class="btn btn-danger mt-3" id="sortPrice">๊ฐ๊ฒฉ์ ์ ๋ ฌ</button>
<button class="btn btn-danger mt-3" id="filterPrice">6๋ง์ ์ดํ ๋ณด๊ธฐ</button>
<button class="btn btn-danger mt-3" id="revert">์๋๋๋ก</button>
</div>
</div>
- ๊ธฐ๋ฅ ์์ฑ ํ ํ๋์ฝ๋ฉํ ๋ถ๋ถ์ ์ง์ฐ๋ฉด ๊น๋ํ๊ฒ items ๋ผ๋ id ๋ฅผ ๊ฐ์ง ๋ด์ฉ ์๋ ํ๊ทธ ํ๋์ ๋ฒํผ๋ค๋ง ๋จ๋๋ค.
๐ ์ํ ์ ๋ ฌ : ๊ฐ๋๋ค์ ์ ๋ ฌ

- sort ํจ์๋ฅผ ํ์ฉํด์ ๊ฐ๋๋ค์ ์ ๋ ฌ ๊ธฐ๋ฅ์ ๋ฃ์ด์ฃผ์๋ค.
- ๋ฒํผ์ ๋๋ฅด๋ฉด ์ํ๋ช ๊ธฐ์ค์ผ๋ก ์ ๋ ฌ๋๋ ๊ฑฐ๋ผ ์ํ๋ช ์ ํ์ด๋ผ์ดํธ๋ฅผ ์ฃผ์๋ค. (๋ฒํผ๋ ๋ณํ๋ฅผ ์ค ๊ฑธ..)
$('#sortAbc').click(function(){
let productsAbc = products.slice();
productsAbc.sort(function(item1, item2){
if (item1.title < item2.title) return -1;
else if (item1.title == item2.title) return 0;
else return 1;
});
$('#items').html('');
layout(productsAbc);
$('.title').css('background','#FFFF9A');
})
- sortAbc ๋ผ๋ id ๋ฅผ ๊ฐ์ง ๋ฒํผ์ ํด๋ฆญํ์ ๋, products ๋ฐฐ์ด์ ๋ณต์ฌ๋ณธ์ธ productsAbc ์ ๊ฐ์ฒด๋ค ์ค ๋ ๊ฐ์ฉ item1, item2 ๋ผ๋ ํ๋ผ๋ฏธํฐ๋ก ๋ฐ์ ๊ทธ๊ฒ๋ค์ title ์ ๋น๊ตํด์ ์ค๋ฆ์ฐจ์์ผ๋ก ์ ๋ ฌํด์ฃผ์๋ค. (์ ํํ๊ฒ๋ ํด๋นํ๋ ์ ๋์ฝ๋๊ฐ์ ์์๋ฅผ ๋น๊ตํ๋ ๊ฒ์ด๋ค.)
- products ๋ฐฐ์ด์ ์ง์ ์ฌ์ฉํ์ง ์๊ณ slice ํจ์๋ฅผ ์ฌ์ฉํ์ฌ productsAbc ์ ์ฌ๋ณธ์ ๋ง๋ ์ด์ ๋ sort ํจ์๊ฐ ์๋ฐฐ์ด์ ์์ ํ์ฌ ๋ค๋ฅธ ๋ฒํผ์ ํด๋ฆญํ์ ๋ ์ํฅ์ ๋ฏธ์น๊ธฐ ๋๋ฌธ์ด๋ค. (์ด๊ฑธ ๋ค๋ฆ๊ฒ ์์์ ๋ฒํผ๋ค์ด ์๋ก ์ํฅ์ ์ฃผ์ง ์๋๋ก ๋ค ์ฌ๋ณธ์ ๋ง๋ค์๋ค.)
- ๊ทธ ๊ณผ์ ์์ ์์ฑ๋ if-else ๋ฌธ์ sort ๋ฅผ ์ฌ์ฉํ ๋ ์ ์์ ์ธ ๊ตฌ๋ฌธ์ธ๋ฐ, ์ผํญ์ฐ์ฐ์๋ฅผ ๋ฐฐ์ ๋ค๋ฉด ์ผํญ์ฐ์ฐ์๋ก ์ ์ผ๋ฉด ํจ์ฌ ๊ฐ๊ฒฐํ๊ธด ํ๋ค.
- ์ด์ ์ ๋ ฌ์ ํ์ผ๋ฉด layout ํจ์๋ฅผ ์ด์ฉํด์ ๋ ์ด์์์ ์์ฑํ์ฌ items ๋ผ๋ id ๋ฅผ ๊ฐ์ง ํ๊ทธ์ ๋ถ์ฌ์ฃผ๋ฉด ๋๋๋ฐ, ๊ทธ ์ ์ items ๋ผ๋ id ๋ฅผ ๊ฐ์ง ํ๊ทธ์ ๋ด์ฉ์ ๋ชจ๋ ์ง์์ฃผ์ด์ผ ํ๋ค.
- ์๋ํ๋ฉด, ์ด์ ์ ์ด๋ฏธ ์ด๊ธฐ ํ๋ฉด์ฉ์ผ๋ก ์ํ๋ค์ ๋ ์ด์์์ ์์ฑํด๋์๊ธฐ ๋๋ฌธ์ items ๋ผ๋ id ๋ฅผ ๊ฐ์ง ํ๊ทธ์ ๋ด์ฉ์ ๋ชจ๋ ์ง์์ฃผ์ง ์์ผ๋ฉด ๊ทธ ๋ ์ด์์๋ค ๋ค์์ผ๋ก ๊ฐ๋๋ค์์ผ๋ก ์ ๋ ฌ๋ ๋ ์ด์์๋ค์ด ์ถ๊ฐ๋ก ๋ถ๊ธฐ ๋๋ฌธ!
- ๊ทธ๋ฌ๊ณ ๋์ ๋ง๋ฌด๋ฆฌ๋ก ํ์ด๋ผ์ดํธ ๊ธฐ๋ฅ์ ๋ฃ์ด์ฃผ๋ฉด ๋๋ค. ํ์ดํ ๊ธฐ์ค์ผ๋ก ๊ฐ๋๋ค์์ด๋ฏ๋ก ์ํ๋ช ์ ๋ ธ๋์ ๊ณ์ด์ ํ์ด๋ผ์ดํธ๋ฅผ ๋ฃ์ด์ฃผ์๋ค.
๐ ์ํ ์ ๋ ฌ : ๊ฐ๊ฒฉ์ ์ ๋ ฌ

- ๊ฐ๊ฒฉ์ ์ ๋ ฌ ๊ธฐ๋ฅ ๋ํ ์ ๋ ฌ์ด๋ฏ๋ก sort ํจ์๋ฅผ ํ์ฉํด ๋ฃ์ด์ฃผ์๋ค.
- ๋ฒํผ์ ๋๋ฅด๋ฉด ๊ฐ๊ฒฉ์์ผ๋ก ์ ๋ ฌ์ด ๋๋ ๊ฑฐ๋ผ ๊ฐ๊ฒฉ์ ํ์ด๋ผ์ดํธ๋ฅผ ์ฃผ์๋ค.
$('#sortPrice').click(function(){
let productsPrice = products.slice();
productsPrice.sort(function(item1, item2){
return item1.price - item2.price;
});
$('#items').html('');
layout(productsPrice);
$('.price').css('background','#FFFF9A');
})
- ์ ์ฒด์ ์ธ JavaScript ๊ตฌ์กฐ๋ ๊ฐ๋๋ค์ ์ ๋ ฌ ๊ธฐ๋ฅ์ ๊ฐ์ง ๋ฒํผ์ ๋ง๋๋ ๊ฒ๊ณผ ์ ์ฌํ๋ค.
- ๋ค๋ง, ์ด๋ฒ์๋ ๊ฐ๊ฒฉ์ ๋น๊ตํ์ฌ ์ ๋ ฌํ๋ ๊ฒ์ด๋ฏ๋ก ๊ฐ๋๋ค์ ์ ๋ ฌ ๊ธฐ๋ฅ๊ณผ๋ ๋ฌ๋ฆฌ ๋บ์ ์ ๊ฒฐ๊ณผ๊ฐ์ ๋ฐ๋ผ ์ค๋ฆ์ฐจ์์ผ๋ก ์ ๋ ฌ์ด ๋๋ค.
- ์ฐธ๊ณ ๋ก ๋ด๋ฆผ์ฐจ์์ผ๋ก ์ ๋ ฌํ๊ณ ์ถ๋ค๋ฉด item2.price - item1.price ๋ก return ์ ํ๋ฉด ๋๋ค.
๐ ์ํ ํํฐ : 6๋ง์ ์ดํ ๋ณด๊ธฐ

- ๋ ์ด์์์ด Bootstrap ์ ์ด์ฉํ ๋ฐ์ํ์ด๋ค๋ณด๋ ํํฐ๋ง๋๋ ์ํ ๊ฐ์์ ๋ฐ๋ผ ํฌ๊ธฐ๊ฐ ์กฐ์ ๋๋ค.
- ๋ฒํผ์ ๋๋ฅด๋ฉด products ๋ฐฐ์ด ๋ด ๋ฐ์ดํฐ์ ๊ทธ๋๋ก ํํฐ๋ง๋๋ค.
- ๋ฒํผ์ ๋๋ฅด๋ฉด 6๋ง์ ์ดํ ์ํ๋ค๋ง ํํฐ๋ง๋๋ ๊ฒ์ด๋ฏ๋ก ๊ฐ๊ฒฉ์ ํ์ด๋ผ์ดํธ๋ฅผ ์ฃผ์๋ค.
$('#filterPrice').click(function(){
let productsFilter = products.filter(function(item){
return item.price <= 60000;
});
$('#items').html('');
layout(productsFilter);
$('.price').css('background','#FFFF9A');
})
- filter ํจ์๋ ํํฐ๊ฐ ์ ์ฉ๋ ์๋ก์ด ๋ฐฐ์ด์ return ๊ฐ์ผ๋ก ๋ฐํํ๊ธฐ ๋๋ฌธ์ ์๋ก์ด ๋ณ์์ ๋ด์ ์ฌ์ฉํด์ผ ํ๋ค.
- filter ํจ์ ๋ด์๋ ์ ์ฉํ ์กฐ๊ฑด์ ์ ์ด์ฃผ๋ฉด ๋๋ค.
๐ ์ํ ์ง์ด : ์๋๋๋ก

- ๊ฐ์ฌ๋์ด ์๋๋๋ก ๋๋๋ฆฌ๋ ๊ธฐ๋ฅ์ ๋ฃ์ด๋ณด๋ผ๊ณ ํ์ ๊ฒ ์ ์ ํ ์์๋ค.
- ์ด ์๋๋๋ก๋ผ๋ ๊ธฐ๋ฅ์ ๋ง๋ค๊ธฐ ์ ์๋ ๊ฐ ๋ฒํผ๋ค์ด ๋ค๋ฅธ ๋ฒํผ๋ค์ ์ํฅ์ ๋ฏธ์น๋ค๋ ์ฌ์ค์ ์์ง ๋ชปํ๋ค.
- ๊ทผ๋ฐ ๋ธ๋ก๊ทธ์ ๊ธ์ ์์ฑํ๊ธฐ ์ํด GIF ์บก์ฒ๋ฅผ ํ๋ ์ค, ๋ค๋ฅธ ๋ฒํผ๋ค์ ๋๋ฅด๊ณ ๋์ ์๋๋๋ก ๋ฒํผ์ ๋๋ฅด๋ฉด ์ฒซ ํ๋ฉด๊ณผ ๋์ผํ๊ฒ ์ํ๋ค์ด ๋ฐฐ์น๋์ด์ผ ํ๋๋ฐ ์ง์ ์ ๋๋ฅธ ๋ฒํผ์ ์ ๋ ฌ ์ํ์ ๋์ผํ๋ค๋ ๊ฒ์ ๋ฐ๊ฒฌํ๋ค.
- ์์ธ์ products ๋ฐฐ์ด ์์ฒด๋ฅผ ๊ทธ๋๋ก ์ด์ฉํด์์๋ค.
- ๊ทธ๋์ ์ด๋ป๊ฒ ์ฒ๋ฆฌ๋ฅผ ํด์ค๊นํ๋ค๊ฐ ๊ทธ๋ฅ ๋ค๋ฅธ ๋ฒํผ๋ค์ products ๋ฐฐ์ด์ ์ฌ์ฉํ์ง ์๊ณ ๊ฐ๊ฐ ์ฌ๋ณธ์ ๋ง๋ค์ด์ ์๋ก์๊ฒ ์ํฅ์ ๋ฏธ์น ์ ์๊ฒ ๋ง๋ค์ด๋ฒ๋ ธ๋ค.
$('#revert').click(function(){
$('#items').html('');
layout(products);
})
- ๊ทธ๋์ ์๋๋๋ก ๋ฒํผ์ ๋๋ฅด๋ฉด products ๋ฐฐ์ด ๊ทธ๋๋ก ์ํ ๋ ์ด์์์ด ์์ฑ๋ ์ ์์๋ค.
728x90
๋ฐ์ํ