KoolReport's Forum

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

Looped Processes ColumnRename #2522

Open George opened this topic on on Jan 6, 2022 - 1 comments

George commented on Jan 6, 2022

Hi,

Is it possible to perform a custom function to rename every column in my datatable? I know you can use ColumnRename in Processes like: ``
public function setup()

{
    ...
    ->pipe(new ColumnRename(array(
        "column0"=>"id",
        "column1"=>"name"
    )))
    ...
}```

However I cannot use this because my columns are dynamic and do not know the column names before hand.

I want to be able to do something like:

        ->pipe(new ColumnRename(function($columnNames){
            //loop through each column name and do something
            ...
            return $columnNames;
        }))

As always thanks for your help.

Sebastian Morales commented on Jan 7, 2022

You can try the Map process:

        ->pipe(new \koolreport\processes\Map(array(
            "{value}" => function($row) { // loop through all data rows
                $row["id"] = $row["column0"];// add column "id" which equals to "column0"
                unset($row["column0"]); // delete column "column0";
                ...
                return $row;
            }
        )))

Here's its docs:

https://www.koolreport.com/docs/processes/map/

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