KoolReport's Forum

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

Datatables total error #2419

Open saiful opened this topic on on Nov 5, 2021 - 4 comments

saiful commented on Nov 5, 2021

hi, i want to add total under my datatables report, but im getting error undefined index total. here is my code:

->pipe(new \koolreport\processes\Map(array(
            "{value}"=>function($row, $meta, $index, $mapState)
                {
                    $total = isset($mapState['Total'])?$mapState['Total']:[];
                    if(!isset($total['Total']))
                    {
                        $total['Total'] = 0;
                    }
                    $total['Total'] += 1*$row['Total'];           // -> here is the error line
                    $mapState['Total'] = $total;
                    return ['{rows}' => $row, '{state}' => $mapState];
                },
            "{end}" => function($count, $mapState)
                {
                    $total = $mapState['Total'];
                    return [$total];
                }
        )))
Sebastian Morales commented on Nov 5, 2021

$mapState["total"] should be your total row, i.e an array of scalar values, not a scalar value itself. And certainly your $row data doesn't have a "total" column. Overall, the code should be:



->pipe(new \koolreport\processes\Map(array(
            "{value}"=>function($row, $meta, $index, $mapState)
                {
                    $totalRow = isset($mapState['TotalRow'])?$mapState['TotalRow']:[]; //change 'Total" to "TotalRow" for clearer meaning
                    $totalColumnNames = ["Sales", "Tax", ...]; // choose which columns to do total
                    foreach ($row as $column => $value) { // loop through the row to do total for chosen columns
                        if (in_array($column, $totalColumnNames) {
                            if (!isset($totalRow[$column])) $totalRow[$column] = 0;
                            $totalRow[$column] += 1 * $value; // or 1 * $row[$column]
                        }
                    }
                    $mapState['TotalRow'] = $totalRow;
                    return ['{rows}' => $row, '{state}' => $mapState];
                },
            "{end}" => function($count, $mapState)
                {
                    $total = $mapState['TotalRow'];
                    return [$total];
                }
        )))
 
saiful commented on Nov 8, 2021

thanks, it worked properly. but how to add "Total" text on the left side of the footer? here is my code in the view page:

"options" => array(
              "footerCallback" => "function ( 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 pages
                total = api
                    .column( 4 )
                    .data()
                    .reduce( function (a, b) {
                        return intVal(a) + intVal(b);
                    }, 0 );
     
                // Total over this page
                pageTotal = api
                    .column( 4, { page: 'current'} )
                    .data()
                    .reduce( function (a, b) {
                        return intVal(a) + intVal(b);
                    }, 0 );
     
                // Update footer
                $( api.column( 4 ).footer() ).html(
                    '$'+pageTotal +' ( $'+ total +' total)'
                );"

now it only show " - " symbol

Sebastian Morales commented on Nov 9, 2021

Those column footers are probably null values because they are not from your total columns. Pls replace these lines

->pipe(new \koolreport\processes\Map(array(
                        ...
                        if (in_array($column, $totalColumnNames) {
                            if (!isset($totalRow[$column])) $totalRow[$column] = 0;
                            $totalRow[$column] += 1 * $value; // or 1 * $row[$column]
                        }

with these ones:

->pipe(new \koolreport\processes\Map(array(
                        ...
                        if (in_array($column, $totalColumnNames) {
                            if (!isset($totalRow[$column])) $totalRow[$column] = 0;
                            $totalRow[$column] += 1 * $value; // or 1 * $row[$column]
                        } else {
                            $totalRow[$column] = ""; // if not a total column, set footer value to empty string or any value you want
                        }
saiful commented on Nov 10, 2021

it works! thank you very much

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
solved

DataGrid