Code Rules

Overview #

There are some code patterns that you see a lot in Dashboard. Understand this code pattern will help you to be more productive in coding.

Get Set #

Many Dashboard components implement TProps trait which allows those components to have this get and set code rule. You can set the properties by assign value into first parameter, for example we have hidden property, you can set its property like following

$object->hidden(true);

The hidden will be set to true, the method will return $object so that you can continue to set another property:

$object->hidden(true)->width(1/2)->...->cssClass("bold");

To retrieve the value from property, you simply call method without parameter:

$isHidden = $object->hidden(); //Return true, because we set it to true earlier.

Example:

$object
    ->hidden(true)
    ->width(1/2)
    ->cssClass("bold");

echo $object->hidden(); //true
echo $object->width(); //1/2
echo $object->cssClass(); //bold

Property defined with anonymous function #

In many cases, we would like to set a different value for a property based on a condition. To make thing simpler, property of an object inside Dashboard could be set by a anonymous function.

Example:

$myWidget->hidden(function(){
    if($this->app()->user()->hasRole("admin")===true) {
        return true;
    }
    return false;
});

Widget properties #

Widget's properties accepts anonymous function with no parameters. The anonymous function can refer to the widget object with $this.


use \koolreport\dashboard\Dashboard;

class MyDashboard extends Dashboard
{
    protected function content()
    {
        return [
            MyColumnChart::create()
                ->hidden(function(){
                    if($this->app()->user()->hasRole("admin")===true) {
                        return true;
                    }
                    return false;
                })
        ];
    }
}

Field properties #

Field's properties accepts anonymous function with two parameters $value and $row.

use \koolreport\dashboard\widgets\Table;

class MyTable extends Table
{
    protected function fields()
    {
        return [
            ...,
            Badge::create("status")->label("Badge")
            ->type(function($value, $row){
                if($status==="Completed") {
                    return "success";
                }
                return "danger";
            }),
        ];
    }
}

In above example, we apply the anonymous function for type property of Badge field in Table. We can do the same with other properties of Field.

Get started with KoolReport

KoolReport will help you to construct good php data report by gathering your data from multiple sources, transforming them into valuable insights, and finally visualizing them in stunning charts and graphs.