Can I display result of sum(col1)/sum(col2) on col3's footer?

Can I display result of sum(col1)/sum(col2) on col3's footer?

kqw1983kqw1983 Posts: 5Questions: 2Answers: 0

Hi, I'm trying to display the division number on one column's footer from other two columns' sums. How can I achieve this by footerCallback? Thanks.

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,301Questions: 26Answers: 4,946
    Answer ✓

    Probably what I would do is keep separate totals (variables) for the two columns then perform the calculation and place it into the appropriate columns footer or other page location.

    Kevin

  • kqw1983kqw1983 Posts: 5Questions: 2Answers: 0

    My code works fine with ".sum" class columns, but there is no update on "credit" header's column sum after filter rows by search.

        "footerCallback": function ( row, data, start, end, display ) {
            var api = this.api(), data;
            // Remove the formatting to get integer 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 sumTotal = this.data().reduce( function (a, b) {
                    return intVal(a) + intVal(b);
                }, 0 );
    
                if(isNaN(sumTotal)){
                    this.footer().innerHTML = '';
                } else {
                    this.footer().innerHTML = sumTotal.toLocaleString();    
                }
            } );
    
            sumCredit = api.column(':contains(credit)').data().reduce(function(a,b){
                return intVal(a) + intVal(b);
            },0);
            $(api.column(':contains(credit)').footer()).html(sumCredit);
    
        }
    
  • kqw1983kqw1983 Posts: 5Questions: 2Answers: 0

    I found the reason, which is I missed {page:'current'} in the bottom line codes. Only then will footer's sum amount be updated after filtering by search.

This discussion has been closed.