• Resolved WPChina

    (@wordpresschina)


    Hello: I have a table with 3 columns:
    Company Name
    Revenue Total
    Date

    I then have a hundred rows of data for each of the 3 columns. I want to calculate the sum of all the Revenue Total fields at the bottom of the list. Is this possible?

Viewing 7 replies - 1 through 7 (of 7 total)
  • Hi,
    1- In addition, your table does not have a table footer. You should add a new row to the end of the table and click the “Table Footer” row as well.

    2- Paste this code to “Custom Commands” textfield without CR LF
    "footerCallback": function( row, data, start, end, display ) { var api = this.api(), data; var intVal = function ( i ) { return typeof i === 'string' ? i.replace(/[\$,]/g, '')*1 : typeof i === 'number' ? i : 0; }; total = api .column( 1, { page: 'current'} ) .data() .reduce( function (a, b) { return intVal(a) + intVal(b); }, 0 ); $( api.column(1 ).footer() ).html( total ); $( api.column( 0 ).footer() ).html('Total :'); }
    Note : first column number is 0

    Best regards
    Eric

    Thread Starter WPChina

    (@wordpresschina)

    Wow that’s great! May I ask where you acquired the code? Is there a page somewhere of more snippets like this for other uses?

    My table has lots of rows and the the Total that is calculated is this number: 267.15999999999985. However none of my numbers have more than 2 decimal places. Is there a method in the custom command to limit output to 2 decimal places?

    Plugin Author Tobias B?thge

    (@tobiasbg)

    Hi,

    thanks for your post, and sorry for the trouble.

    The JS calculation method is indeed very nice! To also give it only two decimals, you would have to add the JavaScript Math.round() function to the code.
    For similar code snippets, you could look through the Examples on https://www.datatables.net/

    Another option (if your cells are plain numbers), would be to use formulas in TablePress. For that, please see https://tablepress.org/tablepress-features-formulas/

    Regards,
    Tobias

    Thread Starter WPChina

    (@wordpresschina)

    Thank you @tobiasbg and @ericc44

    I am not so familiar with how to use Math.round() so I instead just used ={SUM(R2:R215)}

    I’m impressed how easy and flexible this is!

    Plugin Author Tobias B?thge

    (@tobiasbg)

    Hi,

    no problem, you are very welcome! ?? Good to hear that this helped!

    Best wishes,
    Tobias

    P.S.: In case you haven’t, please rate TablePress here in the plugin directory. Thanks!

    Hi,
    to have Round a number to the nearest integer:
    just replace this code
    $( api.column(1 ).footer() ).html( total );
    to
    $( api.column(1 ).footer() ).html( Math.round(total ));

    To have only two decimals
    just replace this code
    $( api.column(1 ).footer() ).html( total );
    to
    $( api.column(1 ).footer() ).html( total.toFixed(2));

    P.S.: In case you haven’t to TOBIAS, please rate TablePress here in the plugin directory.
    Thanks!

    Have a good day
    Best regards
    Eric

    Plugin Author Tobias B?thge

    (@tobiasbg)

    Hi,

    thanks for also posting the solution to this, ericc44!

    Best wishes,
    Tobias

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘How to compute a sum for a column of figures?’ is closed to new replies.