KoolReport's Forum

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

Dashboard: Table - Icon #2063

Closed Andre Van Der Walt opened this topic on on May 10, 2021 - 1 comments

Andre Van Der Walt commented on May 10, 2021

Hi,

Please advise how to reference a Calculated::field in Table using Icon. The following summarized example do not return anything:

Calculated::create("calculatedField",function($row){
  // some operations
}
->label(""),
Icon::create()
                ->label("")
                ->icon(function($row) {
                    if ($row["calculatedField"] == "Low Performer"){
                        return "fa fa-exclamation-triangle";
                    }
                    elseif ($row["calculatedField"] == "Average Performer"){
                        return "fa fa-balance-scale";
                    }
                    elseif ($row["calculatedField"] == "Strong Performer"){
                        return "fa fa-dumbbell";
                    }
                    elseif ($row["calculatedField"] == "Top Performer"){
                        return "fa fa-medal";
                    }
                })->cssStyle("font-size:28px"),

Thank you!

KoolReport commented on May 10, 2021

There are two problems:

  1. The form of anynomous function is icon(function($value,$row) {...}). So you the $row is in second parameter.
  2. The $row will return the original data only so "calclatedField" is not available. You need to re-calculated inside the icon(). or better you create a stateless calculated function inside table:
class MyTable extends Table
{

    public function calculateField($row) {
        ... You calculation
    }
}

and then you can do:

Calculated::create("calculatedField",function($row){
    return $this->calculateField($row);
}
->label(""),
Icon::create()
                ->label("")
                ->icon(function($value, $row) {
                    $calculatedField = $this->calculateField($row);
                    if ($calculatedField == "Low Performer"){
                        return "fa fa-exclamation-triangle";
                    }
                    elseif ($calculatedField == "Average Performer"){
                        return "fa fa-balance-scale";
                    }
                    elseif ($calculatedField == "Strong Performer"){
                        return "fa fa-dumbbell";
                    }
                    elseif ($calculatedField == "Top Performer"){
                        return "fa fa-medal";
                    }
                })->cssStyle("font-size:28px"),

Hope that helps

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

Dashboard