Recalculate DataTable Footer after Printing

Recalculate DataTable Footer after Printing

ivasijaivasija Posts: 2Questions: 2Answers: 0

Hi everyone, I have used Datatables to create a paginated Table and show sum of the current page as the footer, it is working as expected, on every page it will recalculate the sum, but the problem is when I print using PDF or Excel, the sum is same as per page, but the rows are the complete match of the search criteria. Can any of you please help me? if needed here's the jquery code of the table:
```<script>
$( document ).ready(function() {

    var table = $('#table').DataTable({
        'initComplete': function (settings, json){
            this.api().columns('6,8,7', {page:'current'}).every(function(){
                var column = this;
                var sum = column
                .data()
                .reduce(function (a, b) {
                    a = parseFloat(a, 10);
                    if(isNaN(a)){ a = 0; }                  

                    b = parseFloat(b, 10);
                    if(isNaN(b)){ b = 0; }

                    return (a + b).toFixed(2);
                });
                if(!sum.includes('€'))
                    sum +=' &euro;';
                $(column.footer()).html(sum);
            });
        },
        footerCallback: function () {

            this.api().columns('6,8,7', {page:'current'}).every(function(){
                var column = this;
                var sum = column
                .data()
                .reduce(function (a, b) {
                    a = parseFloat(a, 10);
                    if(isNaN(a)){ a = 0; }                  

                    b = parseFloat(b, 10);
                    if(isNaN(b)){ b = 0; }

                    return (a + b).toFixed(2);
                });
                if(!sum.includes('€'))
                    sum +=' &euro;';
                $(column.footer()).html(sum);
            });
        },
        dom: 'Bfrtip',
        buttons: [
            {
                    extend: 'copyHtml5',
                    exportOptions: {
                        columns: [ 0, 1, 2, 3, 4, 5, 6, 7, 8 ]
                    },
                    footer: true
            },
            {
                extend: 'excel',
                exportOptions: {
                    columns: [ 0, 1, 2, 3, 4, 5, 6, 7, 8 ]
                },
                footer: true
            },
            {
                extend: 'pdfHtml5',
                exportOptions: {
                    columns: [ 0, 1, 2, 3, 4, 5, 6, 7, 8 ]
                },
                footer: true
            }
        ]
    });
});

</script>```

Answers

  • jvretamerojvretamero Posts: 26Questions: 0Answers: 3

    It seems that the footer option of the pdf button uses only one footer.

    Checking the source, the messageBotton option could be useful (see here) because it accepts a string or a function, then you can recalculate the values of the whole table and display it.

This discussion has been closed.