使用KnockOut - 我試圖在添加到MVC .Net代碼之前構建主要細節/項目應用程序的基礎知識。KnockOut的計算功能問題
所有我想要做的是有一個簡單的項目,價格,稅收 - 併爲計算欄,顯示的金額含稅每個項目:
客戶端淘汰賽視圖模型是:
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 });
表格標記是:
<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>
它拖延爲formattedPrice
功能犯規似乎是工作。
我已經在一個jsfiddle這裏:http://jsfiddle.net/marktait/TR6Sy/ - 任何人都可以幫助我克服這個看似簡單的障礙嗎?
謝謝
馬克
嗨 - 謝謝你。我認爲增加一條線並改變任何內容(即文本框中的稅額)會很簡單,並且會自動重新計算含稅價格。我可能一直期待太多。謝謝你的時間。 – Mark 2013-05-14 13:38:33
沒問題,樂意幫忙! – Jalayn 2013-05-14 13:40:07