Angularjs: ng-opciones con grupo


Tengo esta situación de árbol de un nivel:

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



donde está el json:

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



el problema es que esto crea los optgroups con sus hijos pero repite las raíces también:

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


quiero producir:

[ GroupName1 ]
- CHILDNAME1
[ GroupName2 ]


Author: ekad, 2013-09-04

3 answers

El agrupamiento no funciona así, si cambias tu json a algo como esto:

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

Entonces obtendrá la agrupación de la manera que desee

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

 31
Author: Mathew Berg,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-09-04 13:51:53

El simple desplegable que puede mostrar elementos con y sin grupos, y también puede establecer algunos elementos como deshabilitados: Aquí está 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>
 6
Author: Shafqat Ali,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-05-04 18:27:29

Estaba buscando la misma pregunta y encontré una mejor respuesta. Uno puede usar la función para devolver datos según lo necesite, como en la consulta a continuación 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

 5
Author: malibeg,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-01-31 22:40:54