Closed amlopezalonso closed 5 years ago
I'm posting my personal solution just in case:
It is standard to a yii2 generated data by ArrayHelper::map as you mentioned for any native HTML select dropdown. The data for usage with optgroups must return an output like below:
$data = [
"Alaskan/Hawaiian Time Zone" => [
"AK" => "Alaska",
"HI" => "Hawaii",
],
"Pacific Time Zone" => [
"CA" => "California",
"NV" => "Nevada",
"OR" => "Oregon",
"WA" => "Washington",
],
"Mountain Time Zone" => [
"AZ" => "Arizona",
"CO" => "Colorado",
"ID" => "Idaho",
"MT" => "Montana",
"NE" => "Nebraska",
"NM" => "New Mexico",
"ND" => "North Dakota",
"UT" => "Utah",
"WY" => "Wyoming",
]
];
Such a format like above has been used in Select2 widget examples
For ajax based loading scenarios as described in core Select2 plugin docs... yes you need to generate your own logic like above... it should be documented as part of plugin. Willing to work on docs... based on your inputs.
If you wish so, I could elaborate my example above a bit further. Just suggest me the way it should be done as I'm not familiar with github way of doing things.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
Hi Kartik,
Please, could you expand examples/documentation on using optgroups in AJAX cases? I feel some parts lack detail level.
For instance, it is said that
data
could be fed withArrayHelper::map
in order to use sub-arrays, but on the contrary to a normal dropDown operation where having optgroups is as simple as doing in model:in Select2 there are some issues that complicate the process as having special hardcoded attributes like
text
,children
, etc. which I'm not sure how to integrate in the widget params in order to show optgroups in dropdown as you do in several examples.It would be nice if you could demonstrate how server/client-side code works in these AJAX cases.
Big thank you for all your work