Is it possible to have an object inside an observableArray that's a computed value of other observables within the same object? I have the following array:
self.drug_costs_table = ko.observableArray([
{
label: ko.observable('salad'),
cost_per_dose: ko.observable('123'),
administrations: ko.observable('9'),
discount: ko.observable('10'),
cost_per_year: ko.computed(function () {
// administrations * cost per dose
})
}
]);
The observableArray will be built dynamically in the HTML and the final column is drug cost x administrations. Is it possible to have this value in the array or will something have to be done outside of the array to compute the cost_per_year?
This is my HTML:
<!-- ko foreach: drug_costs_table -->
<div class="row">
<div class="grid_4"><label data-bind="text: label"></label></div>
<div class="grid_2"><input class="total-val" data-bind="decimal: cost_per_dose"></div>
<div class="grid_2"><input data-bind="number: administrations"></div>
<div class="grid_2"><input data-bind="percentage: discount"></div>
<div class="grid_2"><input class="total-val" data-bind="decimal: cost_per_year"></div>
</div>
<!-- /ko -->
Any help/advice on this would be greatly appreciated; I'm still a bit of a newbie to knockout!
Fiddle: http://jsfiddle.net/VWc8e/