kartik-v / yii2-widget-select2

Enhanced Yii2 wrapper for the Select2 jQuery plugin (sub repo split from yii2-widgets).
http://demos.krajee.com/widget-details/select2
Other
323 stars 145 forks source link

[request:] Enhance AJAX-optgroups usage documentation/example #199

Closed amlopezalonso closed 5 years ago

amlopezalonso commented 7 years ago

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 datacould be fed with ArrayHelper::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:

public function getBibliographyList()
{
    return ArrayHelper::map(Bibliography::find()->select(['id','title','author'])->orderBy('author','title')->all(), 'id', 'title', 'author');
}

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

amlopezalonso commented 7 years ago

I'm posting my personal solution just in case:

http://stackoverflow.com/questions/42331110/yii2-how-to-use-option-groups-with-ajax-based-kartiks-select2

kartik-v commented 7 years ago

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

kartik-v commented 7 years ago

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.

amlopezalonso commented 7 years ago

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.

stale[bot] commented 5 years ago

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.