|
80 | 80 | } else {
|
81 | 81 | // Single-Select
|
82 | 82 | this._deselectAll();
|
83 |
| - value.el.setAttribute('selected', 'selected'); |
| 83 | + this._selectValue(value); |
84 | 84 | }
|
| 85 | + // Refresh Input-Text |
| 86 | + this._setValueToInput(); |
| 87 | + // Trigger Change-Event only when data is different |
85 | 88 | const actualSelectedValues = this.getSelectedValues();
|
86 | 89 | const selectionHasChanged = !this._arraysEqual(
|
87 | 90 | previousSelectedValues,
|
|
166 | 169 | // Initialize dropdown
|
167 | 170 | if (!this.el.disabled) {
|
168 | 171 | let dropdownOptions = $.extend({}, this.options.dropdownOptions);
|
| 172 | + dropdownOptions.coverTrigger = false; |
169 | 173 | let userOnOpenEnd = dropdownOptions.onOpenEnd;
|
170 | 174 | // Add callback for centering selected option when dropdown content is scrollable
|
171 | 175 | dropdownOptions.onOpenEnd = (el) => {
|
|
233 | 237 | $(this.dropdownOptions).append(li);
|
234 | 238 | return li;
|
235 | 239 | }
|
| 240 | + |
| 241 | + _selectValue(value) { |
| 242 | + value.el.selected = true; |
| 243 | + value.optionEl.classList.add('selected'); |
| 244 | + const checkbox = value.optionEl.querySelector('input[type="checkbox"]'); |
| 245 | + if (checkbox) checkbox.checked = true; |
| 246 | + } |
236 | 247 | _deselectValue(value) {
|
| 248 | + value.el.selected = false; |
237 | 249 | value.optionEl.classList.remove('selected');
|
238 |
| - value.el.removeAttribute('selected'); |
| 250 | + const checkbox = value.optionEl.querySelector('input[type="checkbox"]'); |
| 251 | + if (checkbox) checkbox.checked = false; |
239 | 252 | }
|
240 | 253 | _deselectAll() {
|
241 | 254 | this._values.forEach((value) => {
|
242 | 255 | this._deselectValue(value);
|
243 | 256 | });
|
244 | 257 | }
|
| 258 | + _isValueSelected(value) { |
| 259 | + const realValues = this.getSelectedValues(); |
| 260 | + return realValues.some((realValue) => realValue === value.el.value); |
| 261 | + } |
245 | 262 | _toggleEntryFromArray(value) {
|
246 |
| - const li = value.optionEl; |
247 |
| - const isSelected = li.classList.contains('selected'); |
248 |
| - if (isSelected) { |
249 |
| - value.el.removeAttribute('selected'); |
250 |
| - li.classList.remove('selected'); |
251 |
| - } else { |
252 |
| - value.el.setAttribute('selected', 'selected'); |
253 |
| - li.classList.add('selected'); |
254 |
| - } |
255 |
| - li.querySelector('input[type="checkbox"]').checked = !isSelected; |
256 |
| - return isSelected; |
| 263 | + const isSelected = this._isValueSelected(value); |
| 264 | + if (isSelected) this._deselectValue(value); |
| 265 | + else this._selectValue(value); |
257 | 266 | }
|
258 |
| - _isOptionChosen(realOption) { |
259 |
| - if (realOption.hasAttribute('disabled')) return false; |
260 |
| - return realOption.selected || realOption.hasAttribute('selected'); |
| 267 | + _getSelectedOptions() { |
| 268 | + return Array.prototype.map.call(this.el.selectedOptions, (realOption) => realOption); |
261 | 269 | }
|
| 270 | + |
262 | 271 | _setValueToInput() {
|
263 |
| - const texts = this._values |
264 |
| - .filter((value) => this._isOptionChosen(value.el)) |
265 |
| - .map((value) => value.optionEl.querySelector('span').innerText.trim()); |
266 |
| - // Set input-text to first Option with empty value which indicates a description like "choose your option" |
267 |
| - if (texts.length === 0) { |
268 |
| - const firstDisabledOption = this.$el.find('option:disabled').eq(0); |
269 |
| - if (firstDisabledOption.length > 0 && firstDisabledOption[0].value === '') { |
270 |
| - this.input.value = firstDisabledOption.text(); |
271 |
| - return; |
272 |
| - } |
273 |
| - } |
| 272 | + const realOptions = this._getSelectedOptions(); |
| 273 | + const values = this._values.filter((value) => realOptions.indexOf(value.el) >= 0); |
| 274 | + const texts = values.map((value) => value.optionEl.querySelector('span').innerText.trim()); |
274 | 275 | this.input.value = texts.join(', ');
|
275 | 276 | }
|
276 | 277 | _setSelectedStates() {
|
|
291 | 292 | }
|
292 | 293 |
|
293 | 294 | getSelectedValues() {
|
294 |
| - return this._values |
295 |
| - .filter((value) => this._isOptionChosen(value.el)) |
296 |
| - .map((value) => value.el.value); |
| 295 | + return this._getSelectedOptions().map((realOption) => realOption.value); |
297 | 296 | }
|
298 | 297 | }
|
299 | 298 |
|
|
0 commit comments