0

I have a normal datatable that outputs information and a few columns have numbers in them. Is it possible at the bottom of each column to do a tally on the total count in that column?

@foreach($res as $r)
          <tr>
              <td>
                {{-- {{ $user->fname }}   {{ $user->lname }} --}}
              {{ $r['name'] ?? null }}
              </td>
              <td>
                {{ $r[$threelastYearTitle] ?? 0 }}
              </td>
              <td>
                {{ $r[$twolastYearTitle] ?? 0 }}
              </td>
              <td>
                {{ $r[$onelastYearTitle] ?? 0 }}
              </td>
              <td>
                  {{ $r['booked']['onelastYearTitle']}}
              </td>
              <td>
                {{ $r[$currentYearTitle] ?? 0 }}
              </td>
              <td>
                  {{ $r['booked']['currentYear']}}
              </td>
             <td>
               <a class="btn btn-default" href="/admin/user/{{ $id }}/edit">Edit</a>
              </td>
          </tr>
      @endforeach
</table>

$('#myTable').DataTable({
    "order": [],
    // "scrollX": true,
    "columnDefs": [ {
      "targets"  : 'no-sort',
      "orderable": false,
      "language": {
        "search": "Filter records:"
      }
  }],
  "pageLength": 25
});
Roamer-1888
  • 19,138
  • 5
  • 33
  • 44
Victor
  • 136
  • 1
  • 13
  • 2
    yes but you will have to provide your table html or a subset of it – Below the Radar Apr 10 '18 at 18:58
  • @BelowtheRadar updated code – Victor Apr 10 '18 at 19:13
  • Ok thank you. What is that DataTable library you are using? Can you post a link to its repositary? – Below the Radar Apr 10 '18 at 19:15
  • https://datatables.net – Victor Apr 10 '18 at 19:15
  • There's a good example for this on the DataTables website: https://datatables.net/examples/advanced_init/footer_callback.html – colin0117 Apr 10 '18 at 19:16
  • These links can also help in addition to the one above https://datatables.net/plug-ins/api/sum() https://datatables.net/forums/discussion/37830/how-to-add-row-in-the-footer-that-sum-each-column – OPMat Apr 10 '18 at 20:57
  • @OPMat I just made my own sum algorithm and now you show me the easy route lol. Thanks though! – Victor Apr 11 '18 at 13:12
  • Ha, doing it the hard way is always a good learning exercise :) – colin0117 Apr 11 '18 at 13:13
  • Please avoid link only answers. We really dont know if the link exists in 14 days ...I believe this answer should have been posted as a comment only. – davidkonrad Apr 11 '18 at 23:32
  • 1
    @davidkonrad - that's a fair point on the link, I tend to avoid repeating info as that also goes stale, but happy to post examples too. – colin0117 Apr 12 '18 at 07:56
  • @colin0117, please dont take it personal :) If you just have copy pasted the code with your own comments there had been no problem. You can do it now! I know your concerns, you are a fair person with high moral, but even those with the highest rep on this forum have copy pasted code from somewhere else. The only really bad thing to do in this regard is to copy and paste another answer from SO. If you can find an existing question+answer, mark the question as duplicate. This is not the case here. In my opinion, you are free to copy paste what ever from the link and get the reps out of it. – davidkonrad Apr 15 '18 at 13:37

1 Answers1

0

Example to sum the column 3 (index start with 0)

fnFooterCallback : function(nRow, aaData, iStart, iEnd, aiDisplay) {
    var total = 0;
    for (var i = iStart; i < iEnd; i++) {
        total += parseFloat(aaData[aiDisplay[i]][2]);
    }
    var nCells = nRow.getElementsByTagName('th');
    nCells[2].innerHTML = "Total " + total;
}