Soma using Meteorjs

Asked

Viewed 38 times

1

I’m developing a Tasklist to understand how MeteorJS works.
In the application the user can enter the task name, how many hours he will spend on it and when he will perform it.
The system returns a list of all the user tasks and the sum of the tasks' hours, here is the problem, how to sum up the tasks' hours using the 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 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>

1 answer

0


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

Creating a new property on helpers of the template list you can return the total hours, see in the example below how 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 for calculating hours.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.