Mahfuja nilufar
Mahfuja nilufar

Reputation: 337

Display array data to 3 column using angular2 & bootstrap

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.

Upvotes: 3

Views: 2688

Answers (1)

Deividas
Deividas

Reputation: 6507

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>

Upvotes: 5

Related Questions