0

I have jquery datatable and I want to calculate sum of two columns. initialization of the table

 $('#myTable').DataTable({
        autoWidth: false,
        searching: false,
    ...
     initComplete: function (settings, json) {
        // calculate sum and inject into second and third row in the footer
     });
  });

<table id="myTable">
   <thead>
      <tr>
         <th>Id</th>
         <th>Time one</th>
         <th>Time two</th>
      </tr>
   </thead>
   <tfoot>
      <tr>
         <th></th>
         <th></th>
         <th></th>
      </tr>
   </thead>
</table>

Update:

vales inside this columns are like:

=====================
| 1 | 01:15 | 10:00 |
=====================
| 9 | 11:44 | 0:120 

|

2
  • 2
    What exactly is your question? What problem did you encounter? Commented Jan 18, 2017 at 12:29
  • let me rephrase the question: I want total in the footer of the third column for example. Commented Jan 18, 2017 at 12:46

1 Answer 1

0

You can do it using DataTable Plugin API:

// Simply get the sum of a column
var table = $('#example').DataTable();
table.column( 3 ).data().sum();

// Insert the sum of a column into the columns footer, for the visible
  // data on each draw
  $('#example').DataTable( {
    drawCallback: function () {
      var api = this.api();
      $( api.table().footer() ).html(
        api.column( 4, {page:'current'} ).data().sum()
      );
    }
  } );

More: https://datatables.net/plug-ins/api/sum()

Sign up to request clarification or add additional context in comments.

5 Comments

Not sure if OP wants the total or rather a new column containing the row by row sum
total in the footer.
So you can use this api.
this way I'm getting sum of all numbers inside one column. How can I calculate time with hours and minutes like in the example I posted.
Maybe change it to minutes then sum.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.