1

In my ui-select fields I want to sort my options by different optgroups like: enter image description here

Source model location is Vendor/Model/Source/Model.php and it has the function for options as below

public function getAllOptions()
{
    if (empty($this->options)) {
        $options = [];
        $searchCriteria = $this->searchCriteriaBuilder->create();
        $searchResults = $this->ghscategoryRepository->getList($searchCriteria);
        foreach ($searchResults->getItems() as $ghscategory) {
            $options[] = [
                'value' => $ghscategory->getGhscategoryId(),
                'label' => $ghscategory->getGhs_category_code(),
            ];
        }
        $this->options = $options;
    }

    return $this->options;
}

Can I edit the options array and add optgroups?

If not here, which file/files I have to edit to add my optgroups?

  • Vendor/Model/Source/Model.php path is incomplete, there should be a module name – Suman Singh Jun 30 '18 at 4:56
  • Sorry, you are right. Vendor/Modulename/Source/Modulename.php – Dr. Christian Kusche Jun 30 '18 at 6:05
  • But my question is still the same – Dr. Christian Kusche Jun 30 '18 at 6:05
6

In same file create your option array like this

$optionGroup = [
    [
        'label' => 'Option group1 lable',
        'value' => [
            [
                'label' => 'group 1 option 1',
                'value' => 'group1-value1'
            ],
            [
                'label' => 'group 1 option2',
                'value' => 'group 1 value2'
            ],
        ],
    ],
    [
        'label' => 'Option group2 lable',
        'value' => [
            [
                'label' => 'group 2 option 1',
                'value' => 'group2-value1'
            ],
            [
                'label' => 'group 2 option2',
                'value' => 'group2-value2'
            ],
        ],
    ]
];

and return this array it will create drop down with option group.

  • Hi,thank you for your awnser. I am not sure, where I have to add this code.Do I have to replace the array "options" by the new array "optionGroup"??? – Dr. Christian Kusche Jun 30 '18 at 7:32
  • You have to just modify your area structure of $options like I have in my $optionGroup. – Rakesh Varma Jun 30 '18 at 7:34
  • You can also use the my $optionGroup array and return it instead of $options for testing if it will give you perfect output then change the values as you want. – Rakesh Varma Jun 30 '18 at 7:36
  • Thank, the sample is allways working. Now I will try with my values. – Dr. Christian Kusche Jul 2 '18 at 5:22
  • Okay if it's work with your values too then accept the answer so it will help others – Rakesh Varma Jul 2 '18 at 5:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.