grouped select/options from nested data

6,438 views
Skip to first unread message

Danny

unread,
Mar 11, 2013, 8:52:47 PM3/11/13
to ang...@googlegroups.com
Hi,

I'd like to create grouped select/options from nested data.

Specifically, the data looks like this:
var carList = ['honda', 'toyota', 'bmw', 'audi', 'trabant', 'new trabant', 'toyota Mark II']; 
var planeList = ['A310', 'A380', 'B373', 'B747'];
$scope.theList = [{group: 'cars', items: carList}, {group: 'planes', items: planeList}]; 

So I'd like that to render as:

<select>
  <optgroup label='cars'>
    <option>honda</option>
    <option>toyota</option>
   ... etc...
  </optgroup>
  <optgroup label='planes'>
    <option>A310</option>
    <option>A380</option>
    ...etc...
  </optgroup>
</select>


I've tried numerous combinations but can't get the structure I need.  Is it possible to render the grouped list without restructuring the data?

Thanks!
Danny

Clint Checketts

unread,
Mar 11, 2013, 10:49:44 PM3/11/13
to ang...@googlegroups.com
You need the list to be 'flat' Plunkr: http://plnkr.co/edit/gYoYDhKA58SxcYSwvRZr?p=preview

As in:

<select ng-model='theModel' ng-options='veh.name group by veh.type for veh in theList'></select>

$scope.theList = [
    {type:'car',name:'honda'},
    {type:'car',name:'toyota'},
    {type:'car',name:'bmw'},
    {type:'car',name:'audi'},
    {type:'car',name:'trabant'},
    {type:'car',name:'new trabant'},
    {type:'car',name:'toyota Mark II'},
    {type:'plane',name:'A310'},
    {type:'plane',name:'A380'},
    {type:'plane',name:'B373'},
    {type:'plane',name:'B747'}
    ];

});




Danny

--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular?hl=en-US.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Clint Checketts

unread,
Mar 11, 2013, 10:57:17 PM3/11/13
to ang...@googlegroups.com
Sorry about not answering the second part of your question:  You can create a helper method that reduces the 2 separate arrays into the flattened array.

http://plnkr.co/edit/ODgumLVBi6sqGpbTcSwx?p=preview

Danny

unread,
Mar 12, 2013, 2:29:01 AM3/12/13
to ang...@googlegroups.com
Thanks Clint... that was helpful.

I have also found another way to do it... obvious really, just was distracted by the complexities of Angular.  

For those interested, the new solution:

    <select ng-model='theModel'>
      <option value='choose...'>Choose...</option>
      <optgroup ng-repeat='group in theList' label="{{group.group}}">
        <option ng-repeat='veh in group.items'>{{veh}}</option>
      </optgroup>
    </select>


Danny
 

Peter Stephens

unread,
Apr 17, 2013, 5:49:13 AM4/17/13
to ang...@googlegroups.com
Thanks Danny - exactly the problem I was struggling with....

Pete.

Sylvain Robez-Masson

unread,
May 2, 2013, 3:14:30 PM5/2/13
to ang...@googlegroups.com
My problem is different, my data looks like this :
var categories = [
    {"id":"Cart","name":"Ma commande"},
    {"id":18,"name":"Menus"},
    {"id":14,"name":"Entr\u00e9es"},
    {"id":5,"name":"Plats","items":[
        {"id":7,"name":"Viandes"},
                {"id":8,"name":"Poissons"},
                {"id":11,"name":"Pizzas"}]
        },
        {"id":15,"name":"Desserts"},
        {"id":17,"name":"Boissons","items":[
                {"id":1,"name":"Alcoolis\u00e9es"},
                {"id":3,"name":"Sans alcool"}]
        }
]

Entries without items should be <option>, Entries with items should be <optgroup> and the items of the entry must be <option> of <optgroup>
How can I achieve that ?

Charles Kilbourne

unread,
May 2, 2013, 4:17:02 PM5/2/13
to ang...@googlegroups.com
Sylvain you may want to use an ng-switch in your template, if you want an example set up a plunk and I'll try to help you out

Sylvain Robez-Masson

unread,
May 3, 2013, 7:40:39 AM5/3/13
to ang...@googlegroups.com
I don't see any solution with ng-switch.
Maybe something like that : http://jsfiddle.net/vDdG2/2/
But obviously that doesn't work.

Sylvain Robez-Masson

unread,
May 4, 2013, 10:38:17 AM5/4/13
to ang...@googlegroups.com
I found a solution : http://jsfiddle.net/vDdG2/5/

krunal shah

unread,
Jan 5, 2015, 5:42:31 AM1/5/15
to ang...@googlegroups.com
hi i would like to select parent node into dropdown. Like group is also selected..

Phani K

unread,
May 26, 2015, 9:14:45 PM5/26/15
to ang...@googlegroups.com
Hi,
I want to select the group name and click event for group name. any help...
in below example..i want to select Group1.

Ex: 
Group1
    A
    B
Group2
     C
     D
Reply all
Reply to author
Forward
0 new messages