Search code examples
datatables

Data table footer is not included in DataTable print export option


I am using a Codeigniter and ajax request with DataTable to fetch data and trying to print the data using DataTable print export option. But footer is not appearing in the print.

How can i include the footer in printing with DataTable?

Here is my code:

function data_table_report(dateselected){
    $("#dataTables-report").dataTable().fnDestroy();
    table =  $('#dataTables-report').DataTable({
        "ajax": {
        "url": "<?php echo site_url('patients_report/dataTable_report/')?>"+dateselected,
        "type": "POST",
        },
        responsive: true,
        bInfo: false,
        dom: 'Bfrtip',
        buttons: [{ extend: 'print',
            exportOptions: {
                columns: ':visible'
                 }
        },
        'colvis'],
        columnDefs: [ { 
            targets: -1,
            visible: false} 
       ]
   });
}

Solution

  • Try this:

    "fnInitComplete": function (oSettings, json) {          
        myfooter = this.find('tfoot')[0].innerHTML;
        new $.fn.dataTable.Buttons( this, {
        buttons: [          
             {
                extend: 'print',
                exportOptions : {
                    columns: ':not(.notForPrint)'
                },
                customize: function ( win ) {
                    $(win.document.body)
                        .css( 'font-size','10pt') 
                        .css( 'font-family','arial'); 
                    $(win.document.body).find( 'table' )
                        .addClass( 'compact' )
                        .css( 'font-size', 'inherit' );
                    $(win.document.body).find('tfoot')[0].innerHTML = myfooter ;
                    win.print();
                    setTimeout(win.close(),500);
                }
            }
        ]
        });
        this.DataTable().buttons().container().insertBefore( '#example_filter');  
    

    },