Knockout을 사용하여 간단하다고 생각되는 작업을하고 있습니다. 나는 넉 아웃과 자바 스크립트에 익숙하지 않아 붙어있다. 어떤 도움이라도 대단히 감사하겠습니다. 다음은 손에서 문제입니다.Observable Arrays의 Computed Observable Array
배열 (개봉, 마감, 배달)의 재고가 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을 만들었습니다. 제 문제를 확인하고 해결해주십시오. 고마워요.
왜냐하면'SoldGasInventories()'에서는 관찰 가능하지만 업데이트 대신 * 값으로 바꾸기 때문입니다. '.Volume = ...'대신'.Volume (...)'을 시도하십시오. https://jsfiddle.net/8nj31seh/45/ (변경 후 값을 제대로 계산하지 못했는지 확인하십시오) – haim770