【问题标题】:KnockoutJS Select item from foreachKnockoutJS 从 foreach 中选择项目
【发布时间】:2013-10-07 20:07:50
【问题描述】:

我有一个 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,依此类推。 我希望你明白我的意思。

【问题讨论】:

  • 绑定处理程序将在每次插入绑定列表时被调用。所以基本上如果你订阅 groupOperations 你的代码会像你喜欢的那样工作吗?
  • 你是对的。如果我创建一个订阅者,它会按预期工作。但是我已经选择了 bindinghandler,这样我就可以访问 bindingContext, $root, ... 。我可以和订阅者一起做吗??
  • 不知何故我不懂代码。您正在使用 value =>groupOperations 和 options => 运算符。但是 groupOperations 也使用运算符。你不觉得这很奇怪吗?如果您需要这种功能,很多地方绑定处理程序都会做得很好。但似乎你不需要它。如果 groupOperations 是简单的可观察值,您将订阅它。
  • operators 只是一个对象 var operator = [ { "Name": "Und", "Wert": 0 }, { "Name": "Und nicht", "Wert": 1 }, {“名称”:“Oder”,“Wert”:2},{“名称”:“Oder nicht”,“Wert”:3}];是不是不需要设定价值??
  • knockoutjs.com/documentation/value-binding.html。我已经使用了文档。

标签: javascript asp.net-mvc-4 knockout.js


【解决方案1】:

让我们在这里讨论。

operators 是一个数组,你用它来绑定选择

self.groupOperations = ko.observable(operators);

而不是

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
});

当绑定到选择时

 <!-- 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 -->

【讨论】:

  • 没错。我将运算符用于多个绑定。每次推送时,我都会使用新的选择和新的 groupOperations 创建新的 groupOperationsTemplate。该函数是addGreoupOperator。那么为什么订阅者按预期工作而绑定处理程序没有呢?我不明白其中的区别。
  • 我想给你看一张我的意思的图片,但我做不到。
  • 你能为此做一个 jsfiddle 吗?
  • jsfiddle.net/lrz_hal/cTR6v/26。如果您单击“Filter hinzufügen”,您可以在 groupoperationsTemplate 中添加一个新行。如果您单击“Filtergruppe hinzufügen”,您将放置一个新的 groupOperationsTemplate。所以我想要的是,如果你有多行 groupOperationsTemplate,我想检测左侧的选择是否发生了变化,然后遍历所有其他选择并更改它们。该项目是一个 ldap 过滤器。在 ldap 中,您只能将“und”和“und nicht”放在一起。或“Oder”和“Oder nicht”。
  • 这看起来太复杂了,如果没有 bindingHandler 和一个组合框我能做到吗?
猜你喜欢
  • 1970-01-01
  • 2015-09-04
  • 1970-01-01
  • 1970-01-01
  • 2012-05-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多