Adding optgroups in angularjs is proving tricky
Asked Answered
S

2

16

I'm using angularjs, and I've been trying to create an optgroup for the past 2 days while working on some other stuff, and I'm stumped. I had to create an ng-options, but didn't consider an optgroup until I realized I needed one. I tried using the ng-options to add it in, but it would either ignore it completely, mess up the code or ignore the options completely.

I had no clue if I could actually use the (double) ampersand, so I tried combining both ng-options, but to no avail. Furthermore, I toyed around with a specific code found in odetocode (which seemed promising, but could not be incorporated with my code, because of its own limited structure).

Everything I tried breaks my code. The original code is below (the one prior to most of my many other mods), but I also have the plunker for it:

http://plnkr.co/edit/xCLe2GFShUMUlIySPLH9?p=info

HTML

<!DOCTYPE html>
<html ng-app="myApp">

  <head>
    <link rel="stylesheet" href="style.css">
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.19/angular.js"></script>
    <script src="script.js"></script>
  </head>

  <body>


  <form name="FORM" ng-controller="appController">
<the-ratings  model="review.ratings">         
</the-ratings>
</form>

  </body>

</html>

JS

(function(){

var app = angular.module('myApp',[]);

app.controller('appController', ['$scope', function($scope) {
      $scope.review = {};
      $scope.review.ratings = '5 stars';
  }])
  app.directive('theRatings', function() {

    return {
      restrict: 'E',
      scope: { model: '=' },
      template: '<select ng-model="model" ng-options="option.name as option.value for option in options"></select>',
      controller: function($scope) {
        $scope.options = [
            { name:'test', namegroup: 'Rate the product', value:'test2',valueName: 'NA' },
            { name: '1 star', value: '1 star' }, 
            { name: '2 stars', value: '2 stars' }, 
            { name: '3 stars', value: '3 stars' },
            { name: '4 stars', value: '4 stars' },
            { name: '5 stars', value: '5 stars' }
        ];
      }

    };
  });


})();
Skipper answered 17/7, 2014 at 19:0 Comment(0)
H
27

Not sure what you want to group them by so I added an extra property type:

Syntax:

ng-options="option.name as option.value group by option.type for option in options"

DEMO

Hildahildagard answered 17/7, 2014 at 19:17 Comment(7)
Ah damn lol, now I understand where I went wrong. I actually had that before deleting it from my code, but my optgroup was at the end. I had used the type before hand, but I didn't understand back then that EVERY option needed the type:Type 1" etc. Now that I understand that, it works well :) Basically, once you use a type, it creates it once and every one of the options are categorized UNDER that type. Thank you for making me understand that :DSkipper
think of it a bit like making a query on each element of the arrayHildahildagard
oh..and don't feel bad. ng-options takes some time to get used toHildahildagard
Don't forget that you must be using the ng-model attribute on this <select> element for ng-options to work, for some reason...Marrymars
I believe the syntax is in the wrong order, should be: option.value as option.name...Bighorn
@deangrande Then why is it working fine in my plunker demo link?Hildahildagard
@Hildahildagard because option.name and option.value hold the exactly the same value in your plunkr, so you can't see the issue.Bighorn
U
7

There's a good example of this in the AngularJS docs, if you want to group on an attribute of the options you can add a 'group by' clause. E.g.:

$scope.options = [
  { name:'test', namegroup: 'Rate the product', value:'test2',valueName: 'NA' },
  { type: 'one', name: '1 star', value: '1 star' }, 
  { type: 'one', name: '2 stars', value: '2 stars' }, 
  { type: 'two', name: '3 stars', value: '3 stars' },
  { type: 'two', name: '4 stars', value: '4 stars' },
  { type: 'two', name: '5 stars', value: '5 stars' }
];

and an ng-options of:

"option.name as option.value group by option.type for option in options"
Unilateral answered 17/7, 2014 at 19:15 Comment(3)
As I said to charlietfl as his answer came first (which I am saying to you as well): Ah damn lol, now I understand where I went wrong. I actually had that before deleting it from my code, but my optgroup was at the end. I had used the type before hand, but I didn't understand back then that EVERY option needed the type:Type 1" etc. Now that I understand that, it works well :) Basically, once you use a type, it creates it once and every one of the options are categorized UNDER that type. Thank you for making me understand that :DSkipper
@zetetic: Is that "good example" still in the AngularJS docs? I cannot find anything about group by on the page that you referenced.Shagreen
@user1438038: sorry, no idea. Could very well be a stale link. I swear it was there seven years ago.Unilateral

© 2022 - 2024 — McMap. All rights reserved.