knockout.js - Select2 4.0 and Knockout 3.1 select not allowing selection -
i'm trying knockout (version 3.1) work select2 (version 4.0rc2).
i unable select accept input , have initial selection. select seems read only.
below fiddle demonstrating problem. tested on chrome (version 40.0.2214.115 m).
http://jsfiddle.net/r8uf5/402/
javascript:
ko.utils.setvalue = function (property, newvalue) { if (ko.isobservable(property)) property(newvalue); else property = newvalue; }; ko.bindinghandlers.select2 = { init: function (element, valueaccessor, allbindingsaccessor) { var obj = valueaccessor(), allbindings = allbindingsaccessor(), lookupkey = allbindings.lookupkey; $(element).select2(obj); ko.utils.registereventhandler(element, "select2-selected", function (data) { if ('selectedvalue' in allbindingsaccessor()) { ko.utils.setvalue(allbindingsaccessor().selectedvalue, data.choice); } }); ko.utils.domnodedisposal.adddisposecallback(element, function () { $(element).select2('destroy'); }); }, update: function (element, valueaccessor, allbindingsaccessor) { var options = allbindingsaccessor().select2options || {}; (var property in options) { $(element).select2(property, ko.utils.unwrapobservable(options[property])); } $(element).trigger('change'); } }; // constructor object 2 properties var country = function(name, population, price) { this.countryname = name; this.countrypopulation = population; this.price = price; this.id = price; this.text = name; }; var countries = [ new country("uk", 65000000,1), new country("usa", 320000000,2), new country("sweden", 29000000,3)]; var viewmodel = { availablecountries : ko.observablearray(countries), selectedcountry : ko.observable(countries[1]) }; ko.applybindings(viewmodel);
html:
your country: <select style="width:100%;" data-bind="options: $root.availablecountries, optionstext: 'countryname', value: selectedcountry, select2: { }">
here's updated, working fiddle: http://jsfiddle.net/r8uf5/404/
the main issue hitting knockout.js binds options same value
attribute, blank, , select2 couldn't handle correctly. able fix passing optionsvalue: 'id'
data-bind
attribute, correctly set value
attribute id.
the other issue select2 no longer handles selecting options same value
, i've opened ticket about: https://github.com/select2/select2/issues/3163
Comments
Post a Comment