javascript - KnockoutJS Select item from foreach -
i have foreach binding
<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 work 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);
so, want is, if change select, want pick select changing in bindinghandler. problem is, bindinghandler called every select. begins @ 0 , 1,2,3 , on. hope understand mean.
lets discuss here.
operators array , , use bind select
self.groupoperations = ko.observable(operators);
instead of
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 want new value });
when binding 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 -->
Comments
Post a Comment