【问题标题】:Create select with otpgroup based on two different data根据两个不同的数据使用 otpgroup 创建选择
【发布时间】:2017-05-06 15:38:54
【问题描述】:

嗨朋友们,这里有两个数据在一个 JSON 文件中,我需要创建基于国家字段的选择选项并根据位置字段显示数据

"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 文件

 <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>

我需要显示我的数据看起来像:

我尝试了上面的代码,但没有成功。请帮忙

【问题讨论】:

    标签: angularjs ionic-framework angularjs-ng-repeat angularjs-ng-options optgroup


    【解决方案1】:

    无需使用其他数组,您也可以只使用位置数组,如下所示

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

    Check this working example

    【讨论】:

    • 谢谢哥们,你真的拯救了我的一天。
    猜你喜欢
    • 2016-08-01
    • 2022-11-15
    • 1970-01-01
    • 2017-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-31
    相关资源
    最近更新 更多