mfarhan
mfarhan

Reputation: 67

ngFor items per row

I am struggling with display my data with *ngFor loop. I want ngFor to display data 3 items per row. i tried with indexing. but then it was 1 item/row.

<div class="container">
  <div class="row">
    <div class="mb-3 card-deck text-center">
      <div class="card mb-4 box-shadow" *ngFor="let product of item.product;">
        <div class="card-header">
            <h4 class="my-0 font-weight-normal">{{ product.name }}</h4>
        </div>
        <div class="card-body">
                <img src="{{ product.imagePath}}" class="img-thumbnail" alt="{{ product.name }}"> 
                <p class="lead">{{ product.msg }} {{ product.msg2 }}</p>
                <a role="button" class="btn btn-lg btn-block btn-primary" routerLink="product-details">More...</a>
        </div>
      </div>
    </div>
  </div>
</div>

I will highly appreciate any help. I am using

Angular-cli version 6.1.0
bootstrap 4

Upvotes: 3

Views: 10960

Answers (2)

Harun Or Rashid
Harun Or Rashid

Reputation: 5957

You can simply put logic on index as below :

<div *ngFor="let item of items; let i = index">
  <div *ngIf="i % 3 == 0" class="row">
    {{ item }}
    <div *ngIf="i + 1 < items.length">{{ items[i + 1] }}</div>
    <div *ngIf="i + 2 < items.length">{{ items[i + 2] }}</div>
  </div>
</div>

Upvotes: 9

Sarthak Aggarwal
Sarthak Aggarwal

Reputation: 2312

You can use bootstrap css classes to display 3 items per row

Make similar structure to your html file

<div class="row">
 <div class="col-4 col-sm-4 col-md-4 col-lg-4" *ngFor="let product of item.products">
  {{product.name}} | {{product.imagePath}} | {{product.msg2}} <!-- display product data -->
 </div>
</div>

Upvotes: 2

Related Questions