2

I am looking to add a summation row to my rails application that is using Datatables.

My data is being presented with a comma as is 999,999

I noticed that this code:

  $('#example').dataTable( {
      "fnFooterCallback": function ( nRow, aaData, iStart, iEnd, aiDisplay ) {

       var TotalMarks = 0;
      for ( var i=0 ; i<aaData.length ; i++ )
   {
    TotalMarks += aaData[i][12]*1;
   }

   var nCells = nRow.getElementsByTagName('th');
   nCells[1].innerHTML = TotalMarks;
   }
  }); 

will only add up if I remove the (,). Is there a way to have it added with the (,) still in place?

RubyNewbie
  • 547
  • 5
  • 21

1 Answers1

2

The content of aaData is of type string. So you must use some conversion :

var dataTable = $('#example').dataTable({
  fnFooterCallback: function ( nRow, aaData, iStart, iEnd, aiDisplay ) {
      var TotalMarks = 0;
      for ( var i=0 ; i<aaData.length ; i++ ) {
        console.log(TotalMarks, aaData[i][3]);
        TotalMarks = TotalMarks + parseFloat(aaData[i][3].replace(',','.'));
       }
   }
});

console-log for demonstration purposes, see fiddle (with a lot of 999,999 and the like in column #3) -> http://jsfiddle.net/uqN2L/

davidkonrad
  • 83,997
  • 17
  • 205
  • 265
  • Thank you for your response. I used this example but it added the number of records for that column instead of taking the values inside and adding them all up for a total – RubyNewbie Dec 16 '13 at 23:10
  • Have you seen the TotalMarks adding in the console in the fiddle? You have to change aaData[i][3] to aaData[i][12] – davidkonrad Dec 16 '13 at 23:16
  • Yes. I have updated my code to reflect [12] instead of [3]. Unfortunately, from the JS Fiddle I do not where [3] has a total value of all the entries. I just see some 999,999 entries. I checked the footer but did not see a specific summation row for that column. – RubyNewbie Dec 16 '13 at 23:22
  • My issue is when the 999,999 entries exist, the summation row give me an "NaN" value – RubyNewbie Dec 16 '13 at 23:25
  • Updated the answer and the fiddle!! To answer your comment, this is because i log `console.log(TotalMarks, aaData[i][3]);` - havent cared about displaying the value, that should be a nobrainer. – davidkonrad Dec 16 '13 at 23:27
  • Curious..why are you calling a replacement of (,) to a (.)? I need it to display the (,). So if I was summing up column [3] and it had three rows of values each being, lets say 10,000 for example, the summation would be 30,000. Also is there a way to advise it just to place a comma for every three places? I really appreciate your help on this – RubyNewbie Dec 16 '13 at 23:44
  • It is to make parseFloat correct, it need dots. You can always set TotalMarks back to comma notation by `TotalMarks = TotalMarks.replace('.',',');` ParseFloat can parse a lot -> https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseFloat but needs dot notation. – davidkonrad Dec 16 '13 at 23:49
  • 1
    Thank you for the education on the issue. +1 – RubyNewbie Dec 17 '13 at 00:01