Skip to content

Commit 50b2327

Browse files
committed
move createLi into a setTimeout on init to prevent interference with page load (#1665, #2187)
1 parent bce0441 commit 50b2327

File tree

1 file changed

+35
-22
lines changed

1 file changed

+35
-22
lines changed

js/bootstrap-select.js

+35-22
Original file line numberDiff line numberDiff line change
@@ -768,11 +768,12 @@
768768

769769
this.multiple = this.$element.prop('multiple');
770770
this.autofocus = this.$element.prop('autofocus');
771+
771772
this.$newElement = this.createDropdown();
772-
this.createLi();
773773
this.$element
774774
.after(this.$newElement)
775775
.prependTo(this.$newElement);
776+
776777
this.$button = this.$newElement.children('button');
777778
this.$menu = this.$newElement.children(Selector.MENU);
778779
this.$menuInner = this.$menu.children('.inner');
@@ -852,6 +853,7 @@
852853
}
853854

854855
setTimeout(function () {
856+
that.createLi();
855857
that.$element.trigger('loaded' + EVENT_KEY);
856858
});
857859
},
@@ -1156,31 +1158,15 @@
11561158
});
11571159
},
11581160

1159-
createLi: function () {
1160-
var that = this,
1161-
iconBase = that.options.iconBase,
1162-
mainElements = [],
1163-
widestOption,
1164-
widestOptionLength = 0,
1165-
mainData = [],
1166-
optID = 0,
1167-
headerIndex = 0,
1168-
liIndex = -1; // increment liIndex whenever a new <li> element is created to ensure newIndex is correct
1169-
1170-
if (!this.selectpicker.view.titleOption) this.selectpicker.view.titleOption = document.createElement('option');
1171-
1172-
var checkMark;
1173-
1174-
if (that.options.showTick || that.multiple) {
1175-
checkMark = elementTemplates.span.cloneNode(false);
1176-
checkMark.className = iconBase + ' ' + that.options.tickIcon + ' check-mark';
1177-
elementTemplates.a.appendChild(checkMark);
1178-
}
1161+
setPlaceholder: function () {
1162+
var updateIndex = false;
11791163

11801164
if (this.options.title && !this.multiple) {
1165+
if (!this.selectpicker.view.titleOption) this.selectpicker.view.titleOption = document.createElement('option');
1166+
11811167
// this option doesn't create a new <li> element, but does add a new option, so liIndex is decreased
11821168
// since newIndex is recalculated on every refresh, liIndex needs to be decreased even if the titleOption is already appended
1183-
liIndex--;
1169+
updateIndex = true;
11841170

11851171
var element = this.$element[0],
11861172
isSelected = false,
@@ -1208,6 +1194,30 @@
12081194
if (isSelected) element.selectedIndex = 0;
12091195
}
12101196

1197+
return updateIndex;
1198+
},
1199+
1200+
createLi: function () {
1201+
var that = this,
1202+
iconBase = that.options.iconBase,
1203+
mainElements = [],
1204+
widestOption,
1205+
widestOptionLength = 0,
1206+
mainData = [],
1207+
optID = 0,
1208+
headerIndex = 0,
1209+
liIndex = -1; // increment liIndex whenever a new <li> element is created to ensure newIndex is correct
1210+
1211+
var checkMark;
1212+
1213+
if (that.options.showTick || that.multiple) {
1214+
checkMark = elementTemplates.span.cloneNode(false);
1215+
checkMark.className = iconBase + ' ' + that.options.tickIcon + ' check-mark';
1216+
elementTemplates.a.appendChild(checkMark);
1217+
}
1218+
1219+
if (this.setPlaceholder()) liIndex--;
1220+
12111221
var selectOptions = this.$element[0].options;
12121222

12131223
for (var index = 0, len = selectOptions.length; index < len; index++) {
@@ -1449,6 +1459,9 @@
14491459
},
14501460

14511461
render: function () {
1462+
// ensure titleOption is appended and selected (if necessary) before getting selectedOptions
1463+
this.setPlaceholder();
1464+
14521465
var that = this,
14531466
selectedOptions = this.$element[0].selectedOptions,
14541467
selectedCount = selectedOptions.length,

0 commit comments

Comments
 (0)