I have this checkbox control that dynamically populates
<!-- ko foreach: AllPens -->
<label>
<input type="checkbox" data-bind="checked: IsChecked" />
<span data-bind="text: name"></span>
</label>
<!-- /ko -->
Assuming the observable array GETs (AllPens)
{ code: "001" , name: "Parker"},
{ code: "002" , name: "Sheaffer"},
{ code: "003" , name: "Mont Blanc"}
I have to POST back an array of the checked elements -
"Pens": [{
"PenType": "001",
"Order": false
}, {
"PenType": "002",
"Order": true
}]
I have a rough idea to store the checked Pen's code and if ischecked in an object - new Pen('001',true)
function Pen(type, checked) {
var self = this;
self.PenType = ko.observable(type);
self.IsChecked = ko.observable(false);
}
How can I bind checkbox value to Knockout observableArray on an object?
I believe the below should work if i figure out the above.
self.Pens= ko.computed(function()
{
var selectedPens = [];
ko.utils.arrayForEach(self.Pen(), function (pen) {
if(pen.IsChecked())
selectedPens.push(pen);
});
return selectedPens;
});
I'm still learning KO. Any help is appreciated :)