我正在購物車(http://beta.jcantiques.com/Home/Cart) 您需要從產品頁面添加項目。KO Computed field not working
我無法獲得SUM CBM字段的工作。
這裏是代碼:
JS
<script>
$(document).ready(function() {
var CartLine = function() {
var self = this;
self.UniqueItemCode = ko.observable();
self.ItemCode = ko.observable();
self.Description = ko.observable();
self.SizeInCm = ko.observable();
self.CmCBM = ko.observable();
self.Wood = ko.observable();
self.Quantity = ko.observable();
self.subtotalCBM = ko.computed(function() {
return (parseFloat(self.CmCBM) * parseFloat(self.Quantity));
});
};
var Cart = function() {
var self = this;
self.lines = ko.mapping.fromJS([]);
//Converting C# data into JS array
var orderItems = JSON.parse('@Html.Raw(Json.Encode(Model))');
console.log(orderItems);
// Mapping JS array to KO array
ko.mapping.fromJS(orderItems, {}, self.lines);
self.grandTotalCBM = ko.computed(function() {
var total = 0;
$.each(self.lines(), function() { total += this.subtotalCBM(); })
return total;
});
self.totalQty = ko.computed(function() {
var total = 0;
$.each(self.lines(), function() { total += parseInt(this.Quantity()) })
return total;
});
};
ko.applyBindings(new Cart());
});
</script>
HTML
<tbody data-bind="foreach: lines">
<tr>
<td><a href="#" class="icon-close remove-item" data-bind="click: $parent.removeLine"><span aria-hidden="true" class="icon_close"></span></a></td>
<td>
<img style="width:70px; height:70px;" onerror="if (this.src != '/Content/Images/no_picture.gif') this.src = '/Content/Images/no_picture.gif';" data-bind=" attr: { 'src': '/Content/OPAMS_Images/' + ItemCode + '/' + ItemCode + '-Thumb.jpg' }" />
</td>
<td>
<span data-bind="text: ItemCode"></span>
</td>
<td>
<span data-bind="text: Description" style="max-width:200px;"></span>
</td>
<td>
<span data-bind="text: SizeInCm"></span>
</td>
<td>
<span data-bind="text: CmCBM"></span>
</td>
<td>
<span data-bind="text: Wood"></span>
</td>
<td>
<input data-bind='value: Quantity, valueUpdate: "afterkeydown"' style="width: 60px;" />
<span class="hidden" data-bind="text: Quantity"></span>
</td>
<td>
<span data-bind="text: subtotalCBM"></span>
</td>
</tr>
</tbody>
任何幫助將不勝感激。
我查看了您的網站,您沒有使用您創建的CartLine模型。 – Adrian