Refactoring method #init and render.

This commit is contained in:
Макс Овсяников 2022-10-11 13:44:09 +03:00
parent 35c7bff4a5
commit c3bcea0997
2 changed files with 64 additions and 29 deletions

View File

@ -25,6 +25,27 @@ export class DropDown {
} }
addItem(item) { addItem(item) {
if (!item) {
return false;
}
const random = Math.random().toString(36).substring(2, 10);
const index = this.#items.length;
if (typeof item === 'object') {
item = {
id: item.id,
title: item.title,
value: item.value,
};
} else {
item = {
id: random,
title: item,
value: index,
};
}
this.#items.push(item); this.#items.push(item);
this.#render(); this.#render();
} }
@ -71,12 +92,38 @@ export class DropDown {
this.#list = this.#element.querySelector('.list'); this.#list = this.#element.querySelector('.list');
this.#caret = this.#element.querySelector('.caret'); this.#caret = this.#element.querySelector('.caret');
const { items, multiselect } = this.#options; const { items, multiselect, url } = this.#options;
this.#items = items; this.#items = [];
if (multiselect) { if (multiselect) {
this.#selectedItems = []; this.#selectedItems = [];
} }
if (!items && url) {
this.#renderUrl();
return;
}
items.forEach((dataItem, index) => {
const random = Math.random().toString(36).substring(2, 10);
let item = {};
if (this.#checkItemStruct(dataItem)) {
item = {
id: dataItem.id,
title: dataItem.title,
value: index,
};
} else {
item = {
id: random,
title: dataItem,
value: index,
};
}
this.#items.push(item);
});
} }
#initSelected(select) { #initSelected(select) {
@ -137,44 +184,30 @@ export class DropDown {
if (!items && url) { if (!items && url) {
this.#renderUrl(); this.#renderUrl();
return; return;
} }
items.forEach((item, index) => { this.#items.forEach((dataItem) => {
const li = document.createElement('li'); const li = document.createElement('li');
let text = '';
let id = '';
li.classList.add('list__item'); li.classList.add('list__item');
if (this.#checkItemStruct(item)) {
text = item.title;
id = item.id;
} else {
text = item;
}
if (multiselect) { if (multiselect) {
const checkBox = document.createElement('input'); const checkBox = document.createElement('input');
checkBox.type = 'checkbox'; checkBox.type = 'checkbox';
if (multiselectTag) { if (multiselectTag) {
if (this.#checkItemStruct(item)) { checkBox.setAttribute('id', `chbox-${dataItem.id}`);
checkBox.setAttribute('id', `chbox-${item.id}`);
} else {
checkBox.setAttribute('id', `chbox-${index}`);
}
} }
li.appendChild(checkBox); li.appendChild(checkBox);
} }
let textNode = document.createTextNode(text); let textNode = document.createTextNode(dataItem.title);
li.appendChild(textNode); li.appendChild(textNode);
ul.appendChild(li); ul.appendChild(li);
}); });
console.log(this.#items);
this.#addOptionsBehaviour(); this.#addOptionsBehaviour();
} }
@ -192,8 +225,7 @@ export class DropDown {
const response = await fetch(url); const response = await fetch(url);
const data = await response.json(); const data = await response.json();
console.log(response);
this.#items = [];
data.forEach((dataItem, index) => { data.forEach((dataItem, index) => {
const item = { const item = {

View File

@ -15,7 +15,8 @@ const dropdown = new DropDown({
dropdown.addItem('ZAZ'); dropdown.addItem('ZAZ');
dropdown.addItem('LADA'); dropdown.addItem('LADA');
// dropdown.addItem('BMW'); dropdown.addItem('Kamaz 258');
dropdown.addItem('BMW');
const dropdown2 = new DropDown({ const dropdown2 = new DropDown({
selector: '.cg-dropdown2', selector: '.cg-dropdown2',
@ -24,34 +25,36 @@ const dropdown2 = new DropDown({
{ {
id: 'addaw21', id: 'addaw21',
title: 'BMW', title: 'BMW',
value: '1', value: 1,
}, },
{ {
id: '2414q', id: '2414q',
title: 'Opel', title: 'Opel',
value: '2', value: 2,
}, },
{ {
id: '24qwds', id: '24qwds',
title: 'Kamaz 258', title: 'Kamaz 258',
value: '3', value: 3,
}, },
{ {
id: '28wds', id: '28wds',
title: 'MAN', title: 'MAN',
value: '4', value: 4,
}, },
{ {
id: '28qwds', id: '28qwds',
title: 'BOOT', title: 'BOOT',
value: '5', value: 5,
}, },
], ],
multiselect: true, multiselect: true,
multiselectTag: true, // multiselectTag: true,
}); });
dropdown2.addItem('LADA');
//ToDo: paste the desired url; //ToDo: paste the desired url;
const dropdown3 = new DropDown({ const dropdown3 = new DropDown({