3

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?

Suman Singh
1,0072 gold badges10 silver badges22 bronze badges
asked Jun 30, 2018 at 3:19
3
  • Vendor/Model/Source/Model.php path is incomplete, there should be a module name Commented Jun 30, 2018 at 4:56
  • Sorry, you are right. Vendor/Modulename/Source/Modulename.php Commented Jun 30, 2018 at 6:05
  • But my question is still the same Commented Jun 30, 2018 at 6:05

2 Answers 2

12

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.

sv3n
11.7k7 gold badges44 silver badges75 bronze badges
answered Jun 30, 2018 at 6:35
5
  • 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"??? Commented Jun 30, 2018 at 7:32
  • You have to just modify your area structure of $options like I have in my $optionGroup. Commented Jun 30, 2018 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. Commented Jun 30, 2018 at 7:36
  • Thank, the sample is allways working. Now I will try with my values. Commented Jul 2, 2018 at 5:22
  • Okay if it's work with your values too then accept the answer so it will help others Commented Jul 2, 2018 at 5:42
0

Just to complete the answer: if you want to add a "Choose" line you only have to add a line at the beginning of your $optionGroup array, like this:

$optionGroup = [
 ['value' => "Choose", 'label' => __("Choose...")],
 [
 'label' => 'Option group1 lable',
 'value' => [
 [
 'label' => 'group 1 option 1',
 'value' => 'group1-value1'
 ],
 [
 'label' => 'group 1 option2',
 'value' => 'group 1 value2'
 ],
 ],
 ],
 ...
];
answered Mar 17, 2020 at 13:34

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.