KoolReport's Forum

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

Easy way to Sum columns under a DataTable #2459

Closed paulo opened this topic on on Nov 24, 2021 - 4 comments

paulo commented on Nov 24, 2021

Hi, is there an easy way of sum columns to show total at the bottom from a Datatable ny changing the class only (not the view?) See example that I have which aligned a string to the right. I am trying to get a total for columns amount, fee and balance. thank you very much

    $this->src('mysql')
        ->query($query)
        ->params(array(
            ":processedStatus"=>$this->params["processed_status"],

        ))
        ->pipe(new Map(array(

            '{meta}' => function($metaData) {
                $metaData['columns']['CBNumber'] = array(
                    'type' => 'string',
                    'cssStyle' => 'text-align:right',

                );
                return $metaData;
            },
        )))
        ->pipe($this->dataStore("queryTours"));
Sebastian Morales commented on Nov 25, 2021

For DataTables, to sum a column at footer pls set columns' meta's footer and set "showFooter" => true like this:

DataTables::create(array(
    ...
    "showFooter" => true,
    "columns" => array(
        ...
        "amount" => array(
            ...
            "footer" => "sum",
            "footerText" => "Total: @value", // if "footerText" is empty, "@value" will be used
    )
));

Let us know how this works for you. Tks,

paulo commented on Nov 29, 2021

Thanks Sebastian, I converted to meta , it works, but I couldn't get to be right-aligned. An idea? thanks:

        ->pipe(new Map(array(
            '{meta}' => function($metaData) {
                $metaData['columns']['amount'] = array(
                    'cssStyle' => 'text-align:right',
                    'footer' => 'sum',
                    'footerText' => '@value',
                    "type"=>"number",
                    "decimals"=>2,
                );
                return $metaData;
            },
        )))
Sebastian Morales commented on Nov 30, 2021

DataTables currently only has "className" column meta. We will consider adding "cssStyle" in the next version. In the meantime you could set a column css class like this:

//MyReport.php
        ...
        ->pipe(new Map(array(
            '{meta}' => function($metaData) {
                $metaData['columns']['amount'] = array(
                    'className' => 'text-right',
                    ...
                );
                return $metaData;
            },
        ))) 

//MyReport.view.php
<style>
    .text-right {
        text-align:right
    }
</style>
paulo commented on Nov 30, 2021

thank you that worked! Paulo

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

None