вот некоторые другие варианты, которые вы можете рассмотреть:
с использованием OptGroup:
const selObject = { "-- Select Item --": { Clothes: { "-- Select brands --": { Levis: { "-- Select product --": { Jeans: { "-- Select size --": ["38", "39", "40"] }, Jackets: { "-- Select size --": ["41", "42", "43"] } } }, Gucci: { "-- Select product --": { Shoes: { "-- Select size --": ["45", "50", "55"] }, Dresses: { "-- Select size --": ["8", "9", "10"] } } } } }, Gadgets: { "-- Select brands --": { LG: { "-- Select product --": { TVs: { "-- Select size --": ["38", "39", "40"] }, Phones: { "-- Select size --": ["8", "9", "10"] } } }, Apple: { "-- Select product --": { Macbooks: { "-- Select size --": ["15", "17", "21"] }, iPhones: { "-- Select size --": ["8", "9", "10"] } } } } } } };
const generateDropDown = (obj, indent) => {
const spaces = Array(indent).fill(' ').join('');
if (Array.isArray(Object.values(obj)[0])) {
return Object.values(obj)[0].map(e => "<option>" + spaces + e + "</option>").join('');
} else {
return Object.values(obj).map(brand => {
return Object.keys(brand).map(product => {
//?
return `<optgroup label="${spaces + product}"> ${generateDropDown(brand[product], indent + 4)} </optgroup>`;
}).join('');
});
}
};
const list = generateDropDown(selObject, 0).join(' ');
document.querySelector('#dropDown').innerHTML = list;
<select id="dropDown">
</select>
с использованием ul: (более гибкий для стилизации)
const selObject = { "-- Select Item --": { Clothes: { "-- Select brands --": { Levis: { "-- Select product --": { Jeans: { "-- Select size --": ["38", "39", "40"] }, Jackets: { "-- Select size --": ["41", "42", "43"] } } }, Gucci: { "-- Select product --": { Shoes: { "-- Select size --": ["45", "50", "55"] }, Dresses: { "-- Select size --": ["8", "9", "10"] } } } } }, Gadgets: { "-- Select brands --": { LG: { "-- Select product --": { TVs: { "-- Select size --": ["38", "39", "40"] }, Phones: { "-- Select size --": ["8", "9", "10"] } } }, Apple: { "-- Select product --": { Macbooks: { "-- Select size --": ["15", "17", "21"] }, iPhones: { "-- Select size --": ["8", "9", "10"] } } } } } } };
const generateDropDown = (obj, indent) => {
const values = Object.values(obj);
if (Array.isArray(values[0])) {
return values[0].map(e => `<li class="child">${e} </li>`).join(' ');
} else {
return values.map(brand => {
return Object.keys(brand).map(product => {
return `<ul class="parent"> <li class="title">${product}</li> ${generateDropDown(brand[product], indent + 2)} </ul>`;
}).join(' ');
});
}
};
const list = generateDropDown(selObject, 0).join(' ');
document.querySelector('#dropDown').innerHTML = list;
[...document.querySelectorAll('ul,li')].forEach(e => {
e.addEventListener('click', ev => {
ev.cancelBubble = true;
ev.target.classList.toggle('open');
// console.log(ev.target.innerText)
// do some stuff when the element is clicked.
})
})
ul{
padding: 0;
margin: 0;
list-style-type: none;
}
.parent, .child{
padding-left: 15px;
display: none;
cursor: pointer;
}
#dropDown > .parent {
padding-left: 0;
display: block;
}
.open ~ .parent{
opacity: 1;
display: block;
}
.open ~ .child{
opacity: 1;
display: block;
}
.title{
font-weight: bold;
}
.title.open{
color: red;
}
<div id="dropDown">
</div>