skepnaden skepnaden - 3 months ago 131
jQuery Question

DataTables.net table column sum in footer

I'm having issues with a tiny detail in inserting the sum value of each column, with class "sum", into its footer.

The code (more or less taken straight from DataTables.net) goes:



var table = $('#example').DataTable();
table.columns('.sum').each(function (el) {
var sum = table
.column(el)
.data()
.reduce(function (a, b) {
return parseInt(a, 10) + parseInt(b, 10);
});
$(el).html('Sum: ' + sum);
});





"sum" has the correct value but is somehow not inserted into the footer! I.e. its -element shows up empty.

Note that the snippet below works fine, but I want to sum each column with class sum.



var table = $('#example').DataTable();
var column = table.column(4);

$(column.footer()).html(
column.data().reduce(function (a, b) {
return parseInt(a, 10) + parseInt(b, 10);
})
);





////////////////////////////////////////////////////////////////////////////////////

EDIT - Workaround:



I moved the code to where the DataTable is initialized and went with footerCallback instead. See below code:



"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 this page
pageTotal = api
.column('.sum', { page: 'current' })
.data()
.reduce(function (a, b) {
return intVal(a) + intVal(b);
}, 0);

// Update footer
$(api.column('.sum').footer()).html(pageTotal);
}





Somehow now the value is inserted into the right tfoot element. Still no idea why it wouldn't work in the first place (but as mentioned in comment order of including JS-files could have had to do with some of it).

Now I just have to figure out how to loop multiple columns with class="sum"...

Answer

Your table manipulation code needs to be executed only when DataTable is initialized, (see initComplete property).

Also make sure you have <tfoot></tfoot> block defined in your <table> otherwise there would be no footer.

Otherwise you were very close to the solution, please see below the corrected code:

var table = $('#ticketTable').DataTable({
    'initComplete': function (settings, json){
        this.api().columns('.sum').every(function(){
            var column = this;

            var sum = column
                .data()
                .reduce(function (a, b) { 
                    return parseInt(a, 10) + parseInt(b, 10);
                });

            $(column.footer()).html('Sum: ' + sum);
        });
    }
});

See this JSFiddle for an example.

UPDATE

There is also footerCallback property that lets you defined footer display callback function which will be called on every "draw" event.

The difference between initComplete and footerCallback is that initComplete is called once and footerCallback on every "draw" event.

If you're displaying the sum for the whole table, initComplete should suffice. Otherwise if you require to show in the footer data relevant for current page only (as in Footer callback example), use footerCallback instead.