Angularjs:ng-options group by

时间:2013-09-04 13:45:59

标签: angularjs tree ng-options parentid

我有这种一级树情况:

<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"}
]


问题是,这会创建带有子项的optgroups但重复 根源:

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


我想生产:

[ GroupName1 ]
- CHILDNAME1
[ GroupName2 ]


3 个答案:

答案 0 :(得分:34)

如果你将你的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/

答案 1 :(得分:7)

可以显示包含和不包含群组的项目的简单下拉菜单,还可以将某些项目设置为已停用:以下是插件: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>

答案 2 :(得分:5)

我正在寻找同样的问题并找到了更好的答案。可以使用函数来根据需要返回数据,例如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