0

I'm trying to display information from child and parent data within a json object. Below is my data:

$scope.electionDetails = {
  id : 1,
  election_type: "CityCouncil",
  election_name: "City A City Council Elections
  candidates : [{
               id: 1,
               election_id: 1,
               position_id: 1,
               first_name: "John",
               last_name: "Doe"
               },
               {
               id:2,
               election_id:1,
               position_id:1,
               first_name: "Jane",
               last_name: "Doe"
               },
               {
               id:3,
               election_id:1,
               position_id:2,
               first_name: "Mike",
               last_name: "Doe" 
               },
               {
               id:4,
               election_id:1,
               position_id:2,
               first_name: "Mary",
               last_name: "Doe"
               }],
 positions : [{
             id:1,
             election_id: 1,
             position: "Seat 1"
             },
             {
             id:2,
             election_id:1,
             position: "Seat 2"
             }]

}

I want to display this data grouped, using angular like so:

City A City Council Elections

Seat 1

  • John Doe
  • Jane Doe

Seat 2

  • Mike Doe
  • Mary Doe
4
  • 1
    Use table with ng-repeat Commented Apr 6, 2016 at 15:54
  • 1
    What is your question? Have you made an attempt to display the data? If so, can you post it here? Commented Apr 6, 2016 at 15:55
  • I haven't attempted. I know how to use ng-repeat just not in a way that will group this data correctly. Commented Apr 6, 2016 at 16:00
  • I have a solution for you. It is also worth noting that you are missing an ending " and , at the end of the "election_name" property in your code you have posted Commented Apr 6, 2016 at 16:04

1 Answer 1

1

Here you go. The HTML structure may not be exactly what you want, so you can change up what tags you use, but this is the basic idea using ng-if and ng-repeat to create lists of candidates for a given seat.

DEMO

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

3 Comments

This is correct. You can also use the 'filter' filter, instead of ng-if docs.angularjs.org/api/ng/filter/filter
Agreed. If it were my application I would use filter, I was just trying to keep it as simple as possible for the OP. Great suggestion, though!
Thanks! I think I could I apply this with a filter now.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.