0

I got a modelitem in C# like this:

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; }


   }

And this is my code of my datasource:

   vm.dataTotal = data.projectModelList;   

I want to have all the properties the sum of it

I already tried something like this but no succes

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

I already have this list in a kendo grid. Now I want the sum of the budget in the grid into a new datasource so I can bind it with a kendo bar chart.

How can I get all the sum of the properties into vm.dataTotal?

Kind regards

Fearcoder
  • 753
  • 3
  • 15
  • 47
  • Possible duplicate of [Better way to sum a property value in an array (Using Angularjs)](http://stackoverflow.com/questions/23247859/better-way-to-sum-a-property-value-in-an-array-using-angularjs) – Sujeet Sinha Jul 18 '16 at 14:35

2 Answers2

0

Here is another SO post for how to add a Sum function to an array. You would use this like:

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");
Community
  • 1
  • 1
leetibbett
  • 843
  • 4
  • 16
  • Thank you for your time! I get an error "data.projectModelList.sum is not a function" if I use your method. – Fearcoder Jul 18 '16 at 14:39
0

If you are about to get sum of an array in JS you'd better use Array.prototype.reduce: You can use the result (total variable) anywhere for example 'vm.set('total', total)will bind it to your observablevm`object.

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)
Morteza Tourani
  • 3,506
  • 5
  • 41
  • 48
  • Thank you for your time! The budget property is not hard coded. I get this value from a database. I already have this list in a kendo grid. Now I want the sum of the budget displaying in a bar chart – Fearcoder Jul 18 '16 at 14:47
  • I don't get what you'r exactly about. can you add some samples of your data. BTW as I know kendo chart handles aggregates very well. – Morteza Tourani Jul 18 '16 at 14:50
  • { "budget": 245, "amount": 500 , "balance": 2323, "tax": 234, "money1": 23, "money2": 6345, "balance_2": 123123, "balance_3": 3423} – Fearcoder Jul 18 '16 at 14:57
  • This is a json object but I got more of these. I want the sum of all these values in a new datasource. – Fearcoder Jul 18 '16 at 14:58
  • @Fearhunter You have an array of items like this and you want sum of every item. Am I right? – Morteza Tourani Jul 18 '16 at 15:03
  • Yes sir and that into a new datasource so I can bind it to my kendo graph. I look it up on google to get the sum value of the grid into a bar chart. But kendo doesn't support that (yet) – Fearcoder Jul 18 '16 at 15:04
  • Update you question with this information. I'll update my answer. – Morteza Tourani Jul 18 '16 at 15:06