Total or Sum

Aug 25, 2011 at 2:42 PM
Edited Aug 25, 2011 at 2:45 PM

Is there a way of building a simple table of data from the getlistitems, that in a footer row or something similar I could add or total the visible items in a specific column? How could I achive this? I do not need the table, I just need to understand how to add a column. In my example I want to total Hours_worked

[code]

$(document).ready(function(){

$("#TimeKeeping").html("");


$().SPServices({
    operation: "GetListItems",
    webURL: "/sites/CustomerPortal/admin",
    async: false,
    listName: "Timekeeping",
 CAMLQuery: "<Query><Where><Eq><FieldRef Name='Company' /><Value Type='Text'>Interstate Paper</Value></Eq></Where><OrderBy><FieldRef Name='Created' Ascending='False' /></OrderBy></Query>",
    CAMLViewFields: "<ViewFields><FieldRef Name='Month' /><FieldRef Name='Hrs_Worked' /><FieldRef Name='Emp' /><FieldRef Name='Emp 2' /><FieldRef Name='Work_Type' /><FieldRef Name='Company' /><FieldRef Name='Issue_x0020_No_x002e_' /></ViewFields>",
     completefunc: function (xData, Status) {


   var strHtml = "<table class='timekeeping'>";
   strHtml += "<tr class='timekeeping-header'><td>Month</td><td>Hrs Worked</td><td>Emp</td><td>Work Date</td><td>Type</td><td>Company</td><td>Issue Number</td></tr>";
 
      $(xData.responseXML).find("[nodeName='z:row']").each(function(i) {
   var month = $(this).attr("ows_Month");
   var hrs = parseFloat($(this).attr("ows_Hrs_Worked"));
   var hours = hrs.toFixed(2);
   var emp = $(this).attr("ows_Emp");
   //var emp2 = $(this).attr("ows_Emp_x0020_2");
   var workday = $(this).attr("ows_WorkDate");
   var worktype = $(this).attr("ows_Work_Type");
   var comp = $(this).attr("ows_Company");
   var issues = $(this).attr("ows_Issue_x0020_No_x002e_");
   
   
   
   strHtml += "<tr class='timekeeping-item'>"; 
   strHtml += "<td class='month'>";
   if(month != undefined)
   {
    var tempm = month.split('#');
    strHtml += "<div>" + tempm[1] + "</div>";   
   }
   strHtml += "</td>";
  
   strHtml += "<td class='hours'>" + hours + "</td>";
   strHtml += "<td class='emp'>";
   if(emp != undefined)
    {
    strHtml += "<div>" + emp + "</div>";
    }
   strHtml +="</td>";
   //strHtml += "<td class='emp'>";
   //if(emp2 != undefined)
   // {
   // strHtml += "<div>" + emp2 + "</div>";
    //}
   //strHtml +="</td>";
   strHtml += "<td class='workday'>";
   if(workday != undefined)
   {
    var tempw = workday.split(' ');
    strHtml += "<div>" + tempw[0] + "</div>";   
   }
   
   strHtml += "</td>";
   strHtml += "<td class='worktype'>" + worktype + "</td>";
   strHtml += "<td class='comp'>" + comp + "</td>";
   strHtml += "<td class='issues'>";
   if(issues != undefined)
   {
   strHtml += "<div>" + issues + "</div>";
   }
   strHtml += "</td>";
   strHtml += "</tr>";


   
      });
     strHtml += "<tr class='timekeeping-footer'>";
  strHtml += "<td></td>";
  strHtml += "<td></td>";
  strHtml += "<td></td>";
  strHtml += "<td></td>";
  strHtml += "<td></td>";
  strHtml += "<td></td>";
  strHtml += "<td></td></tr>";
 
      strHtml += "</table>";
    
      $("#TimeKeeping").append(strHtml);
 
    }
  });
 
  $('tr.timekeeping-item:even').addClass('even');
  $('tr.timekeeping-item:odd').addClass('odd');
 

});

[/code]

Coordinator
Aug 25, 2011 at 2:48 PM

Yes, you can do the math in your script and put the results into the DOM, just like with anything. There are also some jQuery plugins for datatable-like constructs, but I've never used them, finding that rolling my own code is usually more straightforward.

M.

Aug 25, 2011 at 8:07 PM

Perfect, I was able to manipulate a count and a sum. my fault was missing my "()". thanks. I have another question will post new. it is date related

Apr 18, 2012 at 3:09 PM
Edited Apr 18, 2012 at 3:09 PM
jaxkookie wrote:

Perfect, I was able to manipulate a count and a sum. my fault was missing my "()". thanks. I have another question will post new. it is date related

 

How were you able to do this? I don't see any count or sum in your code above?