Create select with otpgroup based on two different data

Hi friends, two data in one JSON file, where I need to create the Select option based on the country field and show the data based on the location field

"countries": [
        {
            "id": "75",
            "country": "France"
        },
        {
            "id": "82",
            "country": "Germany"
        },
        {
            "id": "208",
            "country": "Switzerland"
        },
        {
            "id": "226",
            "country": "United Kingdom"
        },
        {
            "id": "227",
            "country": "United States"
        }
    ],
    location": [
        {
            "id": "49",
            "name": "Aberdeen",
            "country_id": "226",
            "country": "United Kingdom"
        },
        {
            "id": "52",
            "name": "All Cities",
            "country_id": "226",
            "country": "United Kingdom"
        },
        {
            "id": "51",
            "name": "All Cities",
            "country_id": "82",
            "country": "Germany"
        },
        {
            "id": "50",
            "name": "All Cities",
            "country_id": "227",
            "country": "United States"
        }
        {
            "id": "31",
            "name": "San Jose",
            "country_id": "227",
            "country": "United States"
        },
        {
            "id": "42",
            "name": "Seattle",
            "country_id": "227",
            "country": "United States"
        },
        {
            "id": "18",
            "name": "Stuttgart",
            "country_id": "82",
            "country": "Germany"
        },
        {
            "id": "41",
            "name": "Washington DC",
            "country_id": "227",
            "country": "United States"
        }
    ],

HTML file

 <select ng-model="location">
                <optgroup ng-repeat="data in location"  label="{{data.country}}">
                    <option ng-repeat="item in data" value="{{data.country}}::{{item.name}}">{{item.name}}</option>
                </optgroup>
  </select>

I need to show that my data looks like this:

enter image description here

I am trying to execute the code above, but I am not getting success. Please, help

+4
source share
1 answer

No need to use another array, you can also do this using only an array of locations, as follows

 <select ng-model="location" ng-options="option.name as option.name group by option.country for option in locations"></select>

Check out this working example.

+2
source

Source: https://habr.com/ru/post/1664405/


All Articles