footer total using two columns

footer total using two columns

montoyammontoyam Posts: 568Questions: 136Answers: 5

I am having trouble understanding how to use data from two columns together for the footer. The examples given use something like this:

                    total_WAN = api
                        .column(1)
                        .data()
                        .reduce(function (total, num) {
                            return intVal(total) + intVal(num);
                        }, 0);

however, what I need to do is to sum (column(1)*column(2))

from what I see, the reduce function will only work with one column?

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 20,142Questions: 26Answers: 4,736

    One way is to get the sum of the other column then add the two together for the footer.

    Kevin

  • montoyammontoyam Posts: 568Questions: 136Answers: 5
    edited July 2020

    no, i can't do that. one column is the count of users. the 2nd column is a 1 or a zero. The sum needs to be a sum of all the (count x 1) + (count x 0).

    hope that makes sense. you can't just add the two and multiply or add them.

  • kthorngrenkthorngren Posts: 20,142Questions: 26Answers: 4,736
    Answer ✓

    In that case I would use filter() to filter the rows where the 2nd column is 1 and chain pluck() to get the values in column 1 into an array using toArray(). You can then sum the array using reduce(). Here is an example that generates an array of Names that are in the London Office.
    http://live.datatables.net/yewikige/1/edit

    Kevin

  • montoyammontoyam Posts: 568Questions: 136Answers: 5

    yes, that worked perfectly!! Thank you

                        total_WAN = api
                            .column(1)
                            .data()
                            .reduce(function (total, num) {
                                return intVal(total) + intVal(num);
                            }, 0);
    
                        var total_LAN = api
                            .rows()
                            .data()
                            .filter(function (value, index) {
                                return value["FundOrgWAN_Count"]["UsesLAN"] == '1' ? true : false;
                            })
                            .pluck('FundOrgWAN_Count')
                            .pluck('WAN_Count')
                            .toArray()
                            .reduce(function (total, num) {
                                return intVal(total) + intVal(num);
                            }, 0);
    
                        var total_Internet = api
                            .rows()
                            .data()
                            .filter(function (value, index) {
                                //console.log("value", value, "index", index, "3", value["FundOrgWAN_Count"]["UsesInternet"]);
                                return value["FundOrgWAN_Count"]["UsesInternet"] == '1' ? true : false;
                            })
                            .pluck('FundOrgWAN_Count')
                            .pluck('WAN_Count')
                            .toArray()
                            .reduce(function (total, num) {
                                console.log("num", num);
                                return intVal(total) + intVal(num);
                            }, 0);
    
                        var totals = 'WAN Total: ' + total_WAN + '<br/>Total LAN: ' + total_LAN + '<br/>Total Internet: ' + total_Internet
    
This discussion has been closed.