DataTables / DataTables/Plugins

I can't get the sum of all columns

Open
#258 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
1.1k
Forks
1.7k
PR merge metrics
No merged PRs in 30d

Description

I am trying to calculate the sum of multiple column using the sum() api.

Here is what I have done,


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

here is how I construct my DataTable

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

            $('#reportTable').DataTable({
                pageLength: 50,
                lengthMenu: [10, 25, 50, 75, 100, 250, 500, 1000],
                dom: 'Bfrtip',
                buttons: [
                    'copy', 'csv', 'excel', 'pdf', 'print'
                ],
                order: [   [ 2, 'desc' ]  ],
                columns: [
                    { data: 'chain_name', title: 'Chain Name', width: '20%'},
                    { data: 'store_id' , title: 'Store Number' },
                    { data: 'completed', title: 'Total Surveys Completed' },
                    { data: 'initial_quota', title: 'Target Surveys To Complete' },
                    { data: 'total_callable', title: 'Total Call In The Dialer Queue' },
                    { data: 'current_status', title: 'Current Quota Status' },
                    { data: 'wgtstamp', title: 'Weighted Stamp' },
                    { data: 'total_attempts', title: 'Total Attempts' },
                    { data: 'average_attempts', title: 'Average Attempts' }
                ],
                footerCallback: function () {

                    var api = this.api();

                    var table = api.table();

                    //Sub Total
                    $( table.footer(0) ).html(
                        api.column( 1, {page:'current'} ).data().sum()
                    );

                    $(table.footer(0)).html(
                        api.column(2, { page: 'current' }).data().sum()
                    );

                    $(table.footer(0)).html(
                        api.column(3, { page: 'current' }).data().sum()
                    );

                    $(table.footer(0)).html(
                        api.column(4, { page: 'current' }).data().sum()
                    );
                    /*
                    $(table.footer(0)).html(
                        api.column(6, { page: 'current' }).data().average()
                    );
                    */


                    $(table.footer(0)).html(
                        api.column(7, { page: 'current' }).data().sum()
                    );

                    $(table.footer(0)).html(
                        api.column(8, { page: 'current' }).data().sum()
                    );


                    //Grand Totals
                    $(table.footer(1)).html(
                        api.column(1).data().sum()
                    );

                    $(table.footer(1)).html(
                        api.column(2).data().sum()
                    );

                    $(table.footer(1)).html(
                        api.column(3).data().sum()
                    );

                    $(table.footer(1)).html(
                        api.column(4).data().sum()
                    );

                    /*
                    $(table.footer(1)).html(
                        api.column(6).data().average()
                    );
                    */

                    $(table.footer(1)).html(
                        api.column(7).data().sum()
                    );

                    $(table.footer(1)).html(
                        api.column(8).data().sum()
                    );

                }


            });
        });

However, the I only get one total in the very first column.

I am hopping that I can put the page total in the first row in the footer and the total from all pages in the second row of the footer.

Additionally, is there a shorted way to apply one code for multiple column so I won't have to repeat my code multiple times?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the footerCallback shown in issue #258 and review the DataTables column and footer APIs used there. Verify how the two footer rows and multiple selected columns are addressed; done means page totals and grand totals appear in their corresponding footer cells without repeated overwriting.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
frontend
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.