敲除JS从中选择一个项目
KnockoutJS Select item from foreach
我有一个foreach绑定
<table id="Table_OperationsGroup">
<tbody data-bind="foreach: groupOperationsGroup">
<!-- ko if: $index() < $root.groupOperationsGroup().length - 1 -->
<tr>
<td>
<select data-bind="changeGroup: groupOperations,options: operators, optionsText: 'Name', value: groupOperations" style="width: 105px;margin-top:5px !important;margin-bottom:5px !important;margin-left:0px !important;"></select>
</td>
</tr>
<!-- /ko -->
</tbody>
</table>
.JS
ko.bindingHandlers.changeGroup = {
update: function (element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
//some code to work with the select
}
};
var groupOperationsTemplate = function () {
var self = this;
self.groupOperations = ko.observable(operators);
self.lines = ko.observableArray([{
operations: ko.observable()
}]);
self.addLine = function (line) {
line.lines.push({
operations: ko.observable(operators)
})
};
self.removeLine = function (line) {
if (self.lines().length > 1) {
self.lines.remove(line);
}
};
};
var Filter = function () {
var self = this;
//self.template = ko.observableArray();
self.groupOperationsGroup = ko.observableArray([new groupOperationsTemplate()]);
self.addGroupOperator = function (data) {
self.groupOperationsGroup.splice(self.groupOperationsGroup.indexOf(data) + 1, 0, new groupOperationsTemplate());
};
};
var vm = new Filter();
ko.applyBindings(vm);
所以,我想要的是,如果有人更改了选择,我想准确地选择在绑定处理程序中更改的选择。问题是,每个选择都会调用绑定处理程序。它从 0 开始,然后是 1,2,3,依此类推。我希望你明白我的意思。
让我们在这里讨论。
运算符是一个数组,你用它来绑定选择
self.groupOperations = ko.observable(operator);
而不是
self.groupOperations = ko.observableArray([ { "Name": "Und", "Wert": 0 }, { "Name": "Und nicht", "Wert": 1 }, { "Name": "Oder", "Wert": 2 }, { "Name": "Oder nicht", "Wert": 3 } ]);
self.selectedValue = ko.observable();
self.selectedValue.subscribe(function( newValue) {
//do whatever you want with new value
});
绑定到 select 时
<!-- ko if: $index() < $root.groupOperationsGroup().length - 1 -->
<tr>
<td>
<select data-bind="options: groupOperations, optionsText: 'Name', value: selectedValue" style="width: 105px;margin-top:5px !important;margin-bottom:5px !important;margin-left:0px !important;"></select>
</td>
</tr>
<!-- /ko -->
相关文章:
- 当在Ember中点击一个项目时,我如何将一个活动类添加到项目列表中
- angularjs移除焦点上的活动类并添加到下一个项目
- 如何通过AngularJS删除一个项目
- 获取每个项目中不为 null 的最后一个项目
- 在另一个项目中使用大理石测试rxjs5方法
- Asp.net 引导下拉菜单 - 选择一个项目
- 处理一个项目,想要添加以下菜单栏,并尽量减少向下滑动
- 在angularJs的选择框中预先选择一个项目
- jQuery 使用 ID 作为 URL 选择一个项目
- 我在Phaser有一个项目.我想在画布上的元素上做一个用户图像编辑器
- Javascript:使用 slice() 从数组中选择除一个项目之外的所有项目
- 如何从包含1000个项目的数组中随机选择一个项目
- 从列表中选择一个项目,
- 在可排序的JQuery UI列表中至少保留一个项目
- 仅单击一行中的一个项目
- 在angularjs中一次选择一个项目
- 有没有更花哨的方法可以在同一类中获得上一个项目
- 如何从用户制作的数组中具体拼接出一个项目
- 角度 - ng 网格通过分组按语法选择下一个/上一个项目
- 是否可以通过使用 javascript 获取数组中的一个项目来删除项目