Why initial value selectPicker doesn't work with Knockout version 3.4? With Knockout 3.0 works.
<select data-bind="selectPicker: teamID, items: teamItems, optionsText: 'text', optionsValue : 'id'"></select>
<div>Selected Value(s)
<div data-bind="text: teamID"></div>
</div>
<button data-bind="click: setActive">
Set active
</button>
<button data-bind="click: addStef">
Add Stefan
</button>
function ViewModel() {
var self = this;
this.teamItems = ko.observableArray([
{
text: 'Chris',
id: 1
},
{
text: 'Peter',
id: 2
},
{
text: 'John',
id: 3
}]);
//init value not working
this.teamID = ko.observable(3);
////
this.setActive = function () {
this.teamID(3);
}
this.addStef = function () {
this.teamItems.push({ text: "Stef", id: 4 })
}
}
ko.bindingHandlers.selectPicker = {
init: function (element, valueAccessor, allBindingsAccessor) {
if ($(element).is('select')) {
$(element).addClass('selectpicker').selectpicker();
if (ko.isObservable(valueAccessor())) {
if ($(element).prop('multiple') && $.isArray(ko.utils.unwrapObservable(valueAccessor()))) {
ko.bindingHandlers.selectedOptions.init(element, valueAccessor, allBindingsAccessor);
} else {
ko.bindingHandlers.value.init(element, valueAccessor, allBindingsAccessor);
}
}
}
},
update: function (element, valueAccessor, allBindingsAccessor) {
if ($(element).is('select')) {
var options = allBindingsAccessor().items;
if (typeof options !== 'undefined' && options !== null) {
var isDisabled = allBindingsAccessor().disable || false;
if (ko.utils.unwrapObservable(options).length > 0) {
// call the default Knockout options binding
ko.bindingHandlers.options.update(element, options, allBindingsAccessor);
}
if (isDisabled) {
// the dropdown is disabled and we need to reset it to its first option
$(element).selectpicker('val', $(element).children('option:first').val());
}
$(element).prop('disabled', isDisabled);
}
if (ko.isObservable(valueAccessor())) {
var value = ko.utils.unwrapObservable(valueAccessor());
if ($(element).prop('multiple') && $.isArray(value)) {
ko.bindingHandlers.selectedOptions.update(element, valueAccessor);
} else {
// call the default Knockout value binding
ko.bindingHandlers.value.update(element, valueAccessor);
}
}
}
}
};
ko.applyBindings(new ViewModel());
Problem is with the line this.teamID = ko.observable(3);
Selected value is always equal first element in array.
Updated answer:
Instead of wrapping all the options binding functionality, you can just use the options
binding, which will do all of that correctly:
<select data-bind="selectPicker:true, options:teamItems, value:teamID,optionsText:'text',optionsValue:'id'"></select>
Then your binding handler just needs to ensure that it responds to changes to the options and value:
update: function(element, valueAccessor, allBindingsAccessor) {
if ($(element).is('select')) {
var isDisabled = ko.utils.unwrapObservable(allBindingsAccessor().disable);
if (isDisabled) {
// the dropdown is disabled and we need to reset it to its first option
$(element).selectpicker('val', $(element).children('option:last').val());
}
// React to options changes
ko.unwrap(allBindingsAccessor.get('options'));
// React to value changes
ko.unwrap(allBindingsAccessor.get('value'));
// Wait a tick to refresh
setTimeout(() => {$(element).selectpicker('refresh');}, 0);
}
}
Original answer:
Add the valueAllowUnset
binding. At some point Knockout thinks that the selected value is not in the options, so it is resetting to the first item.
<select data-bind="selectPicker:teamItems,value:teamID,optionsText:'text',optionsValue:'id',valueAllowUnset:true"></select>