2016-11-02 21 views
1

私は単純だと思ったことを行うためにノックアウトを使用しています。私はノックアウトと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を作成しました。私の問題を確認して助けてください。ありがとうございます。

+2

()'あなたならない*更新*観測可能ではなく、*の値と*それを置き換えます。 '.Volume = ...'の代わりに '.Volume(...)'を試してみてください。 https://jsfiddle.net/8nj31seh/45/(変更後に値が正しく計算されているかどうかは分かりません) – haim770

答えて

1

最後に計算された値は計算された値を返しませんが、他の観測値は更新されます。なぜなら `SoldGasInventoriesにあります

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()) 
     ); 
    } 
}); 
+0

@ haim770ありがとうございます。それが問題を解決しました。ありがとう、百万円 –

+0

あなたもありがとう。私は前のコメントにあなたの名前を入れることができませんでした。 –

+0

私が望むように機能しない機能を追加しました。私は、販売可能なStoreProductsを表形式で表示し、ユーザーが各アイテムの販売台数を入力できるようにしたいと考えています。 "TotalAmount"(価格*単位の販売)を更新し、販売されたすべての製品の合計数も提供する必要があります。ここには[JSFIDDLE](https://jsfiddle.net/8nj31seh/54/)があります。どうもありがとう。 –

関連する問題