Skip to content

Commit

Permalink
use native JS for val method instead of jQuery. let val work when opt…
Browse files Browse the repository at this point in the history
…ions are set via data property. have setSelected automatically update the selected property for options (#899)
  • Loading branch information
caseyjhol committed May 8, 2020
1 parent f98e9b5 commit 77093f3
Showing 1 changed file with 32 additions and 14 deletions.
46 changes: 32 additions & 14 deletions js/bootstrap-select.js
Original file line number Diff line number Diff line change
Expand Up @@ -2373,7 +2373,10 @@

if (!li) return;

if (selected !== undefined) liData.selected = selected;
if (selected !== undefined) {
liData.selected = selected;
if (liData.option) liData.option.selected = selected;
}

if (selected && this.options.data) {
this.createOption(liData, false);
Expand Down Expand Up @@ -2505,12 +2508,9 @@
prevData = prevOption ? that.selectpicker.main.data[prevOption.liIndex] : false;

if (prevData) {
prevData.selected = false;
if (prevData.source) prevData.source.selected = false;
that.setSelected(prevData, false);
}

if (prevOption) prevOption.selected = false;

element.selectedIndex = 0;

that.render();
Expand Down Expand Up @@ -2622,13 +2622,9 @@
}

if (!that.multiple) { // Deselect previous option if not multi select
if (prevData) prevData.selected = false;
if (prevOption) prevOption.selected = false;
option.selected = true;
if (prevData) that.setSelected(prevData, false);
that.setSelected(clickedData, true);
} else { // Toggle the clicked option if multi select.
option.selected = !state;

that.setSelected(clickedData, !state);
$this.trigger('blur');

Expand Down Expand Up @@ -2856,13 +2852,35 @@
var element = this.$element[0];

if (typeof value !== 'undefined') {
var prevValue = getSelectValues.call(this);
var selectedOptions = getSelectedOptions.call(this),
prevValue = getSelectValues.call(this, selectedOptions);

changedArguments = [null, null, prevValue];

this.$element
.val(value)
.trigger('changed' + EVENT_KEY, changedArguments);
if (!Array.isArray(value)) value = [ value ];

value.map(String);

for (var i = 0; i < selectedOptions.length; i++) {
var item = selectedOptions[i];

if (item && value.indexOf(String(item.value)) === -1) {
this.setSelected(item, false);
}
}

this.selectpicker.main.data.filter(function (item) {
if (value.indexOf(String(item.value)) !== -1) {
this.setSelected(item, true);
return true;
}

return false;
}, this);

if (this.options.data) element.appendChild(this.selectpicker.main.optionQueue);

this.$element.trigger('changed' + EVENT_KEY, changedArguments);

if (this.$newElement.hasClass(classNames.SHOW)) {
if (this.multiple) {
Expand Down

0 comments on commit 77093f3

Please sign in to comment.