KoolReport's Forum

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

Multiselect Inputs - default dynamically generated items as selected #325

Open Keith Burke opened this topic on on Jun 15, 2018 - 4 comments

Keith Burke commented on Jun 15, 2018

Hi,

Is there a way of setting some or all items as pre-selected as opposed to nothing selected in Multi BSelect or MultiSelect or similar?

I understand that you can set a default value etc but I don't always know the values that are going to be populated form the database.

KoolReport commented on Jun 16, 2018

It is possible. Let me write some example code, test it before sending to you.

KoolReport commented on Jun 16, 2018

Here come the code, let say we have an multiple select name "option" and here is the code to make the multiselec auto select the first value in the first loaded. As you may see in the defaultParamValues() function, we try to connect to database, query all possible options and return the selected one.

As you may notice the function requestDataSending() at the end. This function force the datapipe start to query and push data from the source. That why after this function, we can get data from $this->dataStore("options").

Here come the code:

    protected function defaultParamValues()
    {
        $this->src("mysource")
        ->query("SELECT name,value FROM tbl_options")
        ->pipe($this->dataStore("options"))
        ->requestDataSending();

        $data = $this->dataStore("options")->data();

        //Now you have $data containing list of all possible values, you can set default selection
        //base on $data.

        return array(
            "option"=>array($data[0]["value"]), // Select first item.
        );
    }

    protected function bindParamsToInputs()
    {
        return array(
            "option",
        );
    }
Keith Burke commented on Jun 18, 2018

Nice one. That worked perfectly. To expand on that, to automatically select all...

        $this->src("mysource")
        ->query("SELECT name,value FROM tbl_options")
        ->pipe($this->dataStore("options"))
        ->requestDataSending();

        $data = $this->dataStore("options")->data();

        //Now you have $data containing list of all possible values, you can set default selection
        //base on $data.

        $optionsList = array();
        foreach ($data as $item)
            $optionsList [] = $item["value"];

        return array(
            "optionMultiSelect"=>optionsList , // Select all items.
        );
KoolReport commented on Jun 18, 2018

That's perfect!

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
solved

Inputs