Footercallback on columns error with single row

Footercallback on columns error with single row

kputchakputcha Posts: 9Questions: 1Answers: 0

Works just fine if number of rows is 2+.
If number of rows is 1, column totals are correct, but " bigTotal " is wrong...instead of $45.00, I get $4,500.00

    "footerCallback": function ( row, data, start, end, display ) {
        var api = this.api(), data;
        var bigTotal = 0;
        // 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
        totalCash = api
            .column( 2 )
            .data()
            .reduce( function (a, b) {
                return intVal(a) + intVal(b);
            } );
            bigTotal = bigTotal + totalCash;
        totalCash = Number(totalCash).toLocaleString("en-US", {style:"currency", currency:"USD", minimumFractionDigits: 2});

        totalCheck = api
            .column( 3 )
            .data()
            .reduce( function (a, b) {
                return intVal(a) + intVal(b);
            } );
           bigTotal = bigTotal + totalCheck;   
        totalCheck = Number(totalCheck).toLocaleString("en-US", {style:"currency", currency:"USD", minimumFractionDigits: 2});

        totalCredit = api
            .column( 5 )
            .data()
            .reduce( function (a, b) {
                return intVal(a) + intVal(b);
            } );
        bigTotal = bigTotal + totalCredit;
        totalCredit = Number(totalCredit).toLocaleString("en-US", {style:"currency", currency:"USD", minimumFractionDigits: 2});

        totalPaypal = api
            .column( 6 )
            .data()
            .reduce( function (a, b) {
                return intVal(a) + intVal(b);
           } );
        bigTotal = bigTotal + totalPaypal; 
        totalPaypal = Number(totalPaypal).toLocaleString("en-US", {style:"currency", currency:"USD", minimumFractionDigits: 2});

        //DIVs inside footer th 
        $("#totalCash").text("    " + totalCash);
        $("#totalCheck").text("    " + totalCheck);
        $("#totalCredit").text("    " + totalCredit);
        $("#totalPaypal").text("    " + totalPaypal);
        bigTotal = Number(bigTotal).toLocaleString("en-US", {style:"currency", currency:"USD", minimumFractionDigits: 2});
        $("#bigTotal").text("    " + bigTotal);
    }

Replies

  • kputchakputcha Posts: 9Questions: 1Answers: 0
  • kthorngrenkthorngren Posts: 21,303Questions: 26Answers: 4,947

    According to the reduce() MDN docs the follwoing happens with one value:

    If the array has only one element (regardless of position) and no initialValue is provided, or if initialValue is provided but the array is empty, the solo value will be returned without calling callback.

    Your variables like totalCash end up a string with values like $45.00. bigTotal is probably a concat string of your columns. Try changing to something like this for each column:

    bigTotal = bigTotal + intVal(totalCash);
    

    Kevin

  • kputchakputcha Posts: 9Questions: 1Answers: 0

    That worked...thanks.

This discussion has been closed.