footer total using two columns

footer total using two columns

montoyammontoyam Posts: 329Questions: 82Answers: 4

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: 10,720Questions: 25Answers: 2,449

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

    Kevin

  • montoyammontoyam Posts: 329Questions: 82Answers: 4
    edited July 28

    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: 10,720Questions: 25Answers: 2,449
    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: 329Questions: 82Answers: 4

    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
    
Sign In or Register to comment.