Skip to content

Commit 1600332

Browse files
authored
Update MultiSelect.js
Added a new option to prevent the dropdown list from closing when selecting items. Items will now be appended to the list as opposed to being prepended.
1 parent c95d11d commit 1600332

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

MultiSelect.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class MultiSelect {
1313
search: true,
1414
selectAll: true,
1515
listAll: true,
16+
closeListOnItemSelect: false,
1617
name: '',
1718
width: '',
1819
height: '',
@@ -95,7 +96,12 @@ class MultiSelect {
9596
}
9697
option.classList.add('multi-select-selected');
9798
if (this.options.listAll === true || this.options.listAll === 'true') {
98-
headerElement.insertAdjacentHTML('afterbegin', `<span class="multi-select-header-option" data-value="${option.dataset.value}">${option.querySelector('.multi-select-option-text').innerHTML}</span>`);
99+
if (this.element.querySelector('.multi-select-header-option')) {
100+
let opt = Array.from(this.element.querySelectorAll('.multi-select-header-option')).pop();
101+
opt.insertAdjacentHTML('afterend', `<span class="multi-select-header-option" data-value="${option.dataset.value}">${option.querySelector('.multi-select-option-text').innerHTML}</span>`);
102+
} else {
103+
headerElement.insertAdjacentHTML('afterbegin', `<span class="multi-select-header-option" data-value="${option.dataset.value}">${option.querySelector('.multi-select-option-text').innerHTML}</span>`);
104+
}
99105
}
100106
this.element.querySelector('.multi-select').insertAdjacentHTML('afterbegin', `<input type="hidden" name="${this.name}[]" value="${option.dataset.value}">`);
101107
this.data.filter(data => data.value == option.dataset.value)[0].selected = true;
@@ -124,7 +130,9 @@ class MultiSelect {
124130
this.element.querySelector('.multi-select-search').value = '';
125131
}
126132
this.element.querySelectorAll('.multi-select-option').forEach(option => option.style.display = 'flex');
127-
headerElement.classList.remove('multi-select-header-active');
133+
if (this.options.closeListOnItemSelect === true || this.options.closeListOnItemSelect === 'true') {
134+
headerElement.classList.remove('multi-select-header-active');
135+
}
128136
this.options.onChange(option.dataset.value, option.querySelector('.multi-select-option-text').innerHTML, option);
129137
if (selected) {
130138
this.options.onSelect(option.dataset.value, option.querySelector('.multi-select-option-text').innerHTML, option);
@@ -249,4 +257,4 @@ class MultiSelect {
249257
}
250258

251259
}
252-
document.querySelectorAll('[data-multi-select]').forEach(select => new MultiSelect(select));
260+
document.querySelectorAll('[data-multi-select]').forEach(select => new MultiSelect(select));

0 commit comments

Comments
 (0)