最后的数据计数器行

时间:2018-04-10 18:54:06

标签: javascript php jquery datatable datatables

我有一个正常的数据表输出信息,一些列中有数字。是否可以在每列的底部对该列中的总计数进行计数?

@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
});

1 个答案:

答案 0 :(得分:0)

对第3列求和的示例(索引从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;
}