Using footercallback function to find total for more than 1 column.

Using footercallback function to find total for more than 1 column.

armegaddonarmegaddon Posts: 3Questions: 1Answers: 0

I am using footercallback function to find the total of certain columns, but it seems to be calculating for only one column.
If any one here know the workaround please help me.

Answers

  • WebCodexWebCodex Posts: 71Questions: 13Answers: 3

    The total number of columns? Or the Total of some value inside the columns?

    Maybe some code for us to ponder?

  • armegaddonarmegaddon Posts: 3Questions: 1Answers: 0

    Please look at this example

    http://live.datatables.net/cuhukumu/2/edit

    you can see the sum is calculated for multiple columns.
    this is done using the sum() function.
    It seems to be not working for me.
    Is there any way to do it with footercallback() or sum()?

  • armegaddonarmegaddon Posts: 3Questions: 1Answers: 0
    $("#cashSheet").dataTable({
        "footerCallback": function(row, data, start, end, display){
            var api = this.api(), data;
            var intval = function(i){
                return typeof i === 'string' ?
                i.replace(/[\$,]/g, '')*1:
                typeof i === 'number' ?
                i : 0;
            };
            total = api
                .column( 3 )
                .data()
                .reduce(function(a, b){
                    return intval(a) + intval(b);
                }, 0 );
            pageTotal = api
                .column( 3, {page: 'current'} )
                .data()
                .reduce(function(a, b){
                    return intval(a) + intval(b);
                }, 0 );
            $(api.column( 3 ).footer() ).html(
                ''+pageTotal +' ( '+ total + ' total)'
            );
            total = api
                .column( 4 )
                .data()
                .reduce(function(a, b){
                    return intval(a) + intval(b);
                }, 0 );
            pageTotal = api
                .column( 4, {page: 'current'} )
                .data()
                .reduce(function(a, b){
                    return intval(a) + intval(b);
                }, 0 );
            $(api.column( 4 ).footer() ).html(
                ''+pageTotal +' ( '+ total + ' total)'
            );
        }
    });
    

    The above piece of code worked. This can sum up the 3rd column and 4th column simultaneously, apart from that you can add any number of columns to that to show the sum.

    [closed]

This discussion has been closed.