1

I have a JSON object containing two array objects. The object is store in $scope.person. Now i want to display the Likes array in first column and Dislikes array in the second column of the table. Assume that the length of both arrays are similar so no. of rows will be similar as well. What could be the best way to do it? Check the JSON object below.

{
  "name": "Justin Clark",
  "rating": 3,
  "img": "http://www.fillmy.com/200/200",
  "Description": "Gluten­free cray cardigan vegan. Lumbersexual pork belly blog, fanny pack put a bird on it selvage",
  "Likes": [
    "Dogs",
    "Long walks on the beach",
    "Chopin",
    "Tacos"
  ],
  "Dislikes": [
    "Birds",
    "Red things",
    "Danish food",
    "Dead Batteries"
  ]
}

The table structure looks like this.

<table class="table table-striped">
  <thead> 
    <tr> <th>Likes</th> <th>Dislikes</th></tr> 
  </thead>   
  <tbody> 
    <tr ng-repeat="pl in person"> 
      <td>{{pl.Likes}}</td> <td>{{pl.Dislikes}}</td> 
    </tr>
  </tbody>
</table>
4
  • you need two seperate ng-repeat for both of them Commented Oct 22, 2016 at 4:17
  • i tried doing that, but im not sure how to do it. how can we have two ng-repeat in one tr ? Commented Oct 22, 2016 at 4:20
  • added a simpler way to do this as an answer. Check below. Commented Oct 22, 2016 at 4:47
  • What has number of rows got to do with number of columns? Number of rows will correspond to the number of person objects. Whilst the number of columns will depend upon the number of attributes in each person object, isn't it? Commented Oct 22, 2016 at 5:07

2 Answers 2

2

You can run the ng-repeat on one of the arrays (take Likes) and using the $index of ng-repeat you can have the Dislikes array mapped as well.

Something like this..

<tr ng-repeat="like in data.Likes">
  <td>{{like}}</td> 
  <td>{{data.Dislikes[$index]}}</td>
</tr>

DEMO

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

3 Comments

Thank you tanmay. That works. Can we do like: setting the ng-repeat="like in data" and have the td saying {{data.Likes[$index]}} for Likes and {{data.Dislikes[$index]}} for Dislikes?
@SarmadAsif absolutely
Awesome. Thanks again!
1

You can do this,

<body ng-app='app'>
  <div class="media-list" ng-controller="dishDetailController">
    <table>
      <tr>
        <th>Likes</th>
        <th>Dislikes</th>
      </tr>
      <tr ng-repeat="customer in data">
        {{customer}}
        <td>{{customer.Likes}}</td>
        <td>{{customer.Dislikes}}</td>
      </tr>
    </table>
  </div>
</body>
</html>

DEMO

EDIT:

Based on assumption your data is not an array,

DEMO

8 Comments

Thanks for solution but in the demo, it doesnt display in separate rows.
you need to display the child array as rows?
I want to show Likes array in first column as different rows and Dislike array in the second column as different rows. Since the length of both arrays are similar, which is 4. So there would be 4 rows in total.
@SarmadAsif I thought your data is an array, check this plunker plnkr.co/edit/kDJe6wQ0WJnFvQ7B03Oz?p=preview
@tanmay No i dint copy, its a simple thing bro. i mentioned the reason why it dint work, i was under the impression its a json array. If you want i can delete my answer.
|

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.