KoolReport's Forum

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

Complex header with SuperCube #2632

Closed Ron opened this topic on on Apr 6, 2022 - 24 comments

Ron commented on Apr 6, 2022

Hi I ma using a SuperCube and Datatable widget. I have issues with the header

this is the report code

function setup()
    {
        $this->src('db')
        ->query('CALL getSplitRewardReportFrom(:year)')
        ->params(array(
            ":year"=>$this->params["year"],
        ))
        ->pipe(new SuperCube(array(
            "rows" => "teacher_id, id_number",
            "columns" => "hour_num",
            "max" => 'profession_id, class_id',
            "sum" => "total_hours"
        )))
        ->pipe(new \koolreport\processes\RemoveColumn(array(
            '{{all}}','{{others}}'
        )))
        ->pipe($this->dataStore("st"));
    }

And this is the view code

<?php
        DataTables::create(array(
            "name" => "myTable",
            "language" => "he", //use 'DataTables.de.json'
            "dataSource"=>$this->dataStore("st"),
            "emptyValue" => "", // use an empty string as filler for empty values
            "options"=>array(
                "colReorder"=>false,
                'autoWidth' => false,
                "order"=>[],
                'ordering' => false,
                'columnDefs' => array(
                    array(
        				'visible' => false,
        				//'targets' => [$this->monthDays+6], //hide the first column
        			)
        		),
            ),
            'complexHeaders' => true,
            'headerSeparator' => ' - ',
            "groupCellsInColumns" => [0, 1], // group similar cells in columns
            'cssClass'=>array(
                'table'=>'table table-bordered',
                'tr'=>'cssItem',
                'td'=>function($row,$colName)
                {
                    return in_array($colName, array('teacher_id','id_number')) ? 'border-dark text-right' : 'border-dark text-center';
                },
                'th'=>function($colName)
                {
                    return in_array($colName, array('teacher_id','id_number')) ? 'table-dark border-dark text-right' : 'table-dark border-dark text-center';
                },
            ),
        ));
        echo '<div class="footer print-only">'.lang('tts.print_footer').'</div>';
        ?>

This is the result header that I get

  1. how do I remove the {{all}} | profession_id column.
  2. How to I remove the hour_num top header that was generated.
  3. How to I update a label of a column like 1 | profession_id and the one in the bottom called max?

Ron commented on Apr 7, 2022

?

Sebastian Morales commented on Apr 8, 2022

You can disable "complexHeaders" property to see the exact column names of the datastore. Then apply Map process to remove/rename column names like this:

        ->pipe(new SuperCube(array(
            "rows" => "teacher_id, id_number",
            "columns" => "hour_num",
            "max" => 'profession_id, class_id',
            "sum" => "total_hours"
        )))
        ->pipe(new \koolreport\processes\Map([
            "{value}" => function($row) {
                $colNameAll = ...; // choose the all colNames to remove here
                unset($row[$colNameAll]);
                foreach ($row as $colName => $value) {                    
                    $newColName = removeHourPart($colName); //write your code to remove the hour part of colName here
                    $row[$newColName] = $row[$colName];
                    unset($row[$colName]);
                }        
                return $row;
            },
            "{meta}" => function($meta) {
                $colName = ...; // set the colName you want to set label for
                $meta["columns"][$colName] = ["label" => $colLabel];
                return $meta;
            }
        ]))
Ron commented on Apr 24, 2022

Hello I need complex headers. the top row is hour number 1 to 7 in each hour there are three columns class_id, profession_id and total_hours

like this:

Sebastian Morales commented on Apr 25, 2022

What's your question for the current complex header?

Ron commented on Apr 26, 2022

I need to output to look like this

and not like this

Ron commented on Apr 28, 2022

?

Sebastian Morales commented on Apr 28, 2022

After the SuperCube process, you have to change your column names to "1 - total_hours", "1 - profession_id", "1 - class_id", "2...", ... Then apply comlex header in DataTables.

Ron commented on Apr 28, 2022

After Applying SuperCube I am getting a header with three headers like this:

My question is how do I change the header to look like I need. I need to apply rowsapn on the header in the top level while on the bottom it will say split like this

Sebastian Morales commented on Apr 28, 2022

Did you read my answer on changing column names after SuperCube?

Ron commented on Apr 28, 2022

Yes. I did not manage solving the problem with this solution: 1. I don't know how to make a rowspan of a column on the code. 2. how can I remove the top column hour_num generated by the SuperCube

Ron commented on Apr 28, 2022

When using

complexheader = true 
'headerSeparator' => ' | ',

the following code does not change the labels of the $meta

->pipe(new \koolreport\processes\Map(array(
            "{meta}" => function($meta) {
                $colMetas = $meta["columns"];
                foreach ($colMetas as $colKey => $colMeta) {
                    if (strpos($colKey, 'hour_num')!== false) {
                        $colMetas[$colKey]['label'] = rand();
                    }
                    // = lang("tts.$colKey"); //for column 'hour_num'
                }
                $meta["columns"] = $colMetas;
                return $meta;
            }
        )))

if I set complexheader = false the above code is working!

how to change the label of the header if I set complex header true?

Ron commented on May 1, 2022

Any updates?

Ron commented on May 2, 2022

?

Sebastian Morales commented on May 2, 2022

You need to change the column names to remove their "hour_num | " part at the beginning. For example:

->pipe(new SuperCube(...))
->pipe(new \koolreport\processes\Map(array(
            "{value}" => function($row) {
                foreach ($row as $colName => $colValue) {
                    if (substr( $colName, 0, 11 ) === "hour_num | ")) {
                        $newColName = substr($colName, 11);
                        $row[$newColName] = $row[$colName];
                        unset($row[$colName]);
                    }
                }
                return $row;
            }
        ))) 

Always remember to print out your datastore's data to see its column names. Rgds,

Ron commented on May 2, 2022

IT does not matter what I ma trying to do in the code, right after the SuperCube process the table column names does not change. you code is not working in this case.

This is the code of the

function setup()
    {
        $this->src('db')
        ->query('CALL getSplitRewardReportFrom(:year)')
        ->params(array(
            ":year"=>$this->params["year"],
        ))
        ->pipe(new SuperCube(array(
            "rows" => "teacher_id, id_number",
            "columns" => "hour_num",
            "max" => 'profession_id, class_id',
            "sum" => "total_hours"
        )))
        ->pipe(new \koolreport\processes\Map(array(
            "{value}" => function($row) {
                foreach ($row as $colName => $colValue) {
                    if (substr( $colName, 0, 11 ) === "hour_num | ") {
                        $newColName = substr($colName, 11);
                        $row[$newColName] = $row[$colName];
                        unset($row[$colName]);
                    }
                }
                return $row;
            }
        )))
        ->pipe(new \koolreport\processes\RemoveColumn(array(
            'hour_num - {{all}} | profession_id - max','hour_num - {{all}} | class_id - max','hour_num - {{all}} | total_hours - sum'
        )))
        ->pipe($this->dataStore("st"));
    }

The column names remains the same "hour_num - 1", "hour_num - 2" etc.

Ron commented on May 3, 2022

?

Sebastian Morales commented on May 3, 2022

We probably have to remove the column names from meta as well:

        ->pipe(new \koolreport\processes\Map(array(
            "{value}" => function($row) {
                foreach ($row as $colName => $colValue) {
                    if (substr( $colName, 0, 11 ) === "hour_num | ") {
                        $newColName = substr($colName, 11);
                        $row[$newColName] = $row[$colName];
                        unset($row[$colName]);
                    }
                }
                return $row;
            },
            "{meta}" => function($meta) {
                $colMetas = $meta["columns"];
                foreach ($colMetas as $colName => $colMetaValue) {
                    if (substr( $colName, 0, 11 ) === "hour_num | ") {
                        $newColName = substr($colName, 11);
                        $colMetas[$newColName] = $colMetas[$colName];
                        unset($colMetas[$colName]);
                    }
                } 
                $meta["columns"] = $colMetas;
                return $meta;               
            }
        ))) 
Ron commented on May 5, 2022

ok it seems to work and this is the list of columns I get after the Map process

string(10) "teacher_id"
string(9) "id_number"
string(23) "1 | profession_id - max"
string(23) "2 | profession_id - max"
string(23) "3 | profession_id - max"
string(23) "4 | profession_id - max"
string(23) "5 | profession_id - max"
string(23) "6 | profession_id - max"
string(23) "7 | profession_id - max"
string(18) "1 | class_id - max"
string(18) "2 | class_id - max"
string(18) "3 | class_id - max"
string(18) "4 | class_id - max"
string(18) "5 | class_id - max"
string(18) "6 | class_id - max"
string(18) "7 | class_id - max"
string(21) "1 | total_hours - sum"
string(21) "2 | total_hours - sum"
string(21) "3 | total_hours - sum"
string(21) "4 | total_hours - sum"
string(21) "5 | total_hours - sum"
string(21) "6 | total_hours - sum"

I am trying to change the label of the columns using the method

$colMetas[$newColName] ["label"]= lang($newColName);

but it is not working. the labels remain the same!

Ron commented on May 9, 2022

?

Sebastian Morales commented on May 9, 2022

Oh I see, DataTables' complex header doesn't work with "label" yet. So far it can only use columns' real name to build the complex header. Rgds,

Ron commented on May 9, 2022

I can not use the real columns names because the SuperCube process changes it by adding Sum, Max etc. Beside that can I do ComplexHeader in Table Widget?

Sebastian Morales commented on May 10, 2022

Ok, how about removing the " - sum" and " - max" parts from column names as well:

        ->pipe(new \koolreport\processes\Map(array(
            "{value}" => function($row) {
                foreach ($row as $colName => $colValue) {
                    if (substr( $colName, 0, 11 ) === "hour_num | ") {
                        $newColName = substr($colName, 11);
                        $newColName = str_replace(" - sum", "", $newColName);
                        $newColName = str_replace(" - max", "", $newColName);
                        $row[$newColName] = $row[$colName];
                        unset($row[$colName]);
                    }
                }
                return $row;
            },
            "{meta}" => function($meta) {
                $colMetas = $meta["columns"];
                foreach ($colMetas as $colName => $colMetaValue) {
                    if (substr( $colName, 0, 11 ) === "hour_num | ") {
                        $newColName = substr($colName, 11);
                        $newColName = str_replace(" - sum", "", $newColName);
                        $newColName = str_replace(" - max", "", $newColName);
                        $colMetas[$newColName] = $colMetas[$colName];
                        unset($colMetas[$colName]);
                    }
                } 
                $meta["columns"] = $colMetas;
                return $meta;               
            }
        ))) 
Ron commented on May 10, 2022

You should make DataTable support label change very fast because in most reports I use DataTable and I can't use the real column names in the stored procedure because it is multi language! please add it in the next version and let me know!

Sebastian Morales commented on May 10, 2022

You meant complex header support for column label instead of column name? We will seriously consider it and find a suitable solution. Tks,

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
bug

None