KoolReport's Forum

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

Select2 default value in dashboard #3151

Closed John opened this topic on on Sep 20, 2023 - 10 comments

John commented on Sep 20, 2023

I've set a select2 widget in dashboard like this example but with multiple(true) and I don't get the default values. Also it doesn't work if i use MultiSelect instead of Select2 Can you help?

Sebastian Morales commented on Sep 21, 2023

Probably you have to set the default value of inputs be it TextBox, Select2, BSelect, etc in the report class file like this:

class Report extends \koolreport\KoolReport
{
    use \koolreport\inputs\Bindable;
    use \koolreport\inputs\POSTBinding;

    protected function defaultParamValues()
    {
        return array(
            "textBox"=>"KoolReport is great",
            "singleSelect2" => 'Enaco Distributors', // single select value should be a string or number
            "multipleSelect2" => ['Enaco Distributors'], // multiple select value should be an array
            ...
        );
    } 
John commented on Sep 21, 2023

Thanks but which is the report class file in Dashboard? I tried what you proposed in widget's class but it didn't work.

Sebastian Morales commented on Sep 22, 2023

Ah, sorry I didn't notice that you mentioned Select2 input in a KoolReport Dashboard instead of an individual report. In that case you can set Select2's default value directly like this:

class MultipleSelect2Demo extends Select2
{
    protected function onCreated()
    {
        $this
            ->multiple(true)
            ->defaultValue(['Australia', 'Austria']);
    }
John commented on Sep 22, 2023

As i said when i created this topic above, i've already done this. That's why i think it is a bug.

Sebastian Morales commented on Sep 25, 2023

Would you pls show the PHP code for your Dashboard's Select2 or MultiSelect widget and the screenshot of its initial value? Rgds,

John commented on Oct 4, 2023

You can see image and code below.

Code

<?php

namespace myforder;

use \koolreport\dashboard\inputs\Select2;
use \koolreport\dashboard\fields\Number;
use \koolreport\dashboard\fields\Text;

class mySelectbox extends Select2
{	
    protected function onInit()
    {
        $this
            ->defaultOption(['ABC'])
	    ->cssStyle('margin-left:10px')
            ->cssClass('form-control')
            ->multiple(true)	;
    }  

    //Provide datasource
    protected function dataSource()
    {
      return \xxx\myDB::table("customers")
                ->select("customerName")
                ->orderBy("customerName") ; 
    }

    //Provide list of fields to act as value and text for MultiSelect
    protected function fields()
    {
        return [
            Text::create("customerName")
        ];
    }
	
    //Update when user select values
    protected function actionChange($request, $response)
    {
        $this->sibling("ChartTest3")->update();
    }

}
Sebastian Morales commented on Oct 9, 2023

In Dashboard's Select2, ->defaultOption() is not the same as ->defaultValue() and function onInit() is different from function onCreated(). Pls try this exact method with your Dashboard's Select2:

    protected function onCreated()
    {
        $this
            ->multiple(true)
            ->defaultValue(['ABC']);
    } 
John commented on Oct 9, 2023

Thanks. I've already noticed this detail (with the defaultOption and defaultValue) and i tested it days ago but it didn't work... I didn't imagine that the onCreated() would be different... I'll try exactly what you say and i'll update you.

John commented on Oct 9, 2023

It works! but only with english characters... when i put greek, values are not appeared... :-(

Sebastian Morales commented on Oct 10, 2023

It could be the mismatched character encoding issue between your database data and your web page. When I try at least with utf-8 characters the Select2's default value works.

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
bug
help needed

Inputs