|
768 | 768 |
|
769 | 769 | this.multiple = this.$element.prop('multiple');
|
770 | 770 | this.autofocus = this.$element.prop('autofocus');
|
| 771 | + |
771 | 772 | this.$newElement = this.createDropdown();
|
772 |
| - this.createLi(); |
773 | 773 | this.$element
|
774 | 774 | .after(this.$newElement)
|
775 | 775 | .prependTo(this.$newElement);
|
| 776 | + |
776 | 777 | this.$button = this.$newElement.children('button');
|
777 | 778 | this.$menu = this.$newElement.children(Selector.MENU);
|
778 | 779 | this.$menuInner = this.$menu.children('.inner');
|
|
852 | 853 | }
|
853 | 854 |
|
854 | 855 | setTimeout(function () {
|
| 856 | + that.createLi(); |
855 | 857 | that.$element.trigger('loaded' + EVENT_KEY);
|
856 | 858 | });
|
857 | 859 | },
|
|
1156 | 1158 | });
|
1157 | 1159 | },
|
1158 | 1160 |
|
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; |
1179 | 1163 |
|
1180 | 1164 | if (this.options.title && !this.multiple) {
|
| 1165 | + if (!this.selectpicker.view.titleOption) this.selectpicker.view.titleOption = document.createElement('option'); |
| 1166 | + |
1181 | 1167 | // this option doesn't create a new <li> element, but does add a new option, so liIndex is decreased
|
1182 | 1168 | // since newIndex is recalculated on every refresh, liIndex needs to be decreased even if the titleOption is already appended
|
1183 |
| - liIndex--; |
| 1169 | + updateIndex = true; |
1184 | 1170 |
|
1185 | 1171 | var element = this.$element[0],
|
1186 | 1172 | isSelected = false,
|
|
1208 | 1194 | if (isSelected) element.selectedIndex = 0;
|
1209 | 1195 | }
|
1210 | 1196 |
|
| 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 | + |
1211 | 1221 | var selectOptions = this.$element[0].options;
|
1212 | 1222 |
|
1213 | 1223 | for (var index = 0, len = selectOptions.length; index < len; index++) {
|
|
1449 | 1459 | },
|
1450 | 1460 |
|
1451 | 1461 | render: function () {
|
| 1462 | + // ensure titleOption is appended and selected (if necessary) before getting selectedOptions |
| 1463 | + this.setPlaceholder(); |
| 1464 | + |
1452 | 1465 | var that = this,
|
1453 | 1466 | selectedOptions = this.$element[0].selectedOptions,
|
1454 | 1467 | selectedCount = selectedOptions.length,
|
|
0 commit comments