Underscore template, adding a list into a single array?

84 Views Asked by At

I have a template, which works fine but I am trying to build an array with the final goal of getting a total of those values.

So my template gives me the following list of values,

    10
    23
    14
    1

ect, for each value that is in my database, which loops around fine. But I want a total from this list, which I have done in the row but this goes over each row in the database. So what my aim was (and please tell me if there is a better method of doing this) was to build this into a single array by pushing each value into a new array within the template, then adding the array up, giving me a grand total.

So my template looks like this,

<script type="text/template" id="TimesheetData">
  <input type="type" name="data[Timesheetrow][<%= Timesheetrow.index %>][jobtitle]" value="<%= Timesheetrow.jobtitle  %>">        
  <input type="type" name="data[Timesheetrow][<%= Timesheetrow.index %>][jobtitle]" value="<%= Timesheetrow.mon  %>">        
  <input type="type" name="data[Timesheetrow][<%= Timesheetrow.index %>][jobtitle]" value="<%= Timesheetrow.tue  %>">        
  <input type="type" name="data[Timesheetrow][<%= Timesheetrow.index %>][jobtitle]" value="<%= Timesheetrow.wed  %>">        
  <input type="type" name="data[Timesheetrow][<%= Timesheetrow.index %>][jobtitle]" value="<%= Timesheetrow.thu  %>">        
  <input type="type" name="data[Timesheetrow][<%= Timesheetrow.index %>][jobtitle]" value="<%= Timesheetrow.fri  %>">        
  <input type="type" name="data[Timesheetrow][<%= Timesheetrow.index %>][jobtitle]" value="<%= Timesheetrow.sat  %>">        
  <input type="type" name="data[Timesheetrow][<%= Timesheetrow.index %>][jobtitle]" value="<%= Timesheetrow.sun  %>">

  <span>Weekly total : <span class="TimeSheetWeekly<%= Timesheetrow.index %>"></span></span>

<%
    var WeeklyArray = [Timesheetrow.mon, Timesheetrow.tue, Timesheetrow.wed, Timesheetrow.thu, Timesheetrow.fri, Timesheetrow.sat, Timesheetrow.sun];
    var WeeklyTotals = 0;

    for (var i = 0; i < WeeklyArray.length; i++) {
        WeeklyTotals += WeeklyArray[i] << 0;
    }

    _.defer(function(){ 
        var WeeklyTotalTag = '.TimeSheetWeekly'+ Timesheetrow.index;
        $( WeeklyTotalTag ).html(WeeklyTotals); 
    }) 


%>

</script> 

But when I try to use .zip or .map I can not seem to build the array. It just seems to put each value into its own array and not a total array?

So what am I doing wrong?

Thanks

  • Please let me know if I have not explained myself clearly, I will edit my post.
1

There are 1 best solutions below

1
On

Here is a working jsfiddle

I used reduce to calculate sum of array.

<%= _.reduce(_.values(_.pick(Timesheetrow, 'mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun')), function(a, b) {return a + b}, 0) %>

Full template:

<script type="text/template" id="TimesheetData">
  <input type="type" name="data[Timesheetrow][<%= Timesheetrow.index %>][jobtitle]" value="<%= Timesheetrow.jobtitle  %>">        
  <input type="type" name="data[Timesheetrow][<%= Timesheetrow.index %>][jobtitle]" value="<%= Timesheetrow.mon  %>">        
  <input type="type" name="data[Timesheetrow][<%= Timesheetrow.index %>][jobtitle]" value="<%= Timesheetrow.tue  %>">        
  <input type="type" name="data[Timesheetrow][<%= Timesheetrow.index %>][jobtitle]" value="<%= Timesheetrow.wed  %>">        
  <input type="type" name="data[Timesheetrow][<%= Timesheetrow.index %>][jobtitle]" value="<%= Timesheetrow.thu  %>">        
  <input type="type" name="data[Timesheetrow][<%= Timesheetrow.index %>][jobtitle]" value="<%= Timesheetrow.fri  %>">        
  <input type="type" name="data[Timesheetrow][<%= Timesheetrow.index %>][jobtitle]" value="<%= Timesheetrow.sat  %>">        
  <input type="type" name="data[Timesheetrow][<%= Timesheetrow.index %>][jobtitle]" value="<%= Timesheetrow.sun  %>">

  <span>Weekly total : <span class="TimeSheetWeekly<%= Timesheetrow.index %>">
    <%= _.reduce(_.values(_.pick(Timesheetrow, 'mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun')), function(a, b) {return a + b}, 0) %>
  </span></span>

</script>