KoolReport's Forum

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

Multiselect Or Select2 with default select ALL #2623

Open cfsinc opened this topic on on Mar 30, 2022 - 13 comments

cfsinc commented on Mar 30, 2022

Hello Team,

I have been stuck for a couple of days trying to use Select2, Multiselect, BSelect, Etc with the default of ALL the data in the column.

Normally when I use select option I do a WHERE LIKE :selection in my SQL so I can have % as default and it will select everything by default unless I select something specific in the drop down.

I have seen examples with WHERE IN :selection but I need to still have the default be ALL

Below is examples of my Report and it seems everything I try fails either on report open for selecting all or when I try and select multiple options after the report shows all

event_list produces the list of options in the select dropdown and event_name is what I insert in MySQL query for results after the event is selected in the drop down. If the user does not select anything all data is shown.

SigEventReport.php SigEventReport.view.php

Again I have no problem with Select Option but when I try to implement any Select2 or other multiselect I can not get it to work as select all by default or select multiple options manually.

As always if you can help me with this I will gladly give a Tip!!! Here is examples of the last way I have tried to do it.

SigEventReport.view.php

   /**
     * @return array[]
     */
    protected function defaultParamValues(): array
    {
        return array(
            "dateRange",
            "user_name"=>"%",
            "event_name"=>array(),
            //"multiSelect"=>$allMultiSelectData,
        );
    }

    /**
     * @return string[]
     */
    protected function bindParamsToInputs(): array
    {
        return array(
            "dateRange"=>"dateRange",
            "user_name",
            "event_name"=>"event_name",
            "loadData",
        );
    }


    $sql_event_list = "
                    SELECT
                    a.activity_type_field AS event
                    FROM activities a
                    -- WHERE a.activity_type_field is not null
                    GROUP BY a.activity_type_field
    ";

    //event list for sig event report
    $this->src('lp3')->query($sql_event_list)
    ->pipe($this->dataStore("event_list"));

    $sql_sig_event_data = "
                        SELECT
                        a.activity_date AS Date,
                        u.user_name AS Emp,
                        a.activity_type_field AS Event,
                        a.activity_log Description 
                        FROM activities a
                        LEFT JOIN users u ON u.user_id = a.activity_user_id
                        WHERE activity_date > :start
                        AND activity_date < :end
                        AND u.user_name LIKE :emp
                        
                        // ****************************************************************
                        // Normally I have AND a.activity_type LIKE :event
                        // I need this to show all events by default unless I am choosing one or more events
                        AND a.activity_type_field IN :event

                        AND a.activity_status != 12
    ";

    // pipe table data to sig event report UI
    $this->src('lp3')->query($sql_sig_event_data)
        ->params(array(
            ":start" => $this->params["dateRange"][0],
            ":end" => $this->params["dateRange"][1],
            ":emp" => $this->params["user_name"],
            ":event" => $this->params["event_name"],
    ))
    ->pipe($this->dataStore("sig_event_report"));

//*****************************************************************
sigEventReport.php

    <?php
        // Select::create(array(
        // "name"=>"event_name",
        // //"placeholder"=>"Event Name",
        // "multiple"=>true,
        // "dataStore"=>$this->dataStore("event_list"),
        // "defaultOption"=>array("All"=>"%"),
        // "dataBind"=>array(
        //     "text"=>"event",
        //     "value"=>"event",
        // ),
        // "attributes"=>array(
        //     "class"=>"form-control",
        //     "style"=>"margin-top:10px;width: 300px;padding: 0;font-size: 1em;"
        // )
        // ));
    
        Select2::create(array(
        "name"=>"event_name",
        //"placeholder"=>"Event Name",
        "multiple"=>true,
        "dataStore"=>$this->dataStore("event_list"),
        "defaultOption"=>array("All"=>"%"),
        "dataBind"=>array(
            "text"=>"event",
            "value"=>"event",
        ),
        "attributes"=>array(
            "class"=>"form-control",
            "style"=>"margin-top:10px;width: 300px;padding: 0;font-size: 1em;"
        )
        ));
    

        // MultiSelect::create(array(
        // "name"=>"event_name",
        // //"placeholder"=>"Event Name",
        // "multiple"=>true,
        // "dataStore"=>$this->dataStore("event_list"),
        // //"defaultOption"=>array("All"=>"%"),
        // "dataBind"=>array(
        //     "text"=>"event",
        //     "value"=>"event",
        // ),
        // "attributes"=>array(
        //     "class"=>"form-control",
        //     "style"=>"margin-top:10px;width: 300px;padding: 0;font-size: 1em;"
        // )
        // ));
        
        
        // BSelect::create(array(
        //     "name"=>"event_name",
        //     "multiple"=>true,
        //     "dataStore"=>$this->dataStore("event_list"),
        //     //"defaultOption"=>array("All"=>"%"),
        //     "dataBind"=>array(
        //         "text"=>"event",
        //         "value"=>"event",
        //     ),
        //     'options' => array(
        //         'numberDisplayed' => 5,
        //         'includeSelectAllOption' => true,
        //         'includeResetOption' => true,
        //     ),
        //     "attributes"=>array(
        //         "class"=>"form-control",
        //         "style"=>"margin-top:10px;width: 300px;padding: 0;font-size: 1em;"
        //     )
        // ));

cfsinc commented on Mar 30, 2022

cfsinc commented on Mar 30, 2022

I have tired various topics that are similar to what im trying to do but I still cant get it to work

https://www.koolreport.com/forum/topics/306
https://www.koolreport.com/forum/topics/2429
https://www.koolreport.com/examples/reports/inputs/intro/
https://www.koolreport.com/forum/topics/1234
cfsinc commented on Mar 31, 2022

Please help me with this. I will pay for the help. Thank you

Sebastian Morales commented on Apr 1, 2022

Hi, pls try to set the default value of "event_name" to array("All") (using an array because it's a multiple select) and build your sql query like this:

    protected function defaultParamValues(): array
    {
        return array(
            "dateRange",
            "user_name"=>"%",
            "event_name"=>array("All"), // set event_name default value to be array("All")
            //"multiSelect"=>$allMultiSelectData,
        );
    }

    protected function bindParamsToInputs(): array
    {
        return array(
            "dateRange"=>"dateRange",
            "user_name",
            "event_name"=>"event_name",
            "loadData",
        );
    }


    $sql_event_list = "
                    SELECT
                    a.activity_type_field AS event
                    FROM activities a
                    -- WHERE a.activity_type_field is not null
                    GROUP BY a.activity_type_field
    ";

    //event list for sig event report
    $this->src('lp3')->query($sql_event_list)
    ->pipe($this->dataStore("event_list"));

    $eventWhere = in_array("All", $this->params["event_name"]) ?
        "" : "AND a.activity_type_field IN :event"; // only add event where condition if its value doesn't contain "All"
    $sql_sig_event_data = "
                        SELECT
                        a.activity_date AS Date,
                        u.user_name AS Emp,
                        a.activity_type_field AS Event,
                        a.activity_log Description 
                        FROM activities a
                        LEFT JOIN users u ON u.user_id = a.activity_user_id
                        WHERE activity_date > :start
                        AND activity_date < :end
                        AND u.user_name LIKE :emp
                        
                        // ****************************************************************
                        // Normally I have AND a.activity_type LIKE :event
                        // I need this to show all events by default unless I am choosing one or more events
                        $eventWhere

                        AND a.activity_status != 12
    ";

    $params = array(
            ":start" => $this->params["dateRange"][0],
            ":end" => $this->params["dateRange"][1],
            ":emp" => $this->params["user_name"],
    );    
    if (!in_array("All", $this->params["event_name"])) $params[":event"] = $this->params["event_name"];

    // pipe table data to sig event report UI
    $this->src('lp3')->query($sql_sig_event_data)
        ->params($params)
    ->pipe($this->dataStore("sig_event_report")); 

If this doesn't work for you or you want another default behavior pls let us know. Tks,

cfsinc commented on Apr 4, 2022

Ok it does not work. When you load the page and select run it does give all events in the table but if you try and select an individual event SQL failure like I been getting.

Here is the code for sigEventReport.php and sigEventReport.view.php currently. also screen shots. it works for all but not 1 or more selections of the event list.

cfsinc commented on Apr 4, 2022

Sebastian Morales commented on Apr 5, 2022

Ok, in your report's setup pls replace this string:

    AND a.activity_type_field IN :event

with this one:

    AND a.activity_type_field IN (:event)

Let us know if it works for you or not. Tks,

cfsinc commented on Apr 5, 2022

Absolutely Incredible!!!

Thank you so very much!!! Works perfectly!!! I am so thankful for the support you give me every single time!!

cfsinc commented on Apr 5, 2022

I sent you guys a tip for the help. Thank You! Thank You! Thank You!

KoolReport commented on Apr 5, 2022

Hi Damien, Karl is here. On behalf of the support team, I would like to thank you for your generous support tips. Very appreciated!

cfsinc commented on Apr 8, 2022

Team,

I have one additional problem maybe you can help me with quickly?

Everything is working when you pull up the report and by default if you dont change the Event it will show all events.

If you select 1 or more events it will show the selected events.

If you deselect all events that were manually selected and run the report at this point, it will error.

It seems once you select an event and then deselect the event and there are no events selected, its no longer defaults to all. What do I need to change so that it will default back to all at this point?

KoolReport commented on Apr 10, 2022

Hi Damien, I am Peter from sale team, We were in a national holiday so there is a short delay in support, I will find you a supporter as soon as possible.

Sebastian Morales commented on Apr 10, 2022

Oh, sorry I forgot the edge case of an empty list of events. Pls add the following line to the beginning of your report's setup:

function setup()
{
    if (empty($this->params['event_name'])) $this->params['event_name'] = array("All");
    ...

Pls try this and let us know if there's any issue. Tks,

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

Inputs