简体   繁体   中英

How can I add a subtotal and a grandtotal to my table using DataTables “jQuery Plug-in?”

I am trying to use Datatables plug-in to display data for a report.

I like to add 2 footer rows to my table, the first one for a subtotal "totals from the current page" and another row for Granttotal "Totals from all the pages"

I followed this example in the docs to attempt to add the rows, but for some reason the footer is not showing up.

I populate the datatable on the fly using Ajax Request after the DataTable is initilized. Below is my current code.

Here is my Html markup

<table id="reportTable" class="table table-striped " cellspacing="0" style="width: 100%;">
    <thead></thead>
    <tbody></tbody>
    <tfoot></tfoot>
</table>

Here is my JavaScript code

$(function(e) {

    $(window).load(function (e) {

        $('#reportTable').DataTable({
            pageLength: 50,
            lengthMenu: [10, 25, 50, 75, 100, 250, 500, 1000],
            order: [   [ 2, 'desc' ]  ],
            columns: [
                { data: 'chain_name', title: 'Chain Name', width: '20%'},
                { data: 'store_id' , title: 'Store Number' },
                { data: 'completed', title: 'Total Completed' },
                { data: 'initial_quota', title: 'Target To Complete' },
                { data: 'total_callable', title: 'Total In Queue' },
                { data: 'current_status', title: 'Current Status' },
                { data: 'wgtstamp', title: 'Weight' }
            ]
            ,
            footerCallback: function (row, data, start, end, display) {
                var api = this.api(), data, ;

                // Remove the formatting to get integer data for summation
                var intVal = function (i) {
                    return typeof i === 'string' ?
                        i.replace(/[\$,]/g, '') * 1 :
                        typeof i === 'number' ?
                        i : 0;
                };

                // Total over all pages
                var completedSubTotal = api
                    .column(2)
                    .data()
                    .reduce(function (a, b) {
                        return intVal(a) + intVal(b);
                    }, 0);

                // Total over this page
                var completedGrandTotal = api
                    .column(2, { page: 'current' })
                    .data()
                    .reduce(function (a, b) {
                        return intVal(a) + intVal(b);
                    }, 0);

                // Update footer with a subtotal
                $(api.column(2).footer()).html(completedSubTotal);

                // Update footer with a grand total
                $(api.column(2).footer()).html(completedGrandTotal);
            }


        });
    });


    $('input:search').on('keyup', function () {
        $('#reportTable').DataTable().search(this.value).draw();
    });

    $('#CampaignMenu').change(function(e) {

        $('#reportTable').DataTable().clear().draw();
        var campId = $(this).val();

        if (campId != '0') {

            $.ajax({
                type: 'POST',
                url: '@Url.RouteUrl("AccountQuotaDashboard.GetReportData")',
                data: { 'campaign_id': campId },
                dataType: 'json',
                success: function (json) {

                    if (json && !$.isEmptyObject(json)) {

                        var table = $('#reportTable').dataTable().fnAddData(json);

                    }

                }
            });

        }

    });

});

How can I correctly add 2 footer row to my data table?

UPDATED

I created a filddle to show you the code in action http://live.datatables.net/yubuyewi/2 . As you can see everything is working except for the total rows

You need to change the tfoot section of your table and add a tr and th Inorder to get the sub total and grand total like this.

<tfoot><tr><th></th><th></th><th></th><th></th><th></th><th></th><th></th><th></th></tr></tfoot>

Also you have swapped the sub total and grand total calculation.

In grand total calculation, you have used page: current which should be used in sub total calculation.

Corrected the script and the new fiddle is here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM