Calculate footer based on other footer values

Calculate footer based on other footer values

smasonsmason Posts: 25Questions: 11Answers: 0

I have a footer callback working to summarize columns. I would like to calculate one more footer column based on the values of two of the footer column totals.

Is this possible in the footer callback?

"footerCallback": function(row, data, start, end, display) {
                          var api = this.api();

                          // Remove the comma to get data for summation
                            var intVal = function ( i ) {
                                return typeof i === 'string' ?
                                    i.replace(/,/g,'')*1 :
                                    typeof i === 'number' ?
                                        i : 0;
                            };

                          api.columns('.sum', {
                            page: 'current'
                          }).every(function() {
                            var sum = this
                              .data()
                              .reduce(function(a, b) {
                                    var x = String(a);
                                        x = parseFloat( x.replace(/,/g,'') );
                                        x = Math.round(x * 100) / 100;


                                    var y = String(b);  //turn object into string
                                        y = parseFloat( y.replace(/,/g,'') ); //remove all commas
                                        y = Math.round(y * 100) / 100; //round to 2 decimal places

                                    theSum = (x + y).toFixed(2); //sum the two operands & set to 2 decimal places

                                return theSum.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
                              }, 0);
                            //console.log(sum); //alert(sum);
                            $(this.footer()).html("$ " + sum);
                          });

//this part creates a average but it's based on all the rows, I need it to be based on the totals of two of the other columns
                          api.columns('.avg', {
                            page: 'current'
                          }).every(function() {

                            var numerator = this
                                    .data()
                                    .reduce( function(a, b) {
                                        return (intVal(a) + intVal(b));
                                    }, 0);

                            var denominator = this.data().length;

                            avg = numerator / denominator;
                            avg = avg.toFixed(2)

                            $(this.footer()).html("" + avg + "%");
                          });

This question has an accepted answers - jump to answer

Answers

  • colincolin Posts: 15,236Questions: 1Answers: 2,598
    Answer ✓

    Hi @smason ,

    At the end of that footerCallback function, you can just use column().footer() to get the values of the other two columns, similar to what you're doing. You just need to do it after you've slotted in the other totals.

    Cheers,

    Colin

This discussion has been closed.