Total of data column

Total of data column

Francois62Francois62 Posts: 6Questions: 1Answers: 0

Hi,

I wonder if it's possible to calculate total of data column while filtering this.
Till now, I can calculate total but without filtering.
I want to have total of lines displayed.

Thank you in advance,
Regards,
François.

Answers

  • Francois62Francois62 Posts: 6Questions: 1Answers: 0

    NB : I use the 1.9.4 version!

  • allanallan Posts: 54,182Questions: 1Answers: 8,456 Site admin
    edited May 2014

    In 1.10 its a simple as:

    var sum = table.column( 4 ).data().eq(0).reduce( function ( a, b ) {
      return a + b;
    } );
    

    In 1.9 take a look at this example: http://legacy.datatables.net/release-datatables/examples/advanced_init/footer_callback.html

    Allan

  • Francois62Francois62 Posts: 6Questions: 1Answers: 0
    edited May 2014

    I found another solution to resolve my problem. I put it to help another person who have the same problem :-)

    $(document).ready(function() {
        var oTable = $('#bordereau').dataTable( {
            fnFooterCallback: function ( nRow, aaData, iStart, iEnd, aiDisplay ) {
                var TotalMarks = 0;
                for ( var i=iStart ; i<iEnd ; i++ ) {
                    console.log(TotalMarks, aaData[ aiDisplay[i] ][4].replace(',','.'));
                    TotalMarks += parseFloat(aaData[ aiDisplay[i] ][4].replace(',','.'));
                }
                var nCells = nRow.getElementsByTagName('th');
                nCells[3].innerHTML = "Total = "+TotalMarks+" &euro;";
            }
        } );
    } );
    

    Thank you for your help!

    Regards,

    François

This discussion has been closed.