KoolReport's Forum

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

How to update dashboard with a parameter #3290

Closed Eugene opened this topic on on May 17 - 7 comments

Eugene commented on May 17

Hi koolreport team!

I have been stuck with a simple issue but cannot find any solution because the logic of working the dashboard as a Whole application is a bit unclear for me.

So, I wish that the content of the dashboard can be updated depends on the parameter. Under the content I understand the set of widgets. For example I have 2 sets of wigets and I wish that one set can be visible if the parameter=1 and the second if the parameter=2.

Also I have a buttons that set the parameter. But this approach does not work. It looks like the content function is executed before I change the parameter with the button... Below is the part of my code

myBoard.php:

 protected function content()
    {

$Content1=[

//a lot of content1

Row::create([
                        MyButton1::create()->text('Content1')
                    ]),

]

$Content2=[

//a lot of content2

Row::create([
                        MyButton2::create()->text('Content2')
                    ]),

]

switch ($this->params("parameter")){
            case 1:
                $returnContent = $Content1;
                break;
            case 2:
                $returnContent = $Content2;
                break;

            default:
                $this->params(['parameter'=>1]);
                $returnContent = $Content1;
        }
        return $returnContent;

}


myButton1.php:

class myButton1t extends Button
{
    protected function actionSubmit($request, $response)
    {

        Client::dashboard('myBoard')->load(['parameter'=>2]);
    }
}

I tried to debug the code and noticed that the content function does not execute when I call load method. Perhabs I am wrong but anyway my approach does not work So I need help.

KoolReport commented on May 18

As I suggested from other topic, please try the FlexView to handle this case with flexible content. You can handle multiple content within a dashboard and change view whenever you need (with parameters)

Eugene commented on May 18

well I will try, but why this approach does not work

Why does the dashboard not load with the parameter?

Eugene commented on May 18

I think it could be very useful for everybody if you describe in your documentation or here the workflow of the Dashboard app. What function is executed in what order...

KoolReport commented on May 18

Hi Eugene,

It is because you use the Client command (refer to JS) within server-side action, which will not work. The correct way is like this:

MyButton1::create()
        ->text('Content1')
        ->onClick(Client::dashboard('myBoard')->load(['parameter'=>2]));

Please remove the actionSubmit() inside the MyButton1 class. This is first solution, fastest one, only make 1 round trip to server. I will tell you the second solution at last.

Whenever you use the "Client" class, it is actually generated JS to be run at client-side.

When you use the "action" like "actionSubmit()", you want to handle the event at server-side, under the hood dashboard will automatically attach the event to client-side to make ajax request to server and call the action like actionSubmit for you to handle.

In your case, the flow will be like this:

  1. Client-side request to load the dashboard with parameter.
  2. The parameter is available before content() is called.
  3. When the content() is done, all widgets available then action will be handled (to know which widget to handle action).

As you can see, if you handle things with "action", at that time, content is set already and it is not able to change content. It is come to second solution, the MyButton1 send a javascript to load content from client-side. In this way, you only need to return the Client script to client-side:

class myButton1t extends Button
{
    protected function actionSubmit($request, $response)
    {

        return Client::dashboard('myBoard')->load(['parameter'=>2]); // Return is important
    }
}

By this way, it will work but not efficient, as you can see, dashboard first make ajax request to server to called MyButton1 action, the action return a javascript to client, and client will load the dashboard with new parameter. It is 2 round trip to server.

I recommend the first solution.

I hope that I explain the flow of dashboard and how it handle client and server event.

If you have further question, please let me know.

Eugene commented on May 18

Ok. I think I understand. Is it correct to say that if my parameter is different depending on the current state of my app I have to use the second solution and can calculate the parameter value in actionSubmit function?

KoolReport commented on May 18

Yes. When the MyButton1 is clicked, if you need logic/data especially related to database or any information from server-side, definitely the second option is the way. The second option is good when you want to hide the logic or parameter. The option 1 is fine, but then you need to process and send parameters to client-side, as you know the client code can be viewed and may expose to exploitation for example.

Eugene commented on May 18

Thank you very much. Can you also help me with FileUploader? It is the ticket #3292

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