我正在使用淘汰赛来做一些我认为很简单的事情。我是Knockout和JavaScript的新手,因此遇到了麻烦。任何帮助将不胜感激。以下是手头的问题。
我有三个阵列形式的产品库存(开仓,关闭,交付),我想以阵列形式计算已售产品的库存。实际数据有点复杂。这是我的数据的简化版本
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
}];
以下是我的Knokcout代码,用于计算每个库存的总计并计算已售出的库存数组
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();
});
};
问题:如果我评论最后一个计算函数self.SoldGasInventories,那么所有数组的总数都被很好地计算了。最后一个函数假设是计算了SoldGasInvetories数组,但是它没有按我预期的那样工作。一旦取消注释此函数,就不会调用self.TotalSoldGasInventory。我创建了一个JSFIDDLE,请检查并帮助我解决问题。太感谢了..
您上一次计算的结果不会返回计算值,而是会更新其他可观察值。
self.UpdateSoldGasInventoriesVolumes = 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())
);
}
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句