26

我有这种一级树情况:

<select ng-model="tipost" 
        ng-options="tip.DESC group by tip.TIPIS for tip in tipall"><br>
</select>



json在哪里:

[
  {"ID":"1", "IDPARENT":"0", "TIPIS":"", "DESC":"GroupName1"},
  {"ID":"2", "IDPARENT":"1", "TIPIS":"GroupName1", "DESC":"CHILDNAME1"},
  {"ID":"3", "IDPARENT":"0", "TIPIS":"", "DESC":"GroupName2"}
]


问题是这会创建带有孩子的 optgroup,但也会重复根源:

- GroupName1
- GroupName2
[ GroupName1 ]
- CHILDNAME1
[ GroupName2 ]


我想制作:

[ GroupName1 ]
- CHILDNAME1
[ GroupName2 ]


4

3 回答 3

36

如果您将 json 更改为以下内容,则分组不会那样工作:

[
 {"ID":"1", "TIPIS":"GroupName1", "DESC":"name"},
 {"ID":"2", "TIPIS":"GroupName1", "DESC":"name1"},
 {"ID":"3", "TIPIS":"GroupName2", "DESC":"name2"},
 {"ID":"4", "TIPIS":"GroupName1", "DESC":"name3"},
]

然后你会得到你想要的分组方式

jsFiddle:http: //jsfiddle.net/rtCP3/182/

于 2013-09-04T13:51:53.160 回答
7

可以显示带有和不带有组的项目的简单下拉列表,您还可以将某些项目设置为禁用:这是 plunk:https ://plnkr.co/edit/uhsn4lmzssi6rrijPEAp?p=preview

 <select ng-model="ddl.selected"
    ng-options="item.id as item.text group by item.groupName disable when item.enabled===false for item in ddl.items"></select>
于 2016-05-04T18:27:29.727 回答
6

我正在寻找相同的问题并找到更好的答案。可以根据需要使用函数返回数据,例如 getGarageName 下面的查询

ng-options="car.id as car.name + ' (' + car.color + ')' group by getGarageName(car.garageId) for car in cars"
// this is the data
cars = [
    {"id": 1, "name": "Diablo", "color": "red", "garageId": 1},
    {"id": 2, "name": "Countach", "color": "white", "garageId": 1},
    {"id": 3, "name": "Clio", "color": "silver", "garageId": 2},
    ...
]
garages = [
    {"id": 1, "name": "Super Garage Deluxe"},
    {"id": 2, "name": "Toms Eastside"},
    ...
]

http://www.wenda.io/questions/2597799/angular-js-select-with-ngoptions-label-the-optgroup.html

于 2015-01-31T22:40:54.660 回答