select2 集成knockoutjs
1. execute scripts when page loading:
ko.bindingHandlers.select2 = { init: function(element, valueAccessor, allBindingsAccessor) { var options = ko.toJS(valueAccessor()) || {}; var allBindings = allBindingsAccessor(); var lookupKey = allBindings.lookupKey; setTimeout(function() { $(element).select2(options); }, 0); if (lookupKey) { var value = ko.utils.unwrapObservable(allBindings.value); $(element).select2('data', ko.utils.arrayFirst(options.data.results, function(item) { return item[lookupKey] === value; })); } ko.utils.domNodeDisposal.addDisposeCallback(element, function() { $(element).select2('destroy'); }); }, update: function(element) { $(element).trigger('change'); } };
2. page html tag, Ex:
<select id="form_input1_orientation"></select>
3. data binding :
$("#form_input1_orientation").attr("data-bind", "options: orientationCategories, optionsValue: 'Id', optionsText: 'Name', selectedOptions: install().orientation_1, select2: {}");
4. view modal:
var orientationCategoriesData = [ {Id: "north", Name: "正北"}, {Id: "northeast", Name: "东北"}, {Id: "northwest", Name: "西北"}, {Id: "east", Name: "正东"}, {Id: "southeast", Name: "东南"}, {Id: "south", Name: "正南"}, {Id: "southwest", Name: "西南"}, {Id: "west", Name: "正西"} ] self.orientationCategories = ko.observableArray(orientationCategoriesData); self.orientation_1 = ko.observable(["east"]);
enjoy.
相关推荐
85931734 2019-11-25
88901533 2014-11-06
89570797 2015-03-11
85580894 2014-04-02
89570797 2016-05-31
80961131 2015-03-11
87941631 2015-02-10
89233916 2015-01-14
89233916 2015-01-07
87941631 2014-11-06
89233916 2014-11-04
80961131 2014-11-03
89433869 2014-09-11
89570797 2013-08-24
88901533 2012-08-24
87941631 2012-06-14
89901038 2017-12-18
89901038 2017-07-31
85971631 2019-05-08