3

Does anyone know if Angular-Ui-Select Bootstrap version support an optgroup?

Can't seem to find any documentation for that on https://github.com/angular-ui/ui-select?

Here is their example:

plnkr.co/edit/QCwSM75ilH2Vh6D9aMA4?p=preview

How to add an optgroup?

In this example, lets say, group persons by countries.

user3666197
  • 1
  • 6
  • 50
  • 92
user1906437
  • 233
  • 2
  • 6
  • 12

2 Answers2

3

You can use group-by attribute.

See "Demo Multiselect" (last example "Array of objects (with groupBy)") at https://github.com/angular-ui/ui-select

It's multiselect demo, but group-by works for single select too.

lector
  • 165
  • 2
  • 9
1

This is group-by using string

app.js:

$scope.countries = [
                {
                    "code": "AD",
                    "name": "Andorra",
                    "continent": "Europe"
                },
                {
                    "code": "AE",
                    "name": "United Arab Emirates",
                    "continent": "Asia"
                },
                {
                    "code": "AF",
                    "name": "Afghanistan",
                    "continent": "Asia"
                }
            ];

html:

<div>
    <label>COUNTRY</label><br>
    <ui-select ng-model="user.country" style="min-width: 300px;">
        <ui-select-match placeholder="Select Country">
            <span ng-bind="$select.selected.name"></span>
        </ui-select-match>
        <ui-select-choices repeat="country in countries | filter: {name: $select.search}" group-by="'continent'">
        <span ng-bind="country.name"></span>
        </ui-select-choices>
    </ui-select>
</div>

Generated JSON of all countries with their continent using

http://peric.github.io/GetCountries/

linktoahref
  • 7,812
  • 3
  • 29
  • 51