1

I have created an AngularJS application with select box, The application with select is working fine, but the problem is that I want option group like thing in select which is selectable, since i am aware that the option group feature in select is not selectable, I made similar stuff using CSS, when i put the values statically its working fine, but dynamically I am not able to show the child details

JSFIDDLE

can anyone please tell me some solution for this

angular.module('Test1',[])

.controller('Foo', function($scope) {

$scope.regionsData = {
  "regions": [
    {
      "regionId": 15,
      "regionName": "Kerala",
      "places": [
        {
          "placeId": 21,
          "placeName": "Trivandrum"
        },
        {
          "placeId": 22,
          "placeName": "Kollam"
        }]
    },
    {
      "regionId": 16,
      "regionName": "Chennai",
      "places": [
        {
          "placeId": 23,
          "placeName": "Kanchipuram"
        },
        {
          "placeId": 24,
          "placeName": "Guindy"
        }]
    }
    ]
}
    
});
.region {
    font-weight: bold
}
.place {
    padding-left: 25px;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="Test1">
    <div ng-controller="Foo">
      
<b>DYNAMIC<b><br>
HOW CAN I DISPLAY THE CHILD DETAILS<br>
        <select ng-model="data.selectedDocumentType" class="region" ng-options="region as region.regionName for region in regionsData.regions"> </select> 
        
      
<br><br><b>STATIC<b><br>
THIS IS WHAT I WANT TO ACHIEVE<br>
        <select> 
            <option class="region">Kerala</option>
            <option class="place">Trivandrum</option>
            <option class="place">Kollam</option>
            <option class="region">Chennai</option>
            <option class="place">Kanchipuram</option>
            <option class="place">Guindy</option>
        </select> 
    </div>
</div>
Roman C
  • 49,761
  • 33
  • 66
  • 176
Alex Man
  • 4,746
  • 17
  • 93
  • 178

1 Answers1

1

FYI, google chrome doesn't allow adding css to option elements, see this answer

I found this to work:

example

as mentioned in the comments, I re formatted the data in a simpler fashion, using loops on the original variable:

$scope.test = [];
$scope.regionsData.regions.forEach(function(val, key) {
  $scope.test.push(val.regionName);
  val.places.forEach(function(val, key){
    $scope.test.push(val.placeName);
  })
})

and in html:

<select ng-model="data.selectedDocumentType" class="region" ng-options="region for region in test"> </select>

to overcome chrome's issue, and apply css, a better approach is to use <ul> with <li> to make the dropdown.

here's an example of how to create the html, making in look like a dropdown is out of the scope of your question:

turn select into unordered-list fiddle

see this question on how to turn a ul into a dropdown

Community
  • 1
  • 1
Nitsan Baleli
  • 5,393
  • 3
  • 30
  • 52
  • @AlexMan there are ways to make the ul look like a dropdown, and even far more better looking. Ive updated the answer. please review, and accept if it answered your question. – Nitsan Baleli Sep 18 '14 at 10:43