javascript - How to get the elements selected of checkbox with knockout -
<div id="title" data-bind="foreach: list"> <input type="checkbox" data-bind="attr:{id: $index,value: list.id}" class="k-checkbox"> <label class="k-checkbox-label" data-bind="attr:{for: $index},checked: myfunction(this),text: list.id"></label> </div> <span data-bind="text: elementsselected"></span> </div>
myfunction check if selected checkbox. elementsselected elements selected of 'list'. print array span.
you can use ko.purecomputed
show data based on condition.
here example:
var simplelistmodel = function(id, item) { var self = this; self.id = ko.observable(id); self.item = ko.observable(item); self.isselected = ko.observable(false); self.printitem = ko.purecomputed(function() { if(self.isselected()) return self.id() + " " + self.item(); else return ""; }, this); }; var mastervm = (function () { var self = this; self.lists = ko.observablearray(); for(var = 0; i<5; i++) { self.lists.push(new simplelistmodel(i, "item no. " + i)); } })(); ko.applybindings(mastervm);
<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> <div data-bind="foreach: lists"> <div> <span>check show item: </span> <input type="checkbox" data-bind="checked: isselected" /> <span data-bind="text: printitem"></span> </div> </div>
Comments
Post a Comment