I'm using AngularJS, and I want to use ng-options for a select tag to display options grouped by optgroups.
this is the array I want to use in my select option:
$scope.myList = [
{
"codeGroupCompetence": 1,
"titre": "TECH. DE PRODUCTION / EXPLOITATION",
"activated": true,
"competences": [
{
"codeCompetence": 7,
"titre": "Tech. exploitation",
"activated": true
},
{
"codeCompetence": 8,
"titre": "Tech. postes de travail",
"activated": true
},
{
"codeCompetence": 9,
"titre": "Tech. réseaux",
"activated": true
},
{
"codeCompetence": 10,
"titre": "Tech. Help Desk",
"activated": true
},
{
"codeCompetence": 11,
"titre": "Tech. Téléphonie",
"activated": true
},
{
"codeCompetence": 12,
"titre": "Tech. Autre",
"activated": true
}
]
},
{
"codeGroupCompetence": 2,
"titre": "ETUDES, CONCEPTION, MODELISATION",
"activated": true,
"competences": [
{
"codeCompetence": 5,
"titre": "Concepteur UML, Merise, ...",
"activated": true
},
{
"codeCompetence": 13,
"titre": "Admin Windows",
"activated": true
},
{
"codeCompetence": 14,
"titre": "Admin Unix",
"activated": true
},
{
"codeCompetence": 15,
"titre": "Admin Linux",
"activated": true
},
{
"codeCompetence": 16,
"titre": "Administrateur AS400",
"activated": true
},
{
"codeCompetence": 17,
"titre": "Administrateur Mainframe IBM",
"activated": true
},
{
"codeCompetence": 18,
"titre": "Administrateur Autres Systèmes",
"activated": true
}
]
},
{
"codeGroupCompetence": 3,
"titre": "ADMIN SYSTEMES",
"activated": true,
"competences": [
{
"codeCompetence": 6,
"titre": "Urbaniste SI",
"activated": true
}
]
}
];
In order to display this list using the group by feature in ng-options I created a new array as following :
$scope.competences = [];
myList.forEach(function(group){
group.competences.forEach(function(competence){
$scope.competences.push({
id : competence.codeCompetence,
titre : competence.titre,
group : group.titre
})
});
});
and this is how I display my select option :
<select ng-model="tipost"
ng-options="competence.id as competence.titre group by competence.group for competence in competences track by competence.id">
</select>
Unfortunately this doesn't work for some reason, which I cant figure out what is it.
So please, how can I solve this ?
Another question : isn't there any other method to use only the first array with the group by ?
Thanks in advance.
This is a jsfiddle for my example :