Sum using MeteorJS

1

I'm developing a TaskList to understand how MeteorJS works.
In the application the user can enter the name of the task, how many hours it will spend in it and when it will perform it.
The system returns a list with all the tasks of the user and the sum of the hours of the tasks, ie the problem, how to add the hours of the tasks using MeteorJS ?

Code to enter the task.

Template.new.events({
  "submit form" : function(e, template) {
    e.preventDefault();

    var InputName = $("#taskName");
    var taskName = InputName.val();

    var InputTime = $("#taskTime");
    var taskTime = InputTime.val();

    var InputDate = $("#taskDate");
    var taskDate = InputDate.val();

    var item = {name: taskName, time: taskTime , date: taskDate };
    Task.insert(item);

    InputName.val();

  }
});

Code to list the tasks

Template.list.helpers({
  tasklist: function() {
    return Task.find({});
  }
});

View Code

{{#each tasklist}}
  <tr>
    <td id="time">
      {{time}}
    </td>
    <td>
      {{name}}
    </td>
    <td>
      {{date}}
    </td>
    <td>
      <button class="btn remove-btn  ">Remove</button>
    </td>
  </tr>
{{/each}}

<p class="text-center total " id="resultado">
  Total Hours:
</p>
    
asked by anonymous 28.03.2016 / 16:08

1 answer

0

If the time format is Hora:Minuto . Example : 02:00 (Duas Horas) .

Creating a new property in helpers of template list you can return total hours, see the example below as it would look in your code.

Helpers:

Template.list.helpers({
  tasklist: function() {
    return Task.find({});
  },
  // Propriedade que retorna o total de horas.
  totalHoras: function() {
    var hora = 0;
    var min = 0;

    Task.find().map(function(doc) {
      var horaMinuto = doc.data.split(':'); // Aqui suponho que sua data seja, por exemplo, '02:00' (Duas Horas).
      hora += parseInt(horaMinuto[0]);
      min += parseInt(horaMinuto[1]);
      if(min >= 60) {
        min -= 60;
        hora += 1;
      }
    });

    return ('00' + hora).slice(-2) + ':' + ('00' + min).slice(-2);
  }
});

Template:

<template name="list">
  <p class="text-center total " id="resultado">
    Total Hours: {{totalHoras}}
  </p>
</template>

Source: Logic to calculate hours .

    
29.03.2016 / 15:53