3

In my angular2 component I have listed data and I want to display those data into 3 column by using booststrap class(col-md-4).

For example, I have json object like below.

{
"menuList":[
    {"id":1,"sn":"nd","name":"Noodles"},
    {"id":2,"sn":"sd","name":"Salad"},
    {"id":3,"sn":"sp","name":"Soup"},
    {"id":4,"sn":"fb","name":"Fresh Bowl"},
    {"id":5,"sn":"sn","name":"Snacks"},
    {"id":6,"sn":"pz","name":"Pizza"},
    {"id":7,"sd":"nd","name":"Sandwich"}
]

}

And I want to display the name into 3 column.

Noodles Salad Soup Fresh Bowl Snacks Sandwich

Here is my code that I tried.

<div *ngFor="let menu of menuList; #i=index" *ngIf="i % 3 == 0" class="row">
    <div class="col-md-4">{{menu[$i].name}}</div>
    <div class="col-md-4">{{menu[$i + 1].name}}</div>
    <div class="col-md-4">{{menu[$i + 2].name}}</div>

But it display error, because first of all I cannot use ngFor & ngIf in a same element.

Please give me a solution.

1
  • 1
    3x md-4 equals a full row. You're perfectly fine when you just output one <div class="col-md-4">{{menu.name}}</div> Commented Feb 21, 2017 at 19:19

1 Answer 1

5

To display items in rows you do not need to actually separate the rows, because 'col-md-4' will break into rows after 3 columns (as expected). The code below will work for your case:

<div class="row">
  <div *ngFor="let menu of menuList" class="col-md-4">      
    {{menu.name}}
  </div>
</div>
Sign up to request clarification or add additional context in comments.

Comments

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.