I have the following viewModel
var viewModel = new myViewModel([{
Name: "Name",
price: 32,
tax: 22,
}, {
Name: "Name",
price: 32,
tax: 22,
}]);
I have a data-bind to
<tbody data-bind='foreach: personInfo'>
and input:
<td>
<input class='required' data-bind='value: Name'/>
</td>
<td>
<input class='required' data-bind='value: Price'/>
</td>
<td>
<input class='required' data-bind='value: Tax'/>
</td>
and I want to get the computed value for price and then for tax however I am not successful. :(
self.totalSurcharge = ko.computed(function () {
var total = 0;
for (var i = 0; self.PersonInfo().length; i++)
total += self.PersonInfo[i].price;
return total;
});
any idea?
UPDATE:
something like this: http://jsfiddle.net/hamsaya/9XNDH/1/
Thanks
I have tested the below code which is working fine,
Modify your html as below:
<tr>
<td> Total of price here</td>
<td data-bind="text:totalPrice"></td>
</tr>
Add below Computed Observable in your script to calculate total price:
self.totalPrice = ko.computed({
read: function() {
var totalAmount = null;
for(var i=0 , j=self.gifts().length ; i < j ; i++ )
{
totalAmount =Number(totalAmount)+ Number(self.gifts()[i].price);
}
if(totalAmount == 0){
totalAmount = '0.00'
}
return totalAmount;
}
});