KoolReport's Forum

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

Select2 on Dashboards - Default Values #3246

Open Christian Voß opened this topic on on Feb 14 - 2 comments

Christian Voß commented on Feb 14

Hi,

I'm using the select2 input on my dashboard and it all works fine, unless I try to declare a default value.

Initially I tried this:

 protected function onInit()
    {
        $this->cssClass("form-control")
        ->multiple(true)
        ->placeHolder("Please select")
        ->value([2]);    
    }  

This works fine and it actually set item with the ID=2 as the selected default. However when I then later change the selection and try to get the value from the select on my other dashboard widgets I still only get the default value, even if I had changed it.

$sites = $this->sibling("mySiteSelect")->value();

Result is still = [2], even if I changed the values.

I then reviewed the documentation and found the defaultOption() method. So I tried this:

 protected function onInit()
    {
        $this->cssClass("form-control")
        ->multiple(true)
        ->placeHolder("Please select")
        ->defaultOption([2]);    
    }  

Unfortunately, this doesn't select the item with the id=2, although it should according to the documentation. It just adds a new item with the label 2 to the list and doesn't even select it.

I marked this as a bug, as it doesn't do what it says in the documentation.

Can you please either correct it or let me know what I'm doing wrong?

Thanks

Christian

Christian Voß commented on Feb 14

OK, I just found out that it works fine, if I don't set the value into the onInit function. If I set the value into i.e. the dataSource function, then everything works fine.

Sebastian Morales commented on Feb 15

Thanks for your update. You can also set Dashboard Select2's default value in onCreated method like this:

class MultipleSelect2Demo extends Select2
{
    protected function onCreated()
    {
        $this->defaultValue(array('Australia', 'Austria'));
    }
    ...
}

class SingleSelect2Demo extends Select2
{
    protected function onCreated()
    {
        $this->defaultValue('Australia');
    }
    ...
}

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

Inputs