230420 - localStorage

๋ฐฑ์Šน์—ฐยท2023๋…„ 4์›” 20์ผ
1

๐Ÿšฉ javascript

localStorage

๐Ÿ“ ๋‚ด์šฉ

  • ์˜ˆ์ „์— js๋ฅผ ์ด์šฉํ•ด์„œ ๋งŒ๋“ค์—ˆ๋˜ ์‡ผํ•‘ ๋ฆฌ์ŠคํŠธ ์•ฑ์„ localStorage๋ฅผ ์ด์šฉํ•ด ๋‚ด์šฉ์ด ๋‚ ์•„๊ฐ€์ง€ ์•Š๊ณ  ์œ ์ง€๋˜๋„๋ก ์ฝ”๋“œ ๋ณ€๊ฒฝ


โœ’๏ธ ์‚ฌ์šฉ๋ฒ•

์ž…๋ ฅ

css

* { box-sizing: border-box; }
body { font-family: Verdana, Geneva, Tahoma, sans-serif; background: cornsilk; margin: 50px; }
button { outline: 0; border: 0; background: transparent; cursor: pointer; }
section { min-width: 360px; max-width: 600px; margin: auto; background: #f1f7ff; border-radius: 15px; box-shadow: 5px 5px 10px rgba(0, 0, 0, 15%); }
header { height: 48px; line-height: 48px; background: rgb(180,205,255); background: linear-gradient(45deg, rgba(180,205,255,1) 0%, rgba(233,179,255,1) 100%); border-radius: 15px 15px 0 0; text-align: center; color: #fff; }
ul.items { height: 500px; overflow-y: auto; padding-bottom: 30px; }
.item_row {
}
.item { display: flex; justify-content: space-between; align-items: center; height: auto; padding: 10px 32px; }
.item_name {
}
.itemDelete_btn { font-size: 18px; transition: .2s ease-in; }
.itemDelete_btn:hover { color: red; transform: scale(1.1); }
.item_divider { width: 90%; height: 1px; margin: auto; background-color: #d7c8ff; }
footer { background: rgb(180,205,255); background: linear-gradient(45deg, rgba(180,205,255,1) 0%, rgba(233,179,255,1) 100%); border-radius: 0 0 15px 15px; text-align: center; }
.f_input { width: 100%; height: 40px; padding: 0 32px; border: 0; outline: none; font-size: 18px; }
.f_input::placeholder { color: #ddd; }
.f_addBtn { width: 48px; height: 48px; font-size: 25px; color: rgba(255, 255, 255, .5); transition: .3s ease-in; }
.f_addBtn:hover { color: rgba(255, 255, 255, 1); transform: scale(1.1); }

html

<section>
  <header>ShoppingList</header>
  <ul class="items">
    <li class="item_row">
      <div class="item">
        <span class="item_name">์•„์ด์Šคํฌ๋ฆผ</span>
        <button class="itemDelete_btn">
          <i class="fa-solid fa-trash-can"></i>
        </button>
      </div>
      <div class="item_divider"></div>
    </li>
  </ul>

  <footer>
    <input type="text" class="f_input" placeholder="enter your shopping list">
    <button class="f_addBtn">
      <i class="fa-solid fa-circle-plus"></i>
    </button>
  </footer>
</section>

js

const items = document.querySelector('.items')   //ul
const input = document.querySelector('.f_input')  
const addBtn = document.querySelector('.f_addBtn')  
let id = 0;
let products = []  //์ž…๋ ฅํ•  ๋‚ด์šฉ๋“ค ๋„ฃ์„ ๋ฐฐ์—ด


//localStarage์— ์ž…๋ ฅํ•œ ๋‚ด์šฉ ์ €์žฅ
const save = () => {
  localStorage.setItem('products', JSON.stringify(products));
}


//ํด๋ฆญํ•˜๋ฉด(input์— ์ž…๋ ฅํ›„ ์—”ํ„ฐ์น˜๋ฉด) ๋ฐœ์ƒํ•  ํ•จ์ˆ˜ ์ •์˜
function onAdd(){  
  const product = {
    id:id, 
    text:input.value
  } 
  products.push(product);  //๋ฐฐ์—ด products์•ˆ์— ์˜ค๋ธŒ์ ํŠธ product(์ž…๋ ฅํ•œ ๋‚ด์šฉ๊ณผ id)๋ฅผ์„ ์ง‘์–ด ๋„ฃ๋Š”๋‹ค
  save();  //saveํ•จ์ˆ˜ ์‹คํ–‰ 
  
  if(product.text == ''){
    input.focus();
    return;    
  } 

  //createItem์‹คํ–‰ (ul.items)
  createItem(product);  

  //input ์ดˆ๊ธฐํ™”
  input.value='';
  input.focus();
}

//ul.items๋ฅผ ๋งŒ๋“ค์–ด์ฃผ๋Š” ํ•จ์ˆ˜
function createItem(product){
  const itemRow = document.createElement('li');
  itemRow.setAttribute('class','item_row');
  itemRow.setAttribute('data-id',product.id);

  //์ŠคํŠธ๋ง ๋ฆฌํ„ฐ๋Ÿด ๋ฐฉ์‹์œผ๋กœ ์ถ”๊ฐ€
  itemRow.innerHTML = `  
    <div class="item">
      <span class="item_name">${product.text}</span>
      <button class="itemDelete_btn">
        <i class="fa-solid fa-trash-can" data-id=${product.id}></i>
      </button>
    </div>
    <div class="item_divider"></div>`;
  id++;

  //ul.items์— ๋งŒ๋“  ์•„์ดํ…œ ์ถ”๊ฐ€
  items.appendChild(itemRow)
  //์ƒˆ๋กœ ์ถ”๊ฐ€๋œ ์•„์ดํ…œ์ด ํ™”๋ฉด์— ๋ณด์ด๊ฒŒ(์ž๋™์œผ๋กœ ์Šคํฌ๋กค)
  itemRow.scrollIntoView();
  return  items;  
  //์ตœ์ข…์ ์œผ๋กœ ๋งŒ๋“ค์–ด์ค€  items(ul)๋ฅผ ๋ฆฌํ„ดํ•ด์คŒ  
}

//์ดˆ๊ธฐํ™” ํ•ด์ฃผ๋Š” ํ•จ์ˆ˜(์Šคํ† ๋ฆฌ์ง€์— ์ด๋ฏธ ์ €์žฅ๋œ ๊ฒƒ์ด ์žˆ์„ ๊ฒฝ์šฐ)
function init() {
  const userProducts = JSON.parse(localStorage.getItem('products'))
  //console.log(userProducts)

  if(userProducts) {
    userProducts.forEach(aa => createItem(aa))
    products = userProducts;
  }
}
init();

addBtn.addEventListener('click', onAdd);
input.addEventListener('keypress', event =>{
  event.key === "Enter" && onAdd()
});

input.addEventListener('focus',()=>{
  input.removeAttribute('placeholder')
})

//์‚ญ์ œ
items.addEventListener('click',(e)=>{  
  const idc = e.target.dataset.id;  //์“ฐ๋ ˆ๊ธฐํ†ต์„ ๋ˆŒ๋ €์„๋•Œ๋งŒ id๊ฐ’ ๋ฐ›์•„์˜ด
  if(idc) {
    const toBeDeleted = document.querySelector(`.item_row[data-id="${idc}"]`)
    toBeDeleted.remove()

    // localStorage๋„ ์‚ญ์ œ
    products = products.filter((aa) => aa.id !== parseInt(idc)); // html์—์„œ ๊ฐ€์ ธ์˜จ idc๋Š” ๋ฌธ์ž์—ด์ž„.
    save();
  }
})

์ถœ๋ ฅ

  • ์ด๋ฏธ์ง€๋กœ ๋Œ€์ฒด


๐Ÿ”— ์ฐธ๊ณ  ๋งํฌ & ๋„์›€์ด ๋˜๋Š” ๋งํฌ






profile
๊ณต๋ถ€ํ•˜๋Š” ๋ฒจ๋กœ๊ทธ

0๊ฐœ์˜ ๋Œ“๊ธ€