【发布时间】:2018-04-15 13:42:39
【问题描述】:
我在让具有倍数的 JQuery Select2 和标签与 KnockoutJS 一起工作时遇到问题。
到目前为止,我使用传统的选择框 see this jsfiddle 可以正常工作。
使用带有单例选项的 Select2 时也可以正常工作,see this jsfiddle。
但是一旦我引入标签并多选整个瘦身分崩离析,see this jsfiddle
能否请您帮我在最后一个jsfiddle中正确绑定并打印出右侧的多个选定值?
这是最后一个例子的html代码:
<table data-bind="foreach: fLines">
<tr>
<td>
<select style="width:150px;" multiple="true" class="js-example-basic-multiple" data-bind="options: $root.formatValues, value: type"></select>
</td>
<td>
<a href="#" data-bind='click: function() { $root.fLines.remove($data); }'>Remove</a>
</td>
<td>
Select value is: <span data-bind="text: type"></span>
</td>
</tr>
</table>
<button style="background-color: transparent; border: none;" data-bind="click: addfLine">Add Format</button>
和它的 JS:
$(document).ready(function() {
$('.js-example-basic-multiple').select2({
tags: true
});
});
var Item = function(format) {
var self = this;
self.type = ko.observable(format);
self.value1 = ko.observable();
};
var Formatters = {
formatValues: ko.observableArray(["A", "B", "C"]),
fLines: ko.observableArray([new Item("C")]),
addfLine: function() {
this.fLines.push(new Item("C"));
$('.js-example-basic-single').select2({
tags: true
});
},
removefLline: function() {
this.fLines.remove(ko.dataFor(this));
}
};
ko.applyBindings(Formatters);
【问题讨论】:
-
模板如何适应?您是否尝试通过选择交换每行使用的模板?如果是这样,如果您选择了多个模板,它应该是什么样子?
标签: knockout.js jquery-select2 knockout-3.0