From f106116b8be98a346b6c941f968c5f1e5bf362d9 Mon Sep 17 00:00:00 2001 From: MaxOvs Date: Mon, 26 Sep 2022 17:00:15 +0300 Subject: [PATCH] Methods cg-select complted --- src/cg-dropdown.js | 23 +++++++++++++++++------ src/index.html | 2 +- src/index.js | 28 ++++++++++++++++++++++++++-- 3 files changed, 44 insertions(+), 9 deletions(-) diff --git a/src/cg-dropdown.js b/src/cg-dropdown.js index 51d5472..c433af0 100644 --- a/src/cg-dropdown.js +++ b/src/cg-dropdown.js @@ -4,8 +4,6 @@ export class DropDown { #options; #caret; - //ToDo: Added url - constructor(options = {}) { this.#init(options); this.#initAmount(); @@ -49,7 +47,6 @@ export class DropDown { this.#createSelected(this.#options.placeholder); this.#customStyles(styles); } else { - this.#createSelected('Select...'); this.#customStyles(styles); } @@ -106,10 +103,18 @@ export class DropDown { items.map((item) => { let li = document.createElement('li'); - const text = document.createTextNode(item); - li.classList.add('list__item'); - li.appendChild(text); + if (typeof item == 'object') { + const text = document.createTextNode(item.value); + li.classList.add('list__item'); + li.appendChild(text); + } else { + const text = document.createTextNode(item); + + li.classList.add('list__item'); + li.appendChild(text); + } + ul.appendChild(li); }); } @@ -251,4 +256,10 @@ export class DropDown { this.#render(select); } + + getElement(number) { + const { items } = this.#options; + + return items[number]; + } } diff --git a/src/index.html b/src/index.html index 366bebf..2856148 100644 --- a/src/index.html +++ b/src/index.html @@ -13,7 +13,7 @@
- +
diff --git a/src/index.js b/src/index.js index 04f7867..dea89ed 100644 --- a/src/index.js +++ b/src/index.js @@ -10,7 +10,7 @@ dropdown.addItem('ZAZ'); const dropdown2 = new DropDown({ selector: '.cg-dropdown2', - selected: '...', + placeholder: 'Выберите авто', items: ['BMW', 'Opel', 'Mersedes', 'MAN', 'Kamaz'], event: 'mouseenter', styles: { @@ -34,7 +34,31 @@ const dropdown2 = new DropDown({ }); dropdown2.addItem('LADA'); -dropdown2.selectIndex(3); +// dropdown2.selectIndex(3); + +const dropdown3 = new DropDown({ + selector: '.cg-dropdown3', + items: [ + { + id: '186', + value: 'A008', + }, + { + id: '288', + value: 'BMW', + }, + { + id: '355', + value: 'MAN', + }, + ], +}); + +let a = dropdown3.getElement(0); +console.log(a); + +let b = dropdown.getElement(0); +console.log(b); // dropdown2.deleteItemAll();