KoolReport's Forum

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

How to display default values in the SELECT2 box? #2429

Open Dev opened this topic on on Nov 12, 2021 - 7 comments

Dev commented on Nov 12, 2021

I want to get some default values whenever select2 box loads just like in the image, Is there a way to do this? Then when I click on the box the dropdown should show as usual with other values.

I tried by giving multiselect

Select2::create(array(
                    'name'=>"roles",
                    "multiple"=>true,
                    "placeholder"=>"Select roles",
                    "multiSelect"=>array("default1","default2","default3"),
                    "data"=>$this->params()["adminRoles"],
                    "dataBind"=>array("value"=>"role_id",
                    "text"=>"roles"
                  ),

But its not showing

Sebastian Morales commented on Nov 15, 2021

The best way to do it is in function defaultParamValues() in your report setup like this:

class MyReport extends KoolReport
{
    use \koolreport\inputs\Bindable;
    use \koolreport\inputs\POSTBinding; // use POSTBinding if your form method is "post", GETBinding if your form method is "get"

    protected function defaultParamValues() 
    {
        return array(
            "roles" => array("default1","default2","default3"), // default value for "roles" select2 input
            ... // default values for other inputs
        );
    }
Dev commented on Nov 15, 2021

I tried but its not working, I still cant see default options when page opens/loads.

Here is my code

This is my user.php file

class UserMgmt extends CustomBoard
{
   use \koolreport\inputs\Bindable;
   use \koolreport\inputs\POSTBinding;
  
  protected function defaultParamValues()
    {
      return array(
        "roles" => array("John Doe"=>"33","Doe"=>"11","John"=>"12",),
      );
    }
  protected function bindParamsToInputs()
    {
      return array(
        "roles"=>"roles",
      );
    }

   protected function drawRoles($request, $response, $msg = null, $status = null)
    {

      $defaults= [];             ///It contains data fetched from DB as key value pair

     $this->renderView(
        "AddUser",
      ["name" => $request->params()['name'],
       "email"=> $request->params()['email'],
       "adminRoles" => $defaults
      ]
    );
    }

This is my AddUser.view.php file

<?php
                Select2::create(array(
                    "name" => "roles",
                    "id"   => "roles",
                    "multiple"=>true,
                    "placeholder"=>"Select roles",
                    "data"=>$this->params()["adminRoles"],
                    "dataBind"=>array(
                      "value"=>"role_id",
                      "text"=>"roles"
                      ),
            	"attributes"=>array(
                "class"=>"form-control",
            	  // "size"=>5,
                "includeSelectAllOption"=>true,
                )
            ));
  ?>

But still I cant see "John Doe", "Doe", "John" as default values in in the Select2 box .

Sebastian Morales commented on Nov 15, 2021

The default value should not be an associative array. It should be like this:

      return array(
        "roles" => array(33, 11, 12),
      );

Pls also make sure that your select2's data $this->params()["adminRoles"] has those default values under "role_id" key.

Dev commented on Nov 15, 2021

I got your point, but still no success. No default values are coming in the select2 box.

For your reference this is the data of $this->params()["adminRoles"]

Array ( [38admin] => 1 [38ProgramCoordination] => 2 [38Control Room] => 3 [99admin] => 4 [99Program Coordination] => 5 )

and in defaultParamValues(),

return array(
        "roles" => array(1, 2, 3),
      );
Sebastian Morales commented on Nov 16, 2021

Since your select2's dataBind is:

                    "dataBind"=>array(
                          "value"=>"role_id",
                          "text"=>"roles"
                      ),

your select2's data must be in this format:

$data == array(
    array( "role_id" => 1, "roles" => "admin" ),
    array( "role_id" => 2, "roles" => "coordination" ),
    array( "role_id" => 3, "roles" => "control" ),
);
Dev commented on Nov 16, 2021

The select2's data is coming fine for me, I am able to bind the data and show in dropdown.

The problem is I am unable to pass some default values and show in the select box.

As you suggested I gave "role_id" key in the defaultParamValues(), like this

return array(
        "roles" => array(1, 2, 3),
      );

And select2's data => $this->params()["adminRoles"] has roles associated for each of those "role_id" keys. The values in $this->params()["adminRoles"] are like this

Array ( [38admin] => 1 [38ProgramCoordination] => 2 [38Control Room] => 3 [99admin] => 4 [99Program Coordination] => 5 )

Now, I just want 38admin, 38ProgramCoordination, 38Control Room to display as default values in the select2 box

Sebastian Morales commented on Nov 16, 2021

I've checked Select2 input again and it seems like for default values to work, its "data" property only accepts a simple array of scalar values, not an array of array. Pls try to use "dataStore" instead and let us know the result:

    Select2::create(array(
        ...
        "dataStore"=>$this->params()["adminRoles"],
        ...
    ));

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

None