私は単純だと思ったことを行うためにノックアウトを使用しています。私はノックアウトとJavaScriptには新しかったので、固執しました。どんな助けでも大歓迎です。以下は手元の問題です。観測可能な配列から計算された観測可能な配列
商品の3つの目録(開封、お届け、お届け)が配列形式であり、商品の在庫を配列形式で計算したいと考えています。実際のデータは少し複雑です。ここに私のデータに続く
var OpeningGasInventories = [{
Id: 0,
Volume: 0,
TimeStamp: "0001-01-01T05:00:00Z",
GasInventoryType: "Opening",
GasProductId: 1,
ShiftId: 1
}, {
Id: 0,
Volume: 0,
TimeStamp: "0001-01-01T05:00:00Z",
GasInventoryType: "Opening",
GasProductId: 2,
ShiftId: 1
}];
var ClosingGasInventories = [{
Id: 0,
Volume: 0,
TimeStamp: "0001-01-01T05:00:00Z",
GasInventoryType: "Opening",
GasProductId: 1,
ShiftId: 1
}, {
Id: 0,
Volume: 0,
TimeStamp: "0001-01-01T05:00:00Z",
GasInventoryType: "Opening",
GasProductId: 2,
ShiftId: 1
}];
var DeliveredGasInventories = [{
Id: 0,
Volume: 0,
TimeStamp: "0001-01-01T05:00:00Z",
GasInventoryType: "Opening",
GasProductId: 1,
ShiftId: 1
}, {
Id: 0,
Volume: 0,
TimeStamp: "0001-01-01T05:00:00Z",
GasInventoryType: "Opening",
GasProductId: 2,
ShiftId: 1
}];
var SoldGasInventories = [{
Id: 0,
Volume: 0,
TimeStamp: "0001-01-01T05:00:00Z",
GasInventoryType: "Opening",
GasProductId: 1,
ShiftId: 1
}, {
Id: 0,
Volume: 0,
TimeStamp: "0001-01-01T05:00:00Z",
GasInventoryType: "Opening",
GasProductId: 2,
ShiftId: 1
}];
var GasProductSales= [{
Id: 1,
CashPrice: 1.919,
CreditPrice: 0,
VolumeCashSale: 0,
VolumeCreditSale: 0,
AmountCashSale: 0,
AmountCreditSale: 0,
GasProductId: 1,
GasProductName: "Regular",
ShiftId: 1
}, {
Id: 2,
CashPrice: 2.379,
CreditPrice: 0,
VolumeCashSale: 0,
VolumeCreditSale: 0,
AmountCashSale: 0,
AmountCreditSale: 0,
GasProductId: 2,
GasProductName: "Premium",
ShiftId: 1
}];
の簡易版は、各在庫の合計を計算し、販売在庫配列に
var AppViewModel = function() {
var self = this;
self.OpeningGasInventories = ko.mapping.fromJS(OpeningGasInventories);
self.ClosingGasInventories = ko.mapping.fromJS(ClosingGasInventories);
self.DeliveredGasInventories = ko.mapping.fromJS(DeliveredGasInventories);
self.SoldGasInventories = ko.mapping.fromJS(SoldGasInventories);
self.GasProductSales = ko.mapping.fromJS(GasProductSales);
self.TotalOpeningGasInventory = ko.computed(function() {
// Now calculate the sum of all Open Gas inventories
var total = 0;
self.OpeningGasInventories()
.forEach(function(item, index) {
total += +item.Volume() || 0;
});
return total.toFixed(0);
});
//Compute total of closing gas inventory
self.TotalClosingGasInventory = ko.computed(function() {
// Now calculate the sum of all Open Gas inventories
var total = 0;
self.ClosingGasInventories()
.forEach(function(item, index) {
total += +item.Volume() || 0;
});
return total.toFixed(0);
});
//Compute total of Delivered gas inventory
self.TotalDeliveredGasInventory = ko.computed(function() {
var total = 0;
self.DeliveredGasInventories()
.forEach(function(item, index) {
total += +item.Volume() || 0;
});
return total.toFixed(0);
});
//Compute total of Sold gas inventory
self.TotalSoldGasInventory = ko.computed(function() {
var total = 0;
self.SoldGasInventories()
.forEach(function(item, index) {
console.info("Volume is " + item.Volume());
total += +item.Volume() || 0;
});
return total.toFixed(0);
});
self.SoldGasInventories = ko.computed(function() {
//we know all the four arrays are in same order and of same length
for (var i = 0; i < self.SoldGasInventories().length; i++) {
self.SoldGasInventories()[i]
.Volume = parseFloat(self.OpeningGasInventories()[i].Volume()) +
parseFloat(self.DeliveredGasInventories()[i].Volume()) -
parseFloat(self.ClosingGasInventories()[i].Volume());
}
return self.SoldGasInventories();
});
};
問題を計算するために私Knokcoutコードされている:私は最後に計算機能をコメントする場合self.SoldGasInventoriesすべての配列の合計が正確に計算されています。この最後の関数はSoldGasInvetories配列を計算すると仮定していますが、期待通りに機能しません。この関数のコメントを外すと、self.TotalSoldGasInventoryは呼び出されません。 私はJSFIDDLEを作成しました。私の問題を確認して助けてください。ありがとうございます。
()'あなたならない*更新*観測可能ではなく、*の値と*それを置き換えます。 '.Volume = ...'の代わりに '.Volume(...)'を試してみてください。 https://jsfiddle.net/8nj31seh/45/(変更後に値が正しく計算されているかどうかは分かりません) – haim770