Sum Columns don't work

Sum Columns don't work

diablonetdiablonet Posts: 3Questions: 0Answers: 0

Sorry, but I have this problem. I read different solutions but they don't work.
I need a sum of column in my table. This is my js code but only alert with ciao doesn't show

why? Thanks

$(document).ready(function() {
$('#example').DataTable( {
footerCallback: function () {
alert("Ciao");
var api = this.api();
$( api.table().footer() ).html(
api.column( 5, {page:'current'} ).data().sum()
);
}
} );
} );

Replies

  • kthorngrenkthorngren Posts: 20,369Questions: 26Answers: 4,779

    I don't think sum() is a function of data(). Take a look at your browser's console to see if you get any errors.

    Does the example here help?
    https://datatables.net/examples/advanced_init/footer_callback.html

    Kevin

  • allanallan Posts: 61,823Questions: 1Answers: 10,129 Site admin

    There is a sum() plug-in which would be available at any level of the chain. You do need to make sure you have included that plug-in.

    It sounds like there is a Javascript error occurring. We'd need a link to a test case showing the issue to be able to say exactly what the issue is though.

    Allan

  • diablonetdiablonet Posts: 3Questions: 0Answers: 0

    Hi, I have this error in browser console

    Uncaught TypeError: Cannot read property 'register' of undefinedn at Sum.js:39

    this is the code in sum.js file start at row 39

    jQuery.fn.dataTable.Api.register('sum()', function () {
    return this.flatten().reduce(function (a, b) {
    if (typeof a === 'string') {
    a = a.replace(/[^\d.-]/g, '') * 1;
    }
    if (typeof b === 'string') {
    b = b.replace(/[^\d.-]/g, '') * 1;
    }

        return a + b;
    }, 0);
    

    });

    My project is only on localhost, where could I create a test link?

This discussion has been closed.