Compute column Total amount in a new row using dataset

Compute column Total amount in a new row using dataset

baokybaoky Posts: 8Questions: 6Answers: 0
edited September 2014 in Free community support

Compute column amount in a new row using dataset

My data is from a dataset and not api, is it possible for me to compute a column sub total

E.g if my data set is something like this

$data .= "['" . $t_date . "','" . $t_description . "','" . $t_amount . "','"  . $t_rebate . "','" . $t_net . "','"  . $t_returns.  "','"  . $t_balance . "'],";

I want to compute the total of $t_balance , and append it in a new row below the data table such as, t_balance have value like -100.50, 500.20 , all the doubles with - for negative value, but I not using api so I not sure if I can use fallback function, if I am using dataset, is it possible to get the same result as api, and append the total of my t_balance in a new row on change of page or number of records from 10 to 50 etc.

var subtotal;

//add row
        table.row.add( [
        "",
        "",
        "",
        "",
        "",
        "",
        "",
       'subtotal'
        ] ).draw();

This question has an accepted answers - jump to answer

Answers

  • vogomatixvogomatix Posts: 38Questions: 3Answers: 7
    edited September 2014 Answer ✓

    You can get all the data for a column by:

    var list = table.column(4).data();
    var total = list.reduce(function(previousValue, currentValue, index, array){
      return previousValue + currentValue;
    });
    

    You could hook this calculation into a preDrawCallback or footerCallback and create/update your total row.

This discussion has been closed.