Working with KnockOut - I'm trying to build the basics of a master detail/items app before adding to MVC .Net code.
All I want to do is have a simple item, price, tax - and for a computed column to show the amount including tax for each item:
The client side KnockOut viewmodel is:
var GiftModel = function(gifts) {
var self = this;
self.gifts = ko.observableArray(gifts);
self.formattedPrice = ko.computed(function() {
var pricet = self.gifts().price;
return pricet ? "$" + pricet.toFixed(2) * (1 + self.gifts().tax : "None";
});
self.addGift = function() {
self.gifts.push({
name: "",
price: "",
tax:0
});
};
self.removeGift = function(gift) {
self.gifts.remove(gift);
};
self.save = function(form) {
alert("Could now transmit to server: " + ko.utils.stringifyJson(self.gifts));
// To actually transmit to server as a regular form post, write this: ko.utils.postJson($("form")[0], self.gifts);
};
};
var viewModel = new GiftModel([
{ name: "Tall Hat", price: "39.95", tax:17.5},
{ name: "Long Cloak", price: "120.00", tax:20}
]);
ko.applyBindings(viewModel);
// Activate jQuery Validation
$("form").validate({ submitHandler: viewModel.save });
The table markup is:
<table data-bind='visible: gifts().length > 0'>
<thead>
<tr>
<th>Gift name</th>
<th>Price</th>
<th>Tax</th>
<th />
</tr>
</thead>
<tbody data-bind='foreach: gifts'>
<tr>
<td><input class='required' data-bind='value: name, uniqueName: true' /></td>
<td><input class='required number' data-bind='value: price, uniqueName: true' /></td>
<td><input class='required number' data-bind='value: tax, uniqueName: true' /></td>
<td data-bind='text: formattedPrice'></td>
<td><a href='#' data-bind='click: $root.removeGift'>Delete</a></td>
</tr>
</tbody>
</table>
<button data-bind='click: addGift'>Add Gift</button>
<button data-bind='enable: gifts().length > 0' type='submit'>Submit</button>
It's stalling as the formattedPrice
function doesnt appear to be working.
I've got it in a jsfiddle here: http://jsfiddle.net/marktait/TR6Sy/ - would anyone be able to help me get over this seemingly simple hurdle?
Thank you,
Mark