ng使用angular中的json数据使用optgroup重复select
ng-repeat on select with optgroup using json data in angular
我有一个JSON文件,如下所示。
[
{"id":1,"catid":1,"Cat":"Category 1","label":"Label 1"},
{"id":2,"catid":1,"Cat":"Category 1","label":"Label 2"},
{"id":3,"catid":2,"Cat":"Category 2","label":"Label 3"},
{"id":4,"catid":2,"Cat":"Category 2","label":"Label 4"},
{"id":5,"catid":2,"Cat":"Category 2","label":"Label 5"},
{"id":6,"catid":3,"Cat":"Category 3","label":"Label 6"}
]
我已经设法创建了一个选择菜单,其中包括以上所有内容。
<select>
<option ng-repeat="referral in referralList" value="{{referral.id}}">{{referral.label}}</option>
</select>
Which produces this...
<select>
<option value="1">Label 1</option>
<option value="2">Label 2</option>
<option value="3">Label 3</option>
<option value="4">Label 4</option>
<option value="5">Label 5</option>
<option value="6">Label 6</option>
</select>
我想将类别作为optgroup包含在select中,如下所示。
<select>
<optgroup label="Category 1">
<option value="1">Label 1</option>
<option value="2">Label 2</option>
</optgroup>
<optgroup label="Category 2">
<option value="3">Label 3</option>
<option value="4">Label 4</option>
<option value="5">Label 5</option>
</optgroup>
<optgroup label="Category 3">
<option value="6">Label 6</option>
</optgroup>
</select>
这样尝试。
var app = angular.module("app",[]);
app.controller("ctrl" , function($scope){
$scope.data = [
{"id":1,"catid":1,"Cat":"Category 1","label":"Label 1"},
{"id":2,"catid":1,"Cat":"Category 1","label":"Label 2"},
{"id":3,"catid":2,"Cat":"Category 2","label":"Label 3"},
{"id":4,"catid":2,"Cat":"Category 2","label":"Label 4"},
{"id":5,"catid":2,"Cat":"Category 2","label":"Label 5"},
{"id":6,"catid":3,"Cat":"Category 3","label":"Label 6"}
]
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="ctrl" class="panel-group" id="accordion">
<select ng-model="model" ng-options="d.label group by d.Cat for d in data track by d.id"></select>
</div>
相关文章:
- 角度:在ng重复上切换图像
- 如何将函数包装在函数中以避免代码重复
- AngularJS:ng之后,重复$scope值未按预期更新
- Ajax聊天消息重复而不仅仅是更新
- 使用每次都不同的transclude重复指令
- 每当您在选择器内移动鼠标时,悬停功能就会重复
- 在javascript中搜索具有重复值的两个数组中的匹配值
- 下拉框,带有展开的optgroup
- 角度的项目列表 ng 重复,ng 单击显示全宽描述
- 防止ng重复中的重复值(AngularJS)
- Meteor JS中代码的重复使用部分
- 从键值结构中获取数据,并将其与AngularJS中ng重复的值进行比较
- 字母计数:返回重复字母数最多的第一个单词
- 在ng重复循环中显示来自不同文件夹的图像
- 对象上错误的javascript重复
- Angular ngRepeat:重复错误(尽管没有重复的密钥)
- JS数组-检查对象值是否重复
- 如何提供在javascript中编写对对象的重复访问以设置元素样式的简写
- 创建一个数组数组,没有任何重复的Javascript
- ng使用angular中的json数据使用optgroup重复select