簡體   English   中英

如何獲取數據源中屬性的總和?

[英]How can I get the sum of the properties in the datasource?

我在C#中有一個模型項,如下所示:

public class ProjectDashBoardModel
   {      
      public decimal Budget { get; set; }
      public decimal Amount { get; set; }
      public decimal Balance { get; set; }
      public decimal Tax { get; set; }
      public decimal Money1 { get; set; }
      public decimal Money2 { get; set; }
      public decimal Balance_2 { get; set; }
      public decimal Balance_3 { get; set; }     
   }

   public class ProjectDashboard
   {
      public Guid Id { get; set; }
      public string ProjectLogo { get; set; }
      public string LastMutatedBy { get; set; }
      public DateTime LastMutatedDate { get; set; }
      public List<ProjectDashBoardModel> ProjectModelList { get; set; }


   }

這是我的數據源代碼:

   vm.dataTotal = data.projectModelList;   

我想擁有所有屬性的總和

我已經嘗試過類似的方法,但是沒有成功

 vm.dataTotal = data.projectModelList(function () {
          {field: "budget", aggregate: "sum"}
        });

我已經在劍道網格中有此列表。 現在,我希望將網格中的預算總和添加到新的數據源中,以便將其與kendo條形圖綁定。

如何將所有屬性的總和放入vm.dataTotal?

親切的問候

這是有關如何將Sum函數添加到數組的另一篇SO文章 您可以這樣使用:

data.projectModelList.sum = function (prop) {
    var total = 0;
    for ( var i = 0, _len = this.length; i < _len; i++ ) {
        total += this[i][prop];
    }
    return total;
}

vm.dataTotal = data.projectModelList.sum("budget");

如果要在JS獲取數組的總和,則最好使用Array.prototype.reduce :您可以在任意位置使用結果( total變量),例如'vm.set('total',total) will bind it to your observable虛擬機對象。

 var data = { projectModelList: [ { "budget": 245, "amount": 500 , "balance": 2323, "tax": 234, "money1": 23, "money2": 6345, "balance_2": 123123, "balance_3": 3423}, { "budget": 245, "amount": 500 , "balance": 2323, "tax": 234, "money1": 23, "money2": 6345, "balance_2": 123123, "balance_3": 3423}, { "budget": 245, "amount": 500 , "balance": 2323, "tax": 234, "money1": 23, "money2": 6345, "balance_2": 123123, "balance_3": 3423}, { "budget": 245, "amount": 500 , "balance": 2323, "tax": 234, "money1": 23, "money2": 6345, "balance_2": 123123, "balance_3": 3423}, ] }; var total = data.projectModelList.reduce(function(t, d) { Object.keys(t).forEach(function(k) { t[k] += (d[k] || 0); }); return t; }) console.log(total) 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM