Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a div after every 3 elements in *ngFor

I am trying to build an angular project . In my project I want to show a list of books ,I have a SmallBookView Component for a single book and I want to use ngFor to show many other books./ however after every 3 books I need to add a div to separate this books, In the end it should look like so

<app-small-book-view></app-small-book-view>
<app-small-book-view></app-small-book-view>
<app-small-book-view></app-small-book-view>
<div class="clearfix"> </div>
<app-small-book-view></app-small-book-view>
<app-small-book-view></app-small-book-view>
<app-small-book-view></app-small-book-view>
<div class="clearfix"> </div>
<app-small-book-view></app-small-book-view>
<app-small-book-view></app-small-book-view>
<app-small-book-view></app-small-book-view>
<div class="clearfix"> </div>

the div separate each line of books in the ui. I am trying to use ngFor like so

<app-small-book-view *ngFor="let i of [1,2,3,4,5,6,7]; let _i = index">
  <div class="clearfix" *ngIf="(_i % 3) == 0"></div>
</app-small-book-view>

but this is adding the div only inside the SmallBookView instead of between them, how do I add this div between the components>

like image 988
Yedidya kfir Avatar asked Aug 14 '18 21:08

Yedidya kfir


People also ask

How do you use ngfor in an array?

The ngFor directive does create the HTML-Element it is placed in as many times as there are elements in the array. So to create a list-element for each array element, we place the ngFor directive inside of the li tag.

How to get the index of each element using ngfor directive?

This is an affiliate link. We may receive a commission for purchases made through this link. To get the index of each element, we can define another variable in the ngFor directive. We can name that variable however we like. Let's call it "i" for now. To get the value of the index, we also need to assign "index" to that variable.

What is ngfor directive in angular?

The so-called ngFor directive is a core directive, that comes with the angular framework itself. We can use this directive, if we want to display a dynamic list, for example, an array of elements on the screen.

How to reduce DOM manipulation with angulars ngfor directive?

To reduce DOM-manipulation to a bare minimum, angulars' ngFor directive is heavily optimized. For example, if a element is added to the array, it is not re-rendering the whole list. Instead, all the existing DOM-elements are re-used and only the additional element is created. The same thing happens when a element is removed from the array.


2 Answers

You can use ng-container to group multiple components without the container rendering.

<ng-container *ngFor="let i of [1,2,3,4,5,6,7]; let _i = index">
  <app-small-book-view></app-small-book-view>
  <div class="clearfix" *ngIf="(_i % 3) == 0"></div>
</ng-container>
like image 81
Explosion Pills Avatar answered Oct 20 '22 23:10

Explosion Pills


This should not be a task for script but rather for CSS:

app-small-book-view:nth-child(3n)::after {
    /*the clearfix thing*/
    content: ""; 
    clear: both;
    display: table;
}

So your markup will be just linear sequence of children:

<app-small-book-view></app-small-book-view>
<app-small-book-view></app-small-book-view>
<app-small-book-view></app-small-book-view>
<app-small-book-view></app-small-book-view>
<app-small-book-view></app-small-book-view>
<app-small-book-view></app-small-book-view>
<app-small-book-view></app-small-book-view>
<app-small-book-view></app-small-book-view>
<app-small-book-view></app-small-book-view>
like image 41
c-smile Avatar answered Oct 20 '22 23:10

c-smile