์ด๋ฒ ๋ฒจ๋ก๊ทธ์์๋ javascript๋ก ๋๋จธ์ง movie select๋ฅผ ์ฑ์๋๊ฐ๋ค.
container, seats, count, total, movieSelect ์ฌ์ฉ
const container = document.querySelector('.container');
const seats = document.querySelectorAll('.row .seat:not(.occupied)');
const count = document.getElementById('count');
const total = document.getElementById('total');
const movieSelect = document.getElementById('movie');
์ฐ์ ์ด๋ฒคํธ๋ฅผ ๋จผ์ ๊ตฌ์ฑํด๋ณด์.
์ ํํ ์ํ์ ์ ๋ณด๋ก ์ค์ ์ ์์ผ์ผ ํ๋ค.
movieSelect.addEventListener('change', e=>{
ticketPrice = +e.target.value;
setMoiveData(e.target.selectedIndex, e.target.value);
updateSelectedCount();
});
๊ฐ๊ฐ์ ์ํ๋ค(movie)์ ์ด๋ฒคํธ๋ฅผ ๋ฃ์ด์ฃผ๊ธฐ์๋ ์ฝ๋๊ฐ ๋๋ฌด ๋ณต์กํด์ง๊ธฐ์, ์ด๋ฒคํธ ์์์ ์ฌ์ฉํด movieSelect ์ ์ด๋ฒคํธ๋ฅผ ๊ฑธ์ด์ค๋ค.
์ด๋ฒคํธ ์์: ์ฌ๋ฌ ๊ฐ์ ํ์ DOM ์์์ ๊ฐ๊ฐ ์ด๋ฒคํธ ํธ๋ค๋ฌ๋ฅผ ๋ฑ๋กํ๋ ๋์ ํ๋์ ์์ DOM ์์์ ์ด๋ฒคํธ ํธ๋ค๋ฌ๋ฅผ ๋ฑ๋กํ๋ ๋ฐฉ๋ฒ
ticketPrice๋ ์ถํ let์ผ๋ก ์์ฑํ ํฐ์ผ ๊ฐ๊ฒฉ ๋ณ์์ด๋ฉฐ,
setMovieData์ updateSelectedCount๋ ์กฐ๊ธ ์๋ค ๋ค์ ์ดํด๋ณด๊ธฐ๋ก ํ์.
container.addEventListener('click', e=>{
if(e.target.classList.contains('seat') &&
!e.target.classList.contains('occupied')){
e.target.classList.toggle('selected');
}
updateSelectedCount();
});
seat๋ฅผ ์์์ผ๋ก ๊ฐ์ง๊ณ ์๋ container๋ฅผ ์ด๋ฒคํธ ์์์ผ๋ก์ ์ฌ์ฉํ๋ค.
์ฌ๊ธฐ์ ์ฃผ์ํด์ผ ํ ์ ์, ์ด๋ฒคํธ ์์์ ์ฌ์ฉํ๋ฉด seat ๋ฟ๋ง์ด ์๋ container๋ฅผ ํด๋ฆญํด๋ ์ด๋ฒคํธ๊ฐ ์์ฑ๋๊ธฐ ๋๋ฌธ์ ์กฐ๊ฑด๋ฌธ์ ์ฌ์ฉํด์ ๋ฒ์๋ฅผ seat๋ก ํ์ ํด์ค์ผ ํ๋ค๋ ๊ฒ์ด๋ค.
์ฐ์ localStorage๋,๋ธ๋ผ์ฐ์ ์ธ์ ๊ฐ์ ๊ณต์ ๋๋ ์ ์ฅ๋ ๋ฐ์ดํฐ์ด๋ค.
setItem์ ํตํด ์ ์ฅํ๊ณ , getItem์ ํตํด ๋ฐ์ดํฐ๋ฅผ ๋ถ๋ฌ์จ๋ค.
์์์ ์ค๋ช
ํ๋ ์ํ ์ ํ ์ด๋ฒคํธ๋ฅผ ์์๋ก ์ค๋ช
ํ์๋ฉด,
setMoiveData(e.target.selectedIndex, e.target.value);
ํด๋ฆญํ ์์์ ์ธ๋ฑ์ค์ value ๊ฐ์ ํจ์์ ๋งค๊ฐ๋ณ์๋ก ๋ฐ์ localStorage์ ์ ์ฅํด์ค๋ค.
function setMovieData(movieIndex, moviePrice){
localStorage.setItem('selectedMovieIndex', movieIndex);
localStorage.setItem('selectedMoivePrice', moviePrice);
}
const selectedSeats = document.querySelectorAll('.row .seat.selected');
์ข์ ํด๋ฆญ ์ด๋ฒคํธ๋ก selected๋ seat๋ฅผ selectedSeats๋ก ์ ์ฅ์ํจ๋ค.
const seatsIndex = [...selectedSeats].map(seat => [...seats].indexOf(seat));
[...selectedSeats]
๋ก[...seats].indexOf(seat)
์ 1๋ 1๋ก ์ง์ง์ด์ค๋ค.localStorage.setItem('selectedSeats', JSON.stringify(seatsIndex));
JSON.stringify() ๋ฉ์๋๋ JavaScript ๊ฐ์ด๋ ๊ฐ์ฒด๋ฅผ JSON ๋ฌธ์์ด๋ก ๋ณํํฉ๋๋ค
function updateSelectedCount() {
const selectedSeats = document.querySelectorAll('.row .seat.selected');
const seatsIndex = [...selectedSeats].map(seat => [...seats].indexOf(seat));
localStorage.setItem('selectedSeats', JSON.stringify(seatsIndex));
const selectedSeatsCount = selectedSeats.length;
count.innerText = selectedSeatsCount;
total.innerText = selectedSeatsCount * ticketPrice;
setMovieData(movieSelect.selectedIndex, movieSelect.value);
}
JSON.parse() ๋ฉ์๋๋ JSON ๋ฌธ์์ด์ ๊ตฌ๋ฌธ์ ๋ถ์ํ๊ณ , ๊ทธ ๊ฒฐ๊ณผ์์ JavaScript ๊ฐ์ด๋ ๊ฐ์ฒด๋ฅผ ์์ฑํฉ๋๋ค.
localStorage์์ 'selectedSeats'๋ฅผ ํค๋ก ๊ฐ์ง๊ณ ์๋ ๊ฐ์ ๊ฐ์ ธ์ ๋ณ์ selectedSeats์ ์ ์ฅํ๋ค.
์กฐ๊ฑด๋ฌธ์ 'selectedSeats'๊ฐ ์กด์ฌํ๋ ๊ฒฝ์ฐ์๋ง ์คํ๋๋ค.
seats.forEach((seat, index) =>{
if(selectedSeats.indexOf(index) > -1){
seat.classList.add('selected');
}
});
๋ถ๋ถ์์๋ seats ๋ฐฐ์ด์ ๋ชจ๋ ์์๋ฅผ ์ํํ๋ฉฐ ์ฝ๋ฐฑ ํจ์๋ฅผ ๋ฐ๋ณต ํธ์ถํ๋ค.
ํธ์ถ๋๋ ์ฝ๋ฐฑ ํจ์๋ localStorage์์ ๊ฐ์ ธ์จ selectedSeats์ ์ธ๋ฑ์ค๊ฐ 0 ์ด์์ด๋ฉด ํด๋นํ๋ ์์์ 'selected' ํด๋์ค๋ฅผ ๋ถ์ฌํ๋ค.
function populateUI(){
const selectedSeats = JSON.parse(localStorage.getItem('selectedSeats'));
if(selectedSeats !== null && selectedSeats.length > 0){
seats.forEach((seat, index) =>{
if(selectedSeats.indexOf(index) > -1){
seat.classList.add('selected');
}
});
}
const selectedMovieIndex = localStorage.getItem('selectedMovieIndex');
if(selectedMovieIndex !=null){
movieSelect.selectedIndex = selectedMovieIndex;
}
}
const container = document.querySelector('.container');
const seats = document.querySelectorAll('.row .seat:not(.occupied)');
const count = document.getElementById('count');
const total = document.getElementById('total');
const movieSelect = document.getElementById('movie');
populateUI();
let ticketPrice = +movieSelect.value;
// localStorage์ ์ํ์ index์ ๊ฐ๊ฒฉ ์ค์
function setMovieData(movieIndex, moviePrice){
localStorage.setItem('selectedMovieIndex', movieIndex);
localStorage.setItem('selectedMoivePrice', moviePrice);
}
// total๊ณผ count ์
๋ฐ์ดํธ
function updateSelectedCount() {
const selectedSeats = document.querySelectorAll('.row .seat.selected');
const seatsIndex = [...selectedSeats].map(seat => [...seats].indexOf(seat));
localStorage.setItem('selectedSeats', JSON.stringify(seatsIndex));
const selectedSeatsCount = selectedSeats.length;
count.innerText = selectedSeatsCount;
total.innerText = selectedSeatsCount * ticketPrice;
setMovieData(movieSelect.selectedIndex, movieSelect.value);
}
// localstorage์์ ๋ฐ์ดํฐ ๊ฐ์ ธ์ค๊ธฐ, UI ์ฑ์ฐ๊ธฐ
function populateUI(){
const selectedSeats = JSON.parse(localStorage.getItem('selectedSeats'));
if(selectedSeats !== null && selectedSeats.length > 0){
seats.forEach((seat, index) =>{
if(selectedSeats.indexOf(index) > -1){
seat.classList.add('selected');
}
});
}
const selectedMovieIndex = localStorage.getItem('selectedMovieIndex');
if(selectedMovieIndex !=null){
movieSelect.selectedIndex = selectedMovieIndex;
}
}
// ์ข์ ํด๋ฆญ ์ด๋ฒคํธ
container.addEventListener('click', e=>{
if(e.target.classList.contains('seat') &&
!e.target.classList.contains('occupied')){
e.target.classList.toggle('selected');
}
updateSelectedCount();
});
// ์ํ ์ ํ ์ด๋ฒคํธ
movieSelect.addEventListener('change', e=>{
ticketPrice = +e.target.value;
setMoiveData(e.target.selectedIndex, e.target.value);
updateSelectedCount();
});
updateSelectedCount();