KoolReport's Forum

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

Select2 empty value #638

Closed Eugene opened this topic on on Jan 30, 2019 - 8 comments

Eugene commented on Jan 30, 2019

Hi,

I can not find a solution for this case:

I have a Select2 on my page. The plan was to use it like a filter - show in the report only one item but also I wanted to keep an option to get the report with all items using the empty "" value.

  • Select2 code:
Select2::create(array(
                        "name" => "itemSearchSelect",

                        "dataStore" => $this->dataStore("items"),
                        "dataBind" => array(
                            "text" => "name",
                            "value" => "id",
                        ),
                        "options" => array(
                            "placeholder" => "Select item to search",
                            "allowClear" => true,
                        ),
                    ));
  • Also i set the default value like this:
protected function defaultParamValues()
    {
          return array(
            "itemSearch" =>array(),
        );
    }

    protected function bindParamsToInputs()
    {
        return array(
            "itemSearch" =>"itemSearchSelect",
        );
    }
  • The problem is: If I open the page the first time - I get the correct report with all items - so the default array() works. But in the Select2 I see the first value from the list (actually I expect to see placeholder there). So if I press reload button Select2 works like a filter but I don't need it every time because I have some other inputs and sometimes I need having this Select2 empty. The allowClear option helps just partially - yes I can erase the value using it but I don't want to press it every time before press reload.

Regards, Eugene

KoolReport commented on Jan 30, 2019

I have not understood what you want. You need Select2 to auto-clear itself on everytime the page post back?

Eugene commented on Jan 31, 2019

I want

*. to have empty value on the first load of the page

*. and the Select 2 must keep it until I change it.

*. if i erase the value, Select 2 must remember it.

Now it uses the first value in the list as default

KoolReport commented on Jan 31, 2019

Normally I will do:

Select2::create(array(
    ...
    "defaultOption"=>array("--Select an option"=>"")
))

By this way, you may not see the placeholder however you can clear your selection by select the default option.

Eugene commented on Jan 31, 2019

Thanks, I solved it with the dataStore('items')->prepend(array("id"=>"","name"=>"")); for the list

but now i met another problem. I try to follow forum examples but no success

I have very long sql query this is only the part

protected function defaultParamValues()
    {
            return array(
            "categoriesToSelect"=>$optionsList, // Select all items.
           "itemSearch" =>array("id"=>""),
        );
    }

    protected function bindParamsToInputs()
    {
        return array(
            "categoriesToSelect" =>"categoriesList",
            "active" =>"activeCheckbox",
            "itemSearch" =>"itemSearchSelect",
        );
    }
$this->src('quinos')
            ->query($query)
            ->params(array(
               ":categories" => $this->params["categoriesToSelect"],
                ":active" => $active,
                ":item" => $this->params["itemSearch"][0],
            ))
and recipe.active = :active ' . (($this->params["itemSearch"][0]!="") ? " AND recipe.id IN (:item)" : "") . ' order by recipe.category_id, recipe.name

I get the empty table. even like this


and recipe.active = :active order by recipe.category_id, recipe.name

but if i comment ":item" => $this->params["itemSearch"][0], the report works.

I don't understand the reason and why the report doesn't work even there is no :item parameters in the query.

KoolReport commented on Jan 31, 2019

The error is generated from the standard PDO as it does not find the :item in your query,

Solution is that you make an $param array first, then add the :item if necessary:

$params =array();

if(item condition available)
{
    $params[":item"] = $this->params["itemSearch"][0];
}
Eugene commented on Jan 31, 2019

I am not sure I understood you correctly What I need

  1. If params["itemSearch"][0] not empty (means something is chosen in Select2) - i have to include the following in my query AND recipe.id IN (:item)

  2. if it is empty I have nothing to include

I am a bit confused about where to put your code and what must be in the query?

Eugene commented on Jan 31, 2019

I think I understood what you mean Thanks... i am not sure till the end but it works :-)

Eugene commented on Jan 31, 2019

This is my code maybe it could be helpful for somebody

protected function setup(){
$query = 'select ....
....... where  ..... categ.department_id IN (2,5)' . (($this->params["itemSearch"] != "") ? " and recipe.id in (:item)" : "") . ' and recipe.category_id in (:categories) ........ ';

 $query_params = array();
        if ($this->params["itemSearch"] != "") {
            $query_params[":item"] = $this->params["itemSearch"];
        }
        $query_params[":active"] = ($this->params["active"][0] == '1') ? false : true;
        $query_params[":categories"] = $this->params["categoriesToSelect"];

$this->src('quinos')
            ->query($query)
            ->params($query_params)
...

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
solved

Inputs