0

This my json data how to set a selected option of a dropdown list control using angular JS

$scope.alltoys=[
       {
         "dId": "570b886545034f0001718247",
         "dName": "Toys",
         "dSubName": "Comics",
         "users": [
           {
                "name": "Superman",
                "id": "570b9e3a45034f000171827b"
           },
           {
                "name": "Batman",
                "id": "570ba00045034f000171828a"
           }]
       },
       {
        "dId": "5767c68c52faff0001fb8555",
        "dName": "Toys",
        "dSubName": "General",
        "users": [
           {
                "name": "Jack",
                "id": "570b9e3a45034f000171827b"
           },
           {
                "name": "Sparrow",
                "id": "570ba00045034f000171828a"
           }]
        }
]

How to populate this select box using angular js

I want to populate like this

Toys-Comics
    Batman
    Superman
Toys-General
    Jack
    Sparrow

In here Toys-Comics & Toys-General are only title of that selected content I want to sort users array when populate.

<div class="controls">
    <select class="form-control" ng-model="toy"
        ng-options="eachtoy as eachtoy.dName+' - '+eachtoy.dSubName group by eachtoy for eachtoy in alltoys">
        <option value="">---------------Select---------------</option>
    </select>
</div>

I tried this but its not working.

7
  • tried with order by? Commented Dec 20, 2016 at 13:49
  • Use mdMenu rather than drop down Commented Dec 20, 2016 at 13:50
  • @Kiran Kumar I didnt understand.Can you help me ji.Could you understand my question? Commented Dec 20, 2016 at 13:54
  • @LonelyPlanet I tried but i didnt get it Commented Dec 20, 2016 at 13:55
  • your $scope.alltoys json is not correct.(not formatted) Commented Dec 20, 2016 at 13:55

1 Answer 1

3

Couple of changes. First, make sure $scope.alltoys is a collection (array), like this:

$scope.alltoys = [{
  "dId": "570b886545034f0001718247",
  "dName": "Toys",
  "dSubName": "Comics",
  "users": [{
    "name": "Superman",
    "id": "570b9e3a45034f000171827b"
  }, {
    "name": "Batman",
    "id": "570ba00045034f000171828a"
  }]
}, {
  "dId": "5767c68c52faff0001fb8555",
  "dName": "Toys",
  "dSubName": "General",
  "users": [{
    "name": "Jack",
    "id": "570b9e3a45034f000171827b"
  }, {
    "name": "Sparrow",
    "id": "570ba00045034f000171828a"
  }]
}];

Then change your html to handle groups with optgroup, like this:

<select class="form-control" ng-model="toy">
  <option value="">---------------Select---------------</option>
  <optgroup ng-repeat="eachtoy in alltoys | orderBy: 'dName + dSubName" label="{{eachtoy.dName+' - '+eachtoy.dSubName}}">
    <option ng-repeat="user in eachtoy.users | orderBy: 'name'">{{user.name}}</option>
  </optgroup>
</select>

Here's a working plunk

Sign up to request clarification or add additional context in comments.

6 Comments

how to sorted both heading and content is it posiible
Sure. Use orderBy ... I've updated my answer and plunk
super brown very good your anser is awsome I am new in angular js. I think you are in swim in this good very good
You're welcome. You put together a well written, concise question that made it easy to help you. Nice job! Good luck with Angular ... its a great framework.
how to get corresponding selected toys id i didnt get it?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.