Knockout.js 从可观测阵列计算可观测阵列
我用击倒来做一些我认为很简单的事情。我不熟悉敲除和JavaScript,因此被卡住了。任何帮助都将不胜感激。下面是手头的问题 我有三个数组形式的产品库存(期初、期末、交付),我想以数组形式计算已售出的产品库存。实际数据有点复杂。这是我的数据的简化版本Knockout.js 从可观测阵列计算可观测阵列,knockout.js,Knockout.js,我用击倒来做一些我认为很简单的事情。我不熟悉敲除和JavaScript,因此被卡住了。任何帮助都将不胜感激。下面是手头的问题 我有三个数组形式的产品库存(期初、期末、交付),我想以数组形式计算已售出的产品库存。实际数据有点复杂。这是我的数据的简化版本 var OpeningGasInventories = [{ Id: 0, Volume: 0, TimeStamp: "0001-01-01T05:00:00Z", GasInventoryType: "Opening", G
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();
});
};
var-AppViewModel=function(){
var self=这个;
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(函数(){
//现在计算所有未动用天然气库存的总和
var合计=0;
self.openinggas目录()
.forEach(功能(项目、索引){
总计+=+项.体积()| | 0;
});
返回总计。toFixed(0);
});
//计算期末天然气库存总量
self.TotalClosingGasInventory=ko.computed(函数(){
//现在计算所有未动用天然气库存的总和
var合计=0;
self.closingGasInventures()
.forEach(功能(项目、索引){
总计+=+项.体积()| | 0;
});
返回总计。toFixed(0);
});
//计算已交付天然气库存总量
self.TotalDeliveredGasInventory=ko.computed(函数(){
var合计=0;
自我交付的投资()
.forEach(功能(项目、索引){
总计+=+项.体积()| | 0;
});
返回总计。toFixed(0);
});
//计算已售天然气库存总量
self.TotalSoldGasInventory=ko.computed(函数(){
var合计=0;
self.SoldGasInventories()
.forEach(功能(项目、索引){
console.info(“Volume is”+item.Volume());
总计+=+项.体积()| | 0;
});
返回总计。toFixed(0);
});
self.SoldGasInventories=ko.computed(函数(){
//我们知道这四个数组的顺序和长度都相同
对于(var i=0;i
问题:如果我对最后一个计算函数self.SoldGasInventories进行注释,则所有数组总数都可以正常计算。最后一个函数假定计算了SoldGasInventories数组,但它的工作方式与我预期的不同。一旦取消对该函数的注释,则不会调用self.TotalSoldGasInventory。
我已经创建了一个请检查并帮助我解决我的问题。非常感谢。上次计算不会返回计算值,而是更新其他观察值
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())
);
}
});
self.UpdateSoldGasInventoriesVolumes=ko.computed(函数(){
//我们知道这四个数组的顺序和长度都相同
对于(var i=0;i
这是因为在SoldGasInventories()
中,您没有更新可观察对象,而是将其替换为值。尝试.Volume(…)
而不是.Volume=…
。请参阅@haim770(但不确定更改后是否正确计算了值)@haim770非常感谢。这就解决了问题。也非常感谢你。我无法在之前的评论中输入您的姓名。我添加了一个附加功能,但该功能无法按我所希望的方式工作。我想以表格形式显示可供销售的StoreProducts,并允许用户为每个项目的销售数量提供输入。它应该更新“TotalAmount”(价格*售出单位),并提供所有售出产品的总金额。这是。非常感谢。用于设置其他值的ko.computed
不应返回新值。您不应该替换SoldGasInventories
,而应该像上面那样使用另一个名称。(实际上你甚至不需要给它起名字。)但是解决方法是使用值
绑定
。非常感谢你的解释。我得到了它。一个问题。在let's stay store部分的当前功能中。当我在输入框中输入一个值并从该输入框中离开焦点时,即计算函数运行并计算值时。如果我想让它在我输入一个值后立即运行(而不离开输入框),我需要做什么。我希望我已经解释过了。非常感谢,伙计。