Footer Sums

Footer Sums

dobulet302dobulet302 Posts: 38Questions: 0Answers: 0
edited August 2010 in General
I am trying to display the sum of each column in the footer, I am using this code

[code]
"fnFooterCallback": function(nRow, aasData, iStart, iEnd, aiDisplay ) {

//delete the footers
$($(nRow).children()).remove();
//recreate the footers
for(var i=0;i

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Funny... You'll not get a floating point number when using parseInt on the numbers, but to be off by three orders of magnitude.... wow.

    If you "console.log( parseInt(aasData[aiDisplay[i]][5]) )" in the for loop, what output do you get on the console?

    Allan
  • dobulet302dobulet302 Posts: 38Questions: 0Answers: 0
    Not sure that I follow you, however I inserted that line and ran the page and it didnt return anything.
  • dobulet302dobulet302 Posts: 38Questions: 0Answers: 0
    I was able to go with this

    [code]
    "fnFooterCallback": function (nRow, aasData, iStart, iEnd, aiDisplay ) {
    $($(nRow).children()).remove();
    for(var i=0;i
  • ruzzruzz Posts: 49Questions: 0Answers: 0
    Yeah, ignoring your actual TD traversal entirely (it may be correct, it may not)...

    What Allan is finding funny is you're expecting to see a floating point number (e.g. NNNNN.nnnnn) after having told the code to make it an integer using parseInt. Integers are WHOLE numbers (i.e. no decimal point or figures below it).

    Hint: try parseFloat?

    ruzz
  • ruzzruzz Posts: 49Questions: 0Answers: 0
    Oh and NaN means "not a number". JS is complaining that something in your maths is not numeric and cannot be treated as such. IOW, you can't add 1 and 2 and 3 and [stuff that isn't numeric - which includes nulls and blank cells etc].

    ruzz
  • dobulet302dobulet302 Posts: 38Questions: 0Answers: 0
    thanks for the reply ruzz, I did some reasearch on javascript number formating and I did try parseFloat however it failed. any other suggestions?
  • ruzzruzz Posts: 49Questions: 0Answers: 0
    When you say "failed"...
  • dobulet302dobulet302 Posts: 38Questions: 0Answers: 0
    edited August 2010
    it's simply not adding up any formatted numbers, either with a comma or decimal point.
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Try using this 'for' loop and open the firebug console:

    [code]
    for(var i=iStart;i
  • dobulet302dobulet302 Posts: 38Questions: 0Answers: 0
    getting console is not defined, I have firebug installed and restarted my browser. Not sure why I am getting the error.
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Is Firebug open, and the console enabled (click the little down arrow next to the 'console' tab)?

    Allan
  • dobulet302dobulet302 Posts: 38Questions: 0Answers: 0
    Ahh I am an idiot, ok its enabled and I pretty much get a spit out of all my numbers.

    it shows NaN (34 times) then the rest of the data....nothing else.
  • dobulet302dobulet302 Posts: 38Questions: 0Answers: 0
    Ok I figured out the NaN part, however my results are now showing (0.09659999999999998) when it should be (.0.096600)...all data is 6 decimal places. Also how can I format my footer data to have comma's as well?
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    So is everything basically adding up correctly now? If it's just formatting that is the issue now: http://www.mredkj.com/javascript/nfbasic2.html

    Allan
  • dobulet302dobulet302 Posts: 38Questions: 0Answers: 0
    yes it adds up now I need to format with comma and (6) decimal places.
  • dobulet302dobulet302 Posts: 38Questions: 0Answers: 0
    column 1 is the decimal and commas and column 3 and 4 are just commas. How could I break each one up?
  • dobulet302dobulet302 Posts: 38Questions: 0Answers: 0
    Alan thanks so much for your help, but I am strugling with how to format the footer totals. My code is below.

    [code]
    "fnFooterCallback": function (nRow, aasData, iStart, iEnd, aiDisplay ) {
    $($(nRow).children()).remove();
    for(var i=0;i
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    So the problem is just how to format "total" now is it?

    I'm not sure what you mean by comma and decimal - do you mean a comma for a thousands separator? From the above code you are only going to get integers. In which case you could use the built in number formatter in DataTables:

    [code]
    $($(nRow).children().get(columnaActual)).html(
    this.fnFormatNumber( total ) );
    );
    [/code]
    It won't cope with floats though. For that, have a look on Google - there are loads of number formatters available for Javascript.

    Allan
  • dobulet302dobulet302 Posts: 38Questions: 0Answers: 0
    Thanks for all your help allan, I was able to find 2 javascript functions to help me out.
This discussion has been closed.