KoolReport's Forum

Official Support Area, Q&As, Discussions, Suggestions and Bug reports.
Forum's Guidelines

Data Tables rowGroup #1385

Closed Andre Van Der Walt opened this topic on on Apr 14, 2020 - 1 comments

Andre Van Der Walt commented on Apr 14, 2020

Hi,

The below code display the subtotal per date, but the subtotal only apply to current page. What should I amend to have a subtotal per date across all pages?

DataTables::create([
                    "dataSource"=>$this->dataStore("filtered_data"),
                    "options"=>[
                        "order"=> [[0, 'desc']],
                        "rowGroup"=>[
                            "startRender"=> null,
                            "endRender"=>"function ( rows, group ) {

                                var totalAuthorised = rows
                                    .data()
                                    .pluck(8)
                                    .reduce( function ( a, b ) {
                                        if ( typeof a === 'string' ) {
                                            a = a.replace(/[^\d.-]/g, '') * 1;
                                        }
                                        if ( typeof b === 'string' ) {
                                            b = b.replace(/[^\d.-]/g, '') * 1;
                                        }
                                        return a + b;
                                    }, 0 );
                 
                                var totalUnauthorised = rows
                                    .data()
                                    .pluck(9)
                                    .reduce( function ( a, b ) {
                                        if ( typeof a === 'string' ) {
                                            a = a.replace(/[^\d.-]/g, '') * 1;
                                        }
                                        if ( typeof b === 'string' ) {
                                            b = b.replace(/[^\d.-]/g, '') * 1;
                                        }
                                        return a + b;
                                    }, 0 );
                 
                                return $('<tr/>')
                                    .append( '<td colspan=\"6\">TOTAL FOR '+group+'</td>' )
                                    .append( '<td></td><td></td><td>'+totalAuthorised+'</td>' )
                                    .append( '<td>'+totalUnauthorised.toFixed(0)+'</td>' )
                                    .append( '<td></td>' );
                                
                            }",
                            "dataSrc"=>0,
                        ],

Thank you in advance,

David Winterburn commented on Apr 15, 2020

Hi Leon,

Here's an example of computing average age for all rows in a group instead of just the current page:

            endRender: function ( rows, group ) {
 
                var ageAvg = rows
                    .data()
                    .pluck(3)
                    .reduce( function (a, b) {
                        return a + b*1;
                    }, 0) / rows.count();
              
                var allRows = rows.rows().data().filter(function(value) {                  
                  return value[2] === group? true : false;
                });
                              var allAgeAvg = allRows
                    .pluck(3)
                    .reduce( function (a, b) {
                        return a + b*1;
                    }, 0) / allRows.length;
               
                return $('<tr/>')
                    .append( '<td colspan="3">Averages for '+group+'</td>' )
                    .append( '<td>'+ageAvg.toFixed(0)+ ' (' + allAgeAvg.toFixed(0) + ')</td>' )
                    .append( '<td/>' );
            }, 

I think it should work for total as well. Please try it and let us know your result. Thanks!

Build Your Excellent Data Report

Let KoolReport help you to make great reports. It's free & open-source released under MIT license.

Download KoolReport View demo
help needed

DataGrid