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 nameSuman Singh– Suman Singh2018年06月30日 04:56:36 +00:00Commented Jun 30, 2018 at 4:56
-
Sorry, you are right. Vendor/Modulename/Source/Modulename.phpDr. Christian Kusche– Dr. Christian Kusche2018年06月30日 06:05:14 +00:00Commented Jun 30, 2018 at 6:05
-
But my question is still the sameDr. Christian Kusche– Dr. Christian Kusche2018年06月30日 06:05:58 +00:00Commented Jun 30, 2018 at 6:05
2 Answers 2
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– Dr. Christian Kusche2018年06月30日 07:32:22 +00:00Commented Jun 30, 2018 at 7:32
-
You have to just modify your area structure of $options like I have in my $optionGroup.Rakesh Varma– Rakesh Varma2018年06月30日 07:34:38 +00:00Commented 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.Rakesh Varma– Rakesh Varma2018年06月30日 07:36:08 +00:00Commented Jun 30, 2018 at 7:36
-
Thank, the sample is allways working. Now I will try with my values.Dr. Christian Kusche– Dr. Christian Kusche2018年07月02日 05:22:22 +00:00Commented Jul 2, 2018 at 5:22
-
Okay if it's work with your values too then accept the answer so it will help othersRakesh Varma– Rakesh Varma2018年07月02日 05:42:09 +00:00Commented Jul 2, 2018 at 5:42
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'
],
],
],
...
];