๐ JS๊ฐ ๋ฐ์์ค๋ ๋ฐ์ดํฐ ์์ฝ
const menu = [
{
type: 'group',
name: '์๋ฃ',
children: [
{
type: 'group',
name: '์ฝ๋ ๋ธ๋ฃจ',
children: [
{ type: 'item', name: '๋์ดํธ๋ก ์ฝ๋ ๋ธ๋ฃจ' },
{ type: 'item', name: '๋์ฒด ์ฝ๋ ๋ธ๋ฃจ' },
{ type: 'item', name: '์ ์ฃผ ๋น์๋ฆผ ์ฝ๋ ๋ธ๋ฃจ' },
{ type: 'item', name: '์ฝ๋ ๋ธ๋ฃจ' },
],
},
{
type: 'group',
name: 'ํ๋ผํธ์น๋ ธ',
children: [
{ type: 'item', name: '์ ํ ์ฟ ํค ํฌ๋ฆผ ํ๋ผํธ์น๋ ธ' },
{ type: 'item', name: '๋๋ธ ์์คํ๋ ์ ์นฉ ํ๋ผํธ์น๋ ธ' },
{ type: 'item', name: '๋ชจ์นด ํ๋ผํธ์น๋ ธ' },
{ type: 'item', name: 'ํผ์คํ์น์ค ํฌ๋ฆผ ํ๋ผํธ์น๋ ธ' },
],
},
{
type: 'group',
name: '๋ธ๋ ๋๋',
children: [
{ type: 'item', name: '๋ง๊ณ ๋ฐ๋๋ ๋ธ๋ ๋๋' },
{ type: 'item', name: '๋ธ๊ธฐ ์๊ฑฐํธ ๋ธ๋ ๋๋' },
{ type: 'item', name: '์๋ชฝ ์ ๋ฒ ๋ธ๋ ๋๋' },
{ type: 'item', name: 'ํผ์น & ๋ ๋ชฌ ๋ธ๋ ๋๋' },
],
},
{
type: 'group',
name: 'ํฐ',
children: [
{ type: 'item', name: '๋ผ์ ํจ์ ํฐ' },
{ type: 'item', name: '๋ฏผํธ ๋ธ๋ ๋ ํฐ' },
{ type: 'item', name: '์์ด์ค ์ ์ค๋ฒ ๋ฆฌ ํฐ' },
{ type: 'item', name: '์์ด์ค ์บ๋ชจ๋ง์ผ ๋ธ๋ ๋ ํฐ' },
],
},
{
type: 'group',
name: '์ฃผ์ค',
children: [
{ type: 'item', name: 'ํ๋ฐฉ์ ์ญ ๊ฐ๋น' },
{ type: 'item', name: 'ํ์ดํ ์ฒญ๊ทค' },
{ type: 'item', name: '๋ธ๊ธฐ์ฃผ์ค' },
{ type: 'item', name: '๋์์ฃผ ํํ' },
],
},
],
},
{
type: 'group',
name: '์์',
children: [
{
type: 'group',
name: '๋นต',
children: [
{ type: 'item', name: 'ํธ๋ฌํ ๋ฏธ๋ ์ค์ฝ' },
{ type: 'item', name: '๋ณด๋ฌ๋ฐค ๋ชฝ๋ธ๋ ๋ฐ๋์ฌ' },
{ type: 'item', name: '๊ณ ์ํ ์น์ฆ ๋ฒ ์ด๊ธ' },
{ type: 'item', name: '๋ฏธ๋ ํด๋์ ์ค์ฝ' },
],
},
{
type: 'group',
name: '์ผ์ดํฌ',
children: [
{ type: 'item', name: '๋ฐ๋น ์๊ทธ ํ๋ฅดํธ' },
{ type: 'item', name: '๋ง์ค์นดํฌ๋ค ํฐ๋ผ๋ฏธ์ ์ผ์ดํฌ' },
{ type: 'item', name: '๋ธ๋ฃจ๋ฒ ๋ฆฌ ์ฟ ํค ์น์ฆ ์ผ์ดํฌ' },
{ type: 'item', name: '๋ถ๋๋ฌ์ด ์ํฌ๋ฆผ ์นด์คํ ๋ผ' },
],
},
{
type: 'group',
name: '์๋์์น',
children: [
{ type: 'item', name: '์ ํ ๊น๋ง๋ฒ ๋ฅด ์๋์์น' },
{ type: 'item', name: 'ํธ๋ฆฌํ ๋จธ์ฌ๋ฃธ ์น์ฆ ์๋์์น' },
{ type: 'item', name: '๋ก์คํธ ์นํจ ์๋ฌ๋ ๋ฐ ๋ฐ์ค' },
{ type: 'item', name: 'B.E.L.T ์๋์์น' },
],
},
{
type: 'group',
name: '๊ณผ์ผ',
children: [
{ type: 'item', name: 'ํ๋ฃจ ํ ์ปต RED' },
{ type: 'item', name: 'ํ๋ผ๋ด ๊ฐ๋ ํธ๋ ์ ค๋ฆฌ' },
],
},
{
type: 'group',
name: '์ค๋ต',
children: [
{ type: 'item', name: '๋ฆฌ์ ๋ธ ์ด์ฝ๋ฆฟ ์ธํธ' },
{ type: 'item', name: '๋ก์คํฐ๋ ์๋ชฌ๋ ์ค ์ด์ฝ๋ฆฟ' },
{ type: 'item', name: '๋ง์นด๋กฑ' },
{ type: 'item', name: '์์ผ๋ฆฌํจ ์บ๋ ํฌ๋ฆฌ์คํ ๋ฏผํธ' },
],
},
{
type: 'group',
name: '์์ด์คํฌ๋ฆผ',
children: [
{ type: 'item', name: '์๋ฐ ์นฉ ์ ๊ธฐ๋ ๋ฐ๋๋ผ ์์ด์คํฌ๋ฆผ' },
{ type: 'item', name: '๋์ธ ์ด์ฝ๋ฆฟ ์ํฌ๊ฐํ ' },
{ type: 'item', name: '๋ฐ๋๋ผ ์ํฌ๊ฐํ ' },
],
},
],
},
{
type: 'group',
name: '๊ตฟ์ฆ',
children: [
{
type: 'group',
name: '๋จธ๊ทธ',
children: [
{ type: 'item', name: '์ฐ๋ฆฌ ํ๊ธ ๋ธ๋ ๋จธ๊ทธ 473ml' },
{ type: 'item', name: '์์ธ ํฌ์ด ๋จธ๊ทธ 355ml' },
{ type: 'item', name: '์คํ๋ฒ ์ค 1ํธ์ ๋จธ๊ทธ 400ml' },
{ type: 'item', name: '์์ธ ์ ์ฃผ ๋ฐ์ด๋จธ๊ทธ ์ธํธ' },
],
},
{
type: 'group',
name: 'ํ ๋ธ๋ฌ',
children: [
{ type: 'item', name: 'SS ๋ถ์ฐ ํฌ์ด ํ ๋ธ๋ฌ 355ml' },
{ type: 'item', name: 'SS ๋ธ๋ ํค๋ฆฌํฐ์ง ์ค๋๋ฆฌ ํ ๋ธ๋ฌ 355ml' },
{ type: 'item', name: 'SS ์์น๋ ์ค๋ฒ ํ ๋ธ๋ฌ 473ml' },
],
},
{
type: 'group',
name: '์ ์ธ์ฌ๋ฆฌ',
children: [
{ type: 'item', name: '๋ฆฌ์ ๋ธ ์ค๋ ์ง ์นด๋ ํ๋' },
{ type: 'item', name: '์คํ๋ฒ ์ค 1ํธ์ ์์ฝ๋ฐฑ' },
{ type: 'item', name: '์คํ๋ฒ ์ค 1ํธ์ ๋ฉํ ํ์ฐ์น' },
],
},
],
},
{
type: 'group',
name: '์นด๋',
children: [
{ type: 'item', name: '10000์๊ถ' },
{ type: 'item', name: '30000์๊ถ' },
{ type: 'item', name: '50000์๊ถ' },
{ type: 'item', name: '100000์๊ถ' },
],
},
];
๐ JS ์ฝ๋
const root = document.getElementById('root');
function createTreeView(menu, currentNode) {
menu.map((el) => {
let li = document.createElement('li');
if(el.children !== undefined){
let input = document.createElement('input');
let ul = document.createElement('ul');
let span = document.createElement('span');
input.setAttribute('type', 'checkbox');
span.textContent = el.name;
li.append(input);
li.append(span);
currentNode.append(li);
li.append(ul);
createTreeView(el.children, ul);
}else{
currentNode.append(li);
li.textContent = el.name;
}
});
}
createTreeView(menu, root);
๐ JS ์ฝ๋ ๋ฏ์ด๋ณด๊ธฐ
const root = document.getElementById('root');
function createTreeView(menu, currentNode) {
// ๊ฐ์ฒด๋ค์ ๋ด์ menu ๋ฐฐ์ด์ ๋๋ฉด์ ์์(๊ฐ์ฒด) ๊ฒ์ฌ
menu.map((el) => {
// ๊ฐ์ฒด ๊ฐ์๋งํผ liํํฌ ์์ฑ
let li = document.createElement('li'); // <li></li>
// ๊ฐ์ฒด ์์ childrenํค๊ฐ์ด ์์ผ๋ฉด
if(el.children !== undefined){
let input = document.createElement('input'); // <input>
let ul = document.createElement('ul'); // <ul></ul>
let span = document.createElement('span'); // <span></span>
input.setAttribute('type', 'checkbox'); // <input type="checkbox">
span.textContent = el.name; // <span>${el.name}</span>
li.append(input); // <li>
// <input type="checkbox">
// </li>
li.append(span); // <li>
// <input type="checkbox">
// <span>${el.name}</span>
// </li>
currentNode.append(li); // <div id="root>
// <li>
// <input type="checkbox">
// <span>${el.name}</span>
// </li>
// </div>
// liํ๊ทธ์ ulํ๊ทธ ์ถ๊ฐ
li.append(ul); // <div id="root>
// <li>
// <input type="checkbox">
// <span>${el.name}</span>
// <ul></ul>
// </li>
// </div>
// ์ฌ๊ทํจ์ ํธ์ถ
createTreeView(el.children, ul); // <div id="root>
// <li>
// <input type="checkbox">
// <span>${el.name}</span>
// <ul>
// <li>
// <input type="checkbox">
// <span>${el.children.name}</span>
// <ul></ul>
// </li>
// </ul>
// </li>
// </div>
// ๊ฐ์ฒด ์์ childrenํค๊ฐ์ด ์์ผ๋ฉด
}else{
currentNode.append(li); // <div id="root>
// <li>
// <input type="checkbox">
// <span>${el.name}</span>
// <ul>
// <li>
// <input type="checkbox">
// <span>${el.children.name}</span>
// <ul>
// <li></li>
// </ul>
// </li>
// </ul>
// </li>
// </div>
li.textContent = el.name; // <div id="root>
// <li>
// <input type="checkbox">
// <span>${el.name}</span>
// <ul>
// <li>
// <input type="checkbox">
// <span>${el.children.name}</span>
// <ul>
// <li>
// ${el.name}
// </li>
// </ul>
// </li>
// </ul>
// </li>
// </div>
}
});
}
createTreeView(menu, root);
๐ ์์ฑ๋ HTML ๊ตฌ์กฐ
๐ ๋ ๋๋ง ๊ฒฐ๊ณผ
๐ ์ฌ๊ทํจ์๋ฅผ ์ด์ฉํด DOM ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ฉด์ ์๊ฒ๋ ์
๐ ํ๊ทธ๋ฅผ ํ๋ฒ๋ง ์์ฑํด๋๋๋ ํธ๋ฆฌํจ์ด ์์๋ค.
๐ ์์ฑ๋ ํ๊ทธ๋ฅผ ๋ด์ ๋ณ์๋ ํ ๊ณณ์ ๋ชจ์๋๋ ๊ฒ ๋ณด๊ธฐ ํธํ๋๋ผ.
๐ ํจ์๋ฅผ ์ด๋์๋ ํธ์ถํ ์ ์๋ค๋ ๊ฑธ ์ผ๋์ ๋๊ณ ๋ก์ง์ ๊ผผ๊ผผํ ๊ตฌํํด ํจ์๋ฅผ ์์ฑํด์ผ ํ๋ค.
๐ ์์ฑํ ํ๊ทธ๋ ๊ผญ !! ์ํ๋ํ๊ทธ์ appendํด์ค์ผ ํด๋น ํ๊ทธ์ ์์์์ฑ์ผ๋ก ์ถ๊ฐ๊ฐ ๋๋ค. ( ์์ง๋ง์๋ ์๋ฏธ์์ )
โ ํ๊ทธ๋ฅผ ์์ฑํ๊ธฐ ( document.createElement() )
โ ํ๊ทธ์ ์์ฑ๊ฐ ์ถ๊ฐํ๊ธฐ ( .setAttribute() )
โ ํ๊ทธ์ ๋ด์ฉ ์ถ๊ฐํ๊ธฐ ( .textContent=? )
โ ์์ฑ๋ ํ๊ทธ๋ฅผ DOM์ ์ถ๊ฐํ๊ธฐ ( .append() )
'Front-end > JS' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
์๋ฐ์คํฌ๋ฆฝํธ ๋น๋๊ธฐ ํ๋ก๊ทธ๋๋ฐ Asynchronous Programming (0) | 2021.09.06 |
---|---|
์๋ฐ์คํฌ๋ฆฝํธ ์คํ ์ปจํ ์คํธ Execution Context ๋ฏ์ด ๋ณด๊ธฐ (0) | 2021.09.05 |
์๋ฐ์คํฌ๋ฆฝํธ ํด๋์ค Class ์ ๋ฌธ (0) | 2021.08.29 |
์๋ฐ์คํฌ๋ฆฝํธ ํด๋ก์ ํ์ฉ Closure private ๋ณ์, ๋ฉ์๋ ํ๋ด ๋ด๊ธฐ (0) | 2021.08.28 |
์๋ฐ์คํฌ๋ฆฝํธ ํด๋ก์ Closure (0) | 2021.08.27 |
๋๊ธ