In my application, I have an array of objects with the following structure
[{"ID":1, "parentID":0, "name":"Parent #1"}, {"ID":2, "parentID":0, "name":"Parent #2"}, {"ID":3, "parentID":1, "name":"Child #1 1"}, {"ID":4, "parentID":3, "name":"child #1 2"}, {"ID":5, "parentID":2, "name":"child #2 1"}, {"ID":6, "parentID":5, "name":"child #2 2"}]
I would like to present this as a selection menu that would allow the user to select a node sheet while the output non-selectable parent nodes displayed the hierarchy of the structure.
I tried several approaches, the most successful of which was in my angular template, something like the following.
<div ng-repeat="(idx, category) in $scope.allCats"> <select ng-model="$scope.cats[idx]" ng-options="cat as cat.name group by $scope.parentName(cat.parentID, idx) for cat in $scope.allCategories track by cat.ID"> <option value="">Select A Category</option> </select> </div>
$scope.allCats is an array, and the $scope.parentName() method returns a string.
Problems with this are shown in the following screenshot. Namely, all parent elements appear twice, once as <option> and once as <optgroup> , while I would prefer that they appear only as a selectable element, but with them, obviously, the parent element, and the hierarchy of the structure supported; child nodes with ancestors and descendants do not appear in the correct tree-tree structure.

How can I change the input or my angular pattern to achieve the desired behavior?
That is, to display the entire hierarchy, as defined by the attributes of the parentID , therefore each family has a common ancestor and the parent elements appear only once.
I suspect this is complicated by the fact that more than one level of descendants is possible, and because I would like to keep it as general as possible.