DataTables' Client-side Footers

Various client-side footers for current page, searched pages, all pages

customerName
dollar_sales

This example shows how to calculate various client-side footer for the current page, all searched pages, and all pages.

<?php
require_once "MyReport.php";

$report = new MyReport;
$report->run()->render();
<?php
//Step 1: Load KoolReport
require_once "../../../load.koolreport.php";

//Step 2: Creating Report class
class MyReport extends \koolreport\KoolReport
{
    function settings()
    {
        return array(
            "dataSources" => array(
                "employees"=>array(
                    "connectionString"=>"mysql:host=localhost;dbname=automaker",
                    "username"=>"root",
                    "password"=>"",
                    "charset"=>"utf8"
                ), 
            )
        );
    }
    protected function setup()
    {

    }
}
<?php
    use \koolreport\datagrid\DataTables;
?>
<div class="report-content">
    <div class="text-center">
        <h1>DataTables' Client-side Footers</h1>
        <p class="lead">
            Various client-side footers for current page, searched pages, all pages
        </p>
    </div>

    <?php
    DataTables::create(array(
        'name' => 'DataTable1',
        'dataSource' => function() {
            return $this->src("automaker")
            ->query("select * from customer_product_dollarsales2 limit 500");
        },
        'scope' => $this->params,
        "options" => array(
            "searching" => true,
            "paging" => true,
            "colReorder" => true,
            "order" => [],
            "ordering" => true,
            "pageLength" => 10,
            "footerCallback" => "function ( row, data, start, end, display ) {
                // console.log(row, data, start, end, display);
                var api = this.api(), data;

                // Remove the formatting to get integer data for summation
                var intVal = function ( i ) {
                    return typeof i === 'string' ?
                        i.replace(/[\$,]/g, '')*1 :
                        typeof i === 'number' ?
                            i : 0;
                };

                // Total over all searched pages
                var searchedTotal = api
                    .column( 1 , { 'search': 'applied' })
                    .data()
                    .reduce( function (a, b) {
                        return intVal(a) + intVal(b);
                    }, 0 );
    
                // Total over this page
                var pageTotal = api
                    .column( 1, { page: 'current'} )
                    .data()
                    .reduce( function (a, b) {
                        return intVal(a) + intVal(b);
                    }, 0 );
    
                // Update footer
                console.log('pageTotal = ', pageTotal);
                $( api.column( 1 ).footer().querySelector('.footer-callback-page') ).html(
                    '$' + pageTotal.toLocaleString()
                );
                $( api.column( 1 ).footer().querySelector('.footer-callback-search-page') ).html(
                    '$' + searchedTotal.toLocaleString()
                );
            }"
        ),
        "columns" => [
            "customerName" => [],
            "dollar_sales" => [
                "prefix" => "$",
                "footer" => "sum",
                "aggregates" => [
                    "totalCount" => ["count", "customerName"],                    
                    "avgSale" => ["avg", "dollar_sales"],
                ],
                "footerText" => "
                    Current page: <span class='footer-callback-page'></span>
                    <br>
                    Searched pages: <span class='footer-callback-search-page'></span>
                    <br>
                    All pages: @value 
                    <br>
                    Avg: @avgSale | Count: @totalCount",
            ],
        ],
        "showFooter"=>true,
        "themeBase"=>"bs4",
        "fastRender" => true,
        "searchMode" => "or"
    ));
    ?>
</div>
customerNameproductNameproductLineorderDateorderDayorderMonthorderYearorderQuarterdollar_sales
Online Diecast Creations Co. 1917 Grand Touring Sedan Vintage Cars 2003-01-06 00:00:00 6 1 2,003 1 4,080
Online Diecast Creations Co. 1911 Ford Town Car Vintage Cars 2003-01-06 00:00:00 6 1 2,003 1 2,755
Online Diecast Creations Co. 1932 Alfa Romeo 8C2300 Spider Sport Vintage Cars 2003-01-06 00:00:00 6 1 2,003 1 1,660
Online Diecast Creations Co. 1936 Mercedes Benz 500k Roadster Vintage Cars 2003-01-06 00:00:00 6 1 2,003 1 1,729
Blauer See Auto, Co. 1932 Model A Ford J-Coupe Vintage Cars 2003-01-09 00:00:00 9 1 2,003 1 2,702
Blauer See Auto, Co. 1928 Mercedes-Benz SSK Vintage Cars 2003-01-09 00:00:00 9 1 2,003 1 4,344
Blauer See Auto, Co. 1939 Chevrolet Deluxe Coupe Vintage Cars 2003-01-09 00:00:00 9 1 2,003 1 1,464
Blauer See Auto, Co. 1938 Cadillac V-16 Presidential Limousine Vintage Cars 2003-01-09 00:00:00 9 1 2,003 1 2,040
Vitachrome Inc. 1937 Lincoln Berline Vintage Cars 2003-01-10 00:00:00 10 1 2,003 1 3,726
Vitachrome Inc. 1936 Mercedes-Benz 500K Special Roadster Vintage Cars 2003-01-10 00:00:00 10 1 2,003 1 1,768

What People Are Saying

"KoolReport helps me very much in creating data report for my corporate! Keep up your good work!"
-- Alain Melsens

"The first use of your product. I was impressed by its easiness and powerfulness. This product is a great and amazing."
-- Dr. Lew Choy Onn

"Fantastic framework for reporting!"
-- Greg Schneider

Download KoolReport Get KoolReport Pro