将select2与剔除绑定
问题描述:
我是淘汰赛的新手,试图让我的select2在淘汰赛绑定中表现出色.
I'm new to knockout and trying to get my select2 to play nicely with my knockout bindings.
我要做的就是将帐户数组绑定到我的select2上(这可行),然后 绑定发生时设置初始值.我出于某种原因无法正常工作.还注意到init和update函数最初是被调用的,但是每当我更改select2下拉列表的值时,更新函数都不会被触发.
All I want to do is bind accounts array to my select2 (this works) and then have the initial value set when the binding occurs. I for some reason can not get this to work. Also noticed that the init and update function get called initially, but anytime I change the value of the select2 dropdown, the update function is not being triggered.
任何帮助将不胜感激.
HTML
<div class="col-sm-12 col-md-3">
<fieldset class="form-group">
<label data-bind="attr:{for:'job'+laborDetailId()}">Job</label>
<select class="select2" data-bind="attr:{id:'job'+laborDetailId()},updateaccountdropdown: {value:account(),data:accounts,width:'100%'}">
</select>
</fieldset>
</div>
JS
var accounts = [{"id":-1,"text":"","description":null}, {"id":25,"text":"J13002","description":null}, {"id":28,"text":"J13053","description":null}];
var LaborListModel = function(laborModels) {
var self = this;
//contains all labor models
self.labordetails = ko.observableArray(laborModels);
self.selectedAccount = ko.observable();
//bindings
ko.bindingHandlers.updateaccountdropdown = {
init: function(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
ko.utils.domNodeDisposal.addDisposeCallback(element, function() {
$(element).select2('destroy');
});
var allBindings = allBindingsAccessor(),
select2 = ko.utils.unwrapObservable(allBindings.updateaccountdropdown);
$(element).select2(select2);
},
update: function(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
var allBindings = allBindingsAccessor();
if ("value" in allBindings) {
var val = ko.utils.unwrapObservable(valueAccessor());
$(element).val(val.id).trigger('change');
}
}
};
}
getAllLaborDetails().done(function(result) {
loadAccounts().done(function(_accounts) {
accounts = _accounts;
for (var i = 0; i < result.length; i++) {
//LaborModel
var laborDetails = [];
laborDetails.push(new LaborModel(
result[i].labourDetailId,
result[i].account,
result[i].categoryModel,
result[i].description,
result[i].timeStamp,
result[i].hour
));
}
var vm = new LaborListModel(laborDetails);
ko.applyBindings(vm);
});
})
答
希望此"select2"自定义绑定可以帮助您:
Hope this "select2" custom binding helps you:
ko.bindingHandlers.select2 = {
after: ["options", "value"],
init: function (el, valueAccessor, allBindingsAccessor, viewModel) {
$(el).select2(ko.unwrap(valueAccessor()));
ko.utils.domNodeDisposal.addDisposeCallback(el, function () {
$(el).select2('destroy');
});
},
update: function (el, valueAccessor, allBindingsAccessor, viewModel) {
var allBindings = allBindingsAccessor();
var select2 = $(el).data("select2");
if ("value" in allBindings) {
var newValue = "" + ko.unwrap(allBindings.value);
if ((allBindings.select2.multiple || el.multiple) && newValue.constructor !== Array) {
select2.val([newValue.split(",")]);
}
else {
select2.val([newValue]);
}
}
}
};
$(document).ready(function(){
var data = ko.observableArray([{id: 1, text: "A"}, {id: 2, text: "B"}]);
var value = ko.observable();
ko.applyBindings({data: data, value: value});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.1/js/select2.full.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.1/css/select2.css" />
<span>Selected value: </span>
<span data-bind="text: value"></span>
<div style="width: 350px; margin-top: 20px;">
<select style="width: 100%;" data-bind="value: value, valueAllowUnset: true, options: data, optionsText: 'text', optionsValue: 'id', select2: { placeholder: 'Select an option...', allowClear: true }"></select>
</div>
- Select2团队( https://github.com/select2/select2/issues/3116)
- Stackoverflow社区(使用Ajax的Select2 4.0.0初始值,淘汰/选择2:触发select2根据可观察到的选项更新进行更新)
- Select2 Team (https://github.com/select2/select2/issues/3116)
- Stackoverflow Community (Select2 4.0.0 initial value with Ajax, Knockout/Select2: Triggering select2 to update based on an observable option updating)