Problem with adding column values and rowgrouping

Problem with adding column values and rowgrouping

nicoledramirez@hotmail.comnicoledramirez@hotmail.com Posts: 60Questions: 14Answers: 2
edited January 2018 in DataTables

Hi!
Having an issue with the rowgroup feature. Trying to add the values of a calculated column but keep getting NaN in the grouping:

$('#example').DataTable( {

        dom: "Bfrtip",
        ajax: "/api/cdi_masterpost",
        rowGroup: {
            startRender: null,
            endRender: function ( rows, group ) {
                var sum = rows
                .data()
                .pluck(7)
                .reduce( function (a, b) {
                    return a + b;
                });
 
                return 'Total Billable Hours for '+group+': '+
                $.fn.dataTable.render.number(',').display( sum );
            },
            dataSrc: 'cdi_details.cdi_master_id'
        },
    
        
        columnDefs: [
{
    targets: 7, render: function (data, type, row) {
        var hourscal = parseInt(data.cdi_details.hours);
        var minutescal = parseInt(data.cdi_details.minutes);
        var group = parseInt(data.cdi_details.group_count);
        var groupminutes1 = hourscal * 60 / group;
        if (minutescal !== 0) {
            var groupminutes2 = minutescal / group;
        } else {
            var groupminutes2 = 0;
        }
        var totalgroupminutes = Math.round(groupminutes1 + groupminutes2);
        if (totalgroupminutes >= 60) {
            var totalhours = Math.floor(totalgroupminutes / 60)
        } else {
            var totalhours = 0;
        }
        return totalhours;
    }
},
{
    targets: 8, render: function (data, type, row) {
        var hourscal = parseInt(data.cdi_details.hours);
        var minutescal = parseInt(data.cdi_details.minutes);
        var group = parseInt(data.cdi_details.group_count);
        var groupminutes1 = hourscal * 60 / group;
        if (minutescal !== 0) {
            var groupminutes2 = minutescal / group;
        } else {
            var groupminutes2 = 0;
        }
        var totalgroupminutes = Math.round(groupminutes1 + groupminutes2);
        if (totalgroupminutes >= 60) {
            var totalsubminutes = Math.floor(totalgroupminutes / 60) * 60
        } else {
            var totalsubminutes = 0;
        }
        return totalgroupminutes - totalsubminutes;
    }
}
        ],

        columns: [
              { data: "cdi_details.cdi_master_id" },
                        { data: "cdi_master.cdocdate" },
            { data: null, render: function ( data, type, row ) {
                // Combine the first and last names into a single table field
                return data.cdi_details.firstname+' '+data.cdi_details.lastname;
            }
            },
                  { data: "cdi_details.service_class" },
                    { data: "cdi_details.document_title" },
                        { data: "cdi_details.hours" },
                        { data: "cdi_details.minutes" },
                        {
                            data: null
                          
                        },
                          {
                              data: null
                              
                          },
                        { data: "cdi_details.group_count" }
        ],

Any suggestions? I figured that it was probably the reference to column 7, but it allows me to change visibility of the column referencing it that way and run the function so not sure why it wouldn't work in the rowgrouping...?

Edited by Allan - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    NaN as a return suggests that non numeric data is being added. Without being able to see the page (and thus the data) I'm afraid there isn't a huge amount of help that we can really offer.

    Allan

  • nicoledramirez@hotmail.comnicoledramirez@hotmail.com Posts: 60Questions: 14Answers: 2

    I think I kind of know why it is saying it is a non-numeric data. It is because the two data columns in there are initially set as null {data: null} and then I run the calculation in the ColumnDef code so it is taking the null value in the 7th column. But I am not sure how to get the timing down so that the rowGrid tag executes the calculation after column 7 is populated...??

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Answer ✓

    Use cells().render() rather to get the rendered data rather than one of the .data() methods (which would indeed just return null if you have data: null.

    rows.cells( rows.indexes(), 7 ).render() would be the way to do that in that context.

    You could console.log() the result of that just to make sure.

    Allan

  • nicoledramirez@hotmail.comnicoledramirez@hotmail.com Posts: 60Questions: 14Answers: 2

    Thanks for the info Allan!

This discussion has been closed.