0

I am using the DataTables jQuery plugin to display data from the database. Currently I am using the example of Row Grouping to display the days per week.

I am working at this for a week now to also display an row at the bottom of the week to count the hours of that week but it seems to be nearly to impossible with this plugin to achieve that goal. Is there anybody who could advise me if this can be done and how.

Here is my code:

var api  = this.api(),data;
var rows = api.rows({ page:'current' }).nodes();
var last = null;

total = new Array();

api.column(weekColumn, { page:'current' }).data().each(function(group, i) {
    group_assoc = group.replace(' ',"_");
    if (typeof total[group_assoc] != 'undefined') {
        total[group_assoc]=total[group_assoc]+api.column(5).data()[i];
    } else {
        total[group_assoc]=api.column(5).data()[i];
    }

    if (last !== group) {
        $(rows).eq( i ).before('<tr class="group" style="background-color:#3ea5ce;color:#ffffff;"><td colspan="5">{{ trans('admin.worktime.field.week') }} '+group+'</td><td colspan="3" class="'+group_assoc+'"></td></tr>');

        last = group;
    }
});

for (var key in total) {
    $("." + key).html(total[key]);
}

Currently, it is been outputted as 09:3907:0008:0107:5207:28 instead of been counted as a total. How can I solve this?

1 Answers1

0

I think you are trying to sum time values, but they are treated as strings and concatenated. This post describes how to first make integer values of these time strings first. Calculate the sum of the integers and convert that back to a time.

Community
  • 1
  • 1
verhie
  • 1,298
  • 1
  • 7
  • 7
  • Thank you for your comment. Why do I need to first convert them to seconds and where do I need to put that function? It seems to make no sense to first convert them to seconds and convert them back to HH:mm. –  Sep 12 '16 at 12:51
  • in your .each loop: `a = api.column(5).data()[i].split(':');` `minutes = (+a[0]) * 60 + (+a[1]);` `total[group_assoc]=total[group_assoc] + minutes;` – verhie Sep 13 '16 at 07:19