Send all table content to PDF and Excel

Send all table content to PDF and Excel

mbornmborn Posts: 44Questions: 17Answers: 0

Have a table configured as follows:

        var detailTable = $('#detailTable').DataTable({
            responsive: true,
            paging: false,
            info: false,
            fixedHeader: true,
            orderClasses: false,
            order: [[2, 'asc'], [0, 'asc'], [1, 'asc']],
            buttons: [
                {extend: 'excel', text: '<i class="far fa-file-excel"></i> Excel', title: 'Timecard Inquiry', messageTop: 'Detail Report - Date Range <?=$dateRange?>', footer: true},
                {extend: 'pdf', text: '<i class="far fa-file-pdf"></i> PDF', orientation: 'landscape', title: 'Timecard Inquiry', messageTop: 'Detail Report - Date Range <?=$dateRange?>', footer: true},
                { text: '<i class="fas fa-print"></i> Print',
                    action: function ( e, dt, node, config ) {
                        $("#detailTable").print({prepend : '<b>Timecard Inquiry</b>' + "<br/>" + '<b>Detail Report - Date Range <?=$dateRange?></b>'+ "<br/>"});
                    }
                }

            ],
            rowGroup: {
                startRender: null,
                // This function handles the associate totals
                endRender: function (rows, group) {

                    var assocTotal = rows
                        .data()
                        .pluck(8)
                        .reduce(function (a, b) {
                            return a + b * 1;
                        }, 0);
                    assocTotal = $.fn.dataTable.render.number(',', '.', 2, '').display(assocTotal);

                    var container = $('<tr/>');

                    // This loop determines how many columns are on the main row
                    // (as opposed to child rows) from the left side of the table
                    // to the 9th column, which is the field being totalled.
                    // Note that the responsive.Hidden method is true if the column
                    // is hidden from the responsive element (iow, is visible on the
                    // parent row).
                    visibleFields = 0;
                    var table = $('#detailTable').DataTable();
                    for (let i = 0; i < 9; i++) {
                        if (table.column(i).responsiveHidden()) {
                            visibleFields = visibleFields + 1;
                        }
                    }
                    // We subtract 2 from the number of visible fields to account for
                    // the total literal and the actual total below. The result tells us
                    // how many empty columns need to be inserted to line up the total.
                    visibleFields = visibleFields - 2;
                    for (let i = 0; i < visibleFields; i++){
                        container.append('<td></td>');
                    }
                    container.append('<td><b>Associate Total </b></td>');
                    container.append('<td style="text-align: right;"><b>' + assocTotal + '</b></td>');
                    return $(container)
                },
                dataSrc: 4,
            },
            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;
                };

                var assocTotal = api
                    .column(8, {page: 'current'})
                    .data()
                    .reduce(function (a, b) {
                        return intVal(a) + intVal(b);
                    }, 0);
                assocTotal = $.fn.dataTable.render.number(',', '.', 2, '').display(assocTotal);

                // Update footer by showing the total with the reference of the column index
                $(api.column(7).footer()).html(' Grand Total');
                $(api.column(8).footer()).html(assocTotal);
            },
        });

        detailTable.buttons().container()
            .appendTo('#detailTable_wrapper .col-sm-6:eq(0)');

The print button as configured above allows me to print all of the table, including subtotals built using the rowGroup feature. I'm trying to get exports to PDF and Excel to work in the same way, as the current code prints the table, but no subtotals. How do I accomplish this?

Thanks in advance,
Mike

Answers

This discussion has been closed.