Sum of columns in Meteor client side

182 views Asked by At

Goal: A row called Total with sum of each column, so total of Course1 grades, total of Course2 grades etc. I created a Session and am handling this totalling on the Client side so it'll start fresh on browser refresh.

Storing this kind of data -> {Student1, Course1, Course2, Course 3, Course 4, Course 5}, {Student2, Course1, Course2, Course 3, Course 4, Course 5} and so on.

The only approach that comes to mind is many variables like {{sum_course1}} in the html and calculate them in Template helpers, but that seems repetitive! What's a good solution? I came across an aggr function for server side but nothing promising for the client side.

This is what I have working for sum, but I don't have repetitive functions like this for the 5 different courses.

    Template.body.helpers({
      sum:function(){
       var sum=0;
       var cursor=Tasks.find({});
       cursor.forEach(function(Tasks){
         sum = Tasks.courseone + sum;
       });
       return sum;
     }
    });
1

There are 1 answers

0
user1678031 On

I was finally able to achieve this aggregate on the client side using a package called Mongo Sum (https://atmospherejs.com/peppelg/mongo-sum). Super easy, and did exactly what I was looking for :)

meteor add peppelg:mongo-sum

Template.body.helpers({
  sum:function(){
    var cursor=Tasks.find({});
    return cursor.sum('courseone');
   }
});

Posting this so it helps anyone looking for something similar!