sum renderd column

sum renderd column

hsoft2020hsoft2020 Posts: 16Questions: 6Answers: 0

Hi. I have table that some colums are rendered value. I cant manage to get the sum of that colums.I saw som post in datatable archives aboute it. but I could not figure it out how to use it. someone suggest user cells().render() instead of column().data(). I used this code:

        total2 = api
            .cells(4)
            .render('display')
            .reduce(function (a, b) {
                return intVal(a) + intVal(b);
            }, 0);

$(api.column(4).footer()).html(total2);

and I just get NAN value.

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,343Questions: 26Answers: 4,954

    That looks like it should work. Do you have the intVal() function?

    You can do some debugging of the reduce() function to see what the values are for a and b. Please provide a link to your page or a test case replicating the issue so we can help debug.
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    Kevin

  • rf1234rf1234 Posts: 3,000Questions: 87Answers: 421

    It depends on how your numbers are rendered. You would need to modify the intVal function or whatever you want to use in order to get the numbers right

    var intVal = function ( i ) {
            return typeof i === 'string' ?
                i.replace(/[\$,]/g, '')*1 :
            typeof i === 'number' ?
            i : 0;
            };
            
    alert (intVal("3,000.12")) //result: 3000.12 = ok
    
    alert (intVal("3.000,12")) //result: 3.00012 = wrong
    
    alert (intVal("3.000,12 EUR")) //result: NaN
    
  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    Yep, that should work, so it would suggest there's an issue with your data. We're happy to take a look, but as per the forum rules, please link to a test case - a test case that replicates the issue will ensure you'll get a quick and accurate response. Information on how to create a test case (if you aren't able to link to the page you are working on) is available here.

    Cheers,

    Colin

  • rf1234rf1234 Posts: 3,000Questions: 87Answers: 421

    I do numbers rendering differently depending on the user's language. For some users the thousand separator is a period and they use a decimal comma (e.g. German), other users have commas as thousand separators and decimal points (e.g. English).

    This is my code to make rendered numbers numeric depending on the user language:

    if (lang == 'de') { //e.g. 3.000.000,12
        rowTotal = rowTotal.toString().replace( /[\.]/g, "" );
        rowTotal = rowTotal.toString().replace( /[\,]/g, "." );            
    } else { //eg 3,000,000.12
        rowTotal = rowTotal.toString().replace( /[\,]/g, "" );
    }
    //result is 3000000.12 in both cases
    
    
  • allanallan Posts: 63,535Questions: 1Answers: 10,475 Site admin

    Worth noting that as of 1.12 the number renderer can do automatic locale formatting.

    Allan

  • hsoft2020hsoft2020 Posts: 16Questions: 6Answers: 0

    Hi. I could provide a test link.
    please see this:
    https://affilate.buy.parsdatam.company/testdt.php

    in Share Amount columt ajax return the share precent and data will be rendered with this formula: share amount= share precent /100* Payment
    As you see total sum in columns 2 and 3 works fine because they are not rendered but in column 4 it returns 0

  • rf1234rf1234 Posts: 3,000Questions: 87Answers: 421

    this looks weird:

    total2 = api
                    .cells(4)
                    .render('display')
                    .reduce(function (a, b) {
                        return intVal(a) + intVal(b);
                    }, 0);
    

    You want column 4 right? I don't think that cells(4) will give you the values of the cells of column 4. Check the docs please: https://datatables.net/reference/api/cells()

    This could work

    total2 = api
                    .column(4, { page: 'current' })
                    .data()
                    .reduce(function (a, b) {
                        return intVal(a) + intVal(b);
                    }, 0);
    
  • hsoft2020hsoft2020 Posts: 16Questions: 6Answers: 0

    yes , I want column 4 sum. but as I said column().data() just return raw data sum. I want to sum the rendered data in column 4.
    it contains the percent and I have to multiple it in column 3 data and then sum the result in footer

  • kthorngrenkthorngren Posts: 21,343Questions: 26Answers: 4,954
    edited July 2022 Answer ✓

    This thread has the same question. As rf1234 points out you need to add null to the cells() API, like this:

    total2 = api
                    .cells(null, 4)
    

    This means the row-selector is null meaning all rows and the column-selector is 4.

    Kevin

  • hsoft2020hsoft2020 Posts: 16Questions: 6Answers: 0

    thank. this worked. I missed the row selector and I get wrong result. thanks again for answering.

Sign In or Register to comment.