KoolReport's Forum

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

Optgroups on Bselect to have grouping in options #2887

Open iWorQ Systems opened this topic on on Nov 28, 2022 - 4 comments

I
iWorQ Systems commented on Nov 28, 2022

Hey, Just wondering if there is a way that I can do optgroups for Bselects? I want to be able to organize the options I have into categories, and I am not seeing a easy way of doing that with Koolreports? I know that the Bootstrap MultiSelect that it is using has the capabilities, as I looked at the documentation, just not sure if I know how to do it on the koolreport side. An example of what I really want is in the picture. I look forward to your help, and want to thank you for your time.

I
iWorQ Systems commented on Nov 28, 2022

I was able to create a messy one on my own. it makes something like in the picture, but was wondering if in the future this is something that would be just default in KoolReports. I made the these changes to the code to get it to work as well:

My View code:

 BSelect::create(array(
                'name'=>"my_select",
                "multiple"=>true,
                "placeholder"=>"Select customer",
                "data"=>array(
                    "permit #"=>"permitnum_id",
                    "permit Date"=>"permit_dt",
                    "text 1"=>"text1",
                    "fee" => "feename",
                    "payment" => "payment",
                ), 
                "optgroup" =>["permit" => ["permit #",
                "permit Date",
                "text 1"],"Fees"=>["fee",
                "payment",]],
                "options"=>array(  
                    'enableClickableOptGroups'=> true,
                    'enableCollapsibleOptGroups' => true,
                    'enableFiltering' => true,
                    'includeSelectAllOption' => true, 
                ),

                ));

BSelect.php

 protected $optgroup;
 protected function onInit()
    {
        parent::onInit();

        $this->multiple = Utility::get($this->params,"multiple",false);
        $this->optgroup = Utility::get($this->params,"optgroup",null);
        //continue with Bselect.php
}

Bselect.tpl.php

if($this->optgroup){
    foreach($this->optgroup as $key=>$group){
        echo('<optgroup label="'.$key.'"');
        foreach($group as $row){
            foreach($this->data as $item)
            {
                
                $value = $item["value"];
                $text = $item["text"];
                if($text == $row){
                ?>
                    <option value="<?php echo $value; ?>" <?php echo (($this->multiple)?in_array($value,$this->value):($value==$this->value))?"selected":""; ?>><?php echo $text; ?></option>
                <?php
                }
            }
        }
        echo('</optgroup>');
    }
}else{
    foreach($this->data as $item)
    {
        $value = $item["value"];
        $text = $item["text"];
    ?>
        <option value="<?php echo $value; ?>" <?php echo (($this->multiple)?in_array($value,$this->value):($value==$this->value))?"selected":""; ?>><?php echo $text; ?></option>
    <?php
    }
}

Anyways, thanks for the time, and i hope to hear back from you soon.

S
Sebastian Morales commented on Dec 1, 2022

I think this is a very nice solution, props to you! We will see if there's another way to do grouping in BSelect and let you know. Rgds,

I
iWorQ Systems commented on Jul 3, 2023

Have we gotten any where with this? I really do not want an option where we have to change the vendor files if we do not have to.

I
iWorQ Systems commented on Jul 3, 2023

By that I mean us manually changing it on our side.

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