get sum of all columns at tfooter

get sum of all columns at tfooter

ASHFAQAHMEDASHFAQAHMED Posts: 6Questions: 0Answers: 0
edited February 2012 in General
hello all,

i am trying to get the sum of each column, how should i do that ?

i have used a table on more then 12 pages, and defined at first home page which is included at all pages,
here is code with i want to show the sum of each column

$('#example').dataTable(
{

//"bJQueryUI": true,
"sScrollX": "110%",
"bScrollCollapse": true,
"iDisplayLength": 10,
////// add more show per page list "aLengthMenu": [[25, 50, 100, -1], [25, 50, 100, "All"]];
"sDom": 'rt<"bottom"iflp<"clear">>',
"sPaginationType": "full_numbers"
} )

Thanks

Replies

  • allanallan Posts: 22,819Questions: 0Answers: 527
    What you can do is something like:

    $('#example').dataTable()._('td:nth-child(4)')
    That will give you an array of data from the 4th column which you can sum as you would do any other array :-)

    Allan
  • ASHFAQAHMEDASHFAQAHMED Posts: 6Questions: 0Answers: 0
    Thanks :)
  • ASHFAQAHMEDASHFAQAHMED Posts: 6Questions: 0Answers: 0
    Hello ,

    any example for this ? i have tried but can not have the values for the columns

    Thanks
  • allanallan Posts: 22,819Questions: 0Answers: 527
    Here is my code from above running: http://live.datatables.net/odawav/edit . Have a look at the console in your browser and you'll see the data array.

    Allan
  • davcaddodavcaddo Posts: 3Questions: 0Answers: 0
    Good work Allan, Is there any way i can output the sum result in a div. An example will do, thanks
  • garybrett37garybrett37 Posts: 8Questions: 0Answers: 0
    Hi there, did you manage to resolve this, I am looking to do the same, when I looked at Allans example it doesnt appear to show summary in footer?

    Thanks
  • allanallan Posts: 22,819Questions: 0Answers: 527
  • garybrett37garybrett37 Posts: 8Questions: 0Answers: 0
    edited June 2012
    Hi Allan, many thanks for the reply, I did indeed see that example and several others but not being good with js I am still struggling. Using your excellent example I did manage to sum a single column but cannot fathom out how to sum multiple from dynamic table. I edited your code to simply sum a column [1] as such;

    <script type="text/javascript" charset="utf-8"> $(document).ready(function() { $('#example').dataTable( { "fnFooterCallback": function ( nRow, aaData, iStart, iEnd, aiDisplay ) { var iTotalCases = 0; for ( var i=0 ; i<aaData.length ; i++ ) { iTotalCases += aaData[i][1]*1; } /* Calculate the market share for browsers on this page */ var iPageCases = 0; for ( var i=iStart ; i<iEnd ; i++ ) { iPageCases += aaData[ aiDisplay[i] ][2]*1; } /* Modify the footer row to match what we want */ var nCells = nRow.getElementsByTagName('th'); nCells[1].innerHTML = parseInt(iPageCases * 100)/100 + ''; } } ); } ); </script>
    Also tried this but doesn't output the summary;
    <script type="text/javascript" charset="utf-8"> $(document).ready(function() { $('#example').dataTable(); console.log( $('#example').dataTable()._('td:nth-child(2)') ) } ); </script>
    Kind Regards

    Gary
  • garybrett37garybrett37 Posts: 8Questions: 0Answers: 0
    Does anyone know how much the paid support is?

    Thanks
This discussion has been closed.