KoolReport's Forum

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

Zero value to blank #2659

Closed Ron opened this topic on on May 5 - 10 comments

Ron commented on May 5

After SuperCube I get zero value in the max (class_id, professions_id) and sum (class_id) columns. instead of zero I need blank. I can't do it in the view code using formatValue function because I am using ComplexHeader = true.

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;
            },
            "{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;
            }
        )))
        ->pipe(new \koolreport\processes\RemoveColumn(array(
            '{{all}} | profession_id - max', '{{all}} | class_id - max', '{{all}} | total_hours - sum'
        )))
        ->pipe($this->dataStore("st"));
    }
Ron commented on May 9

?

Sebastian Morales commented on May 10

You can use the Map process again to convert 0 values to blank:

        ->pipe(new \koolreport\processes\Map(array(
            "{value}" => function($row) {
                foreach ($row as $colName => $colValue) {
                    if ($colValue == 0) $row[$colName] = "";
                }
                return $row;
            }
        ))) 
Ron commented on May 10

when I add this code to I get ErrorException: number_format() expects parameter 1 to be float, string given

Sebastian Morales commented on May 10

I see, column of type number expects numeric values instead of blank. You have 2 choices, either change column type to string or use "formatValue" in Table/DataTables to convert 0 to blank. I don't think complex header property affects column's formatValue.

Ron commented on May 10

If I need to change the column type in the DataTable it means I have to details all the list of columns in the columns property. is there a way to change the type of this column only without detailing all the other columns

Ron commented on May 10

Also please note that I am using

'complexHeaders' => true,
'headerSeparator' => ' | ',

So it is very complex to detail all the list of columns. I need to find another elegant way to remove the zeros

Sebastian Morales commented on May 10

Pls try to set "formatValue" for every column meta with Map like this:

        ->pipe(new \koolreport\processes\Map(array(
            "{meta}" => function($meta) {
                $colMetas = $meta["columns"];
                foreach ($colMetas as $colName => $colMetaValue) {
                    $colMetas[$colName]["formatValue"] = function($value) {
                        if ($value == 0) return "";
                        else return $value;
                    }
                }
                $meta["columns"] = $colMetas;
                return $meta;
            }
        )))
        ->pipe($this->dataStore("st"));
Ron commented on May 10

Your code gives error syntax error, unexpected '}'

Sebastian Morales commented on May 11

Pls add a semicolon like this:

        ->pipe(new \koolreport\processes\Map(array(
            "{meta}" => function($meta) {
                $colMetas = $meta["columns"];
                foreach ($colMetas as $colName => $colMetaValue) {
                    $colMetas[$colName]["formatValue"] = function($value) {
                        if ($value == 0) return "";
                        else return $value;
                    }; // add semicolon here
                }
                $meta["columns"] = $colMetas;
                return $meta;
            }
        )))
        ->pipe($this->dataStore("st"));
Ron commented on May 11

thanks works fine

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
None yet

None