html - Using *ngFor in CSS Grid Layout Undesirably Displaying Everything in One Column -


i new programming , web dev in general. in process of making personal website.

i want put boxes in grid 4 columns, similar see here: https://devpost.com/software/search?query=is%3afeatured

each of these boxes represents object, , want able display of data box, , rest of data in popup dialog when click on box.

i have playing around css grid layout, becoming popular these days (highly recommend video: https://www.youtube.com/watch?v=7kvecqqcxlk).

the thing 4 columns work when hardcode bunch of div elements inside wrapper div. however, whenever use *ngfor on wrapper containing array of data , feed in data each iteration inner div element, grid layout gets destroyed, placing 1 column.

when manually feed in data using multiple div elements (item2 here), works desired:

.wrapper {      margin-left: 1em;      margin-right: 1em;      display: grid;      grid-template-rows: auto;      grid-template-columns: repeat(4, 1fr);      grid-row-gap: 1em;      grid-column-gap: 1em;  }
<div class="wrapper">        <div class="item2" style="background-color: deepskyblue;">          <img class="img-responsive img-rounded" src="assets/logo/square_filler.png" alt="pic-test" >          <p>more text here.</p>      </div>        <div class="item2" style="background-color: deepskyblue;">          <img class="img-responsive img-rounded" src="assets/logo/square_filler.png" alt="pic-test" >          <p>more text here.</p>      </div>        <div class="item2" style="background-color: deepskyblue;">          <img class="img-responsive img-rounded" src="assets/logo/square_filler.png" alt="pic-test" >          <p>more text here.</p>      </div>        <div class="item2" style="background-color: deepskyblue;">          <img class="img-responsive img-rounded" src="assets/logo/square_filler.png" alt="pic-test" >          <p>more text here.</p>      </div>        <div class="item2" style="background-color: deepskyblue;">          <img class="img-responsive img-rounded" src="assets/logo/square_filler.png" alt="pic-test" >          <p>more text here.</p>      </div>        </div>

using *ngfor... runs length of array "stickynotes," stacks boxes below each row.

<div class="wrapper" *ngfor="let s of stickynotes; let = index" >      <div class="item2" style="background-color: deepskyblue;">          <img class="img-responsive img-rounded" src="assets/logo/square_filler.png" alt="pic-test" >          <p>more text here.</p>          <p>{{s.title}}</p>      </div>  </div>

my hack around was... have div element 4 times incrementing 1 on *ngif (i+1, i+2, etc.). problem here when new row starts, grid-row-gap in css ignored.

<div class="item2" style="background-color: deepskyblue;" *ngif="i < stickynotes.length && i%4 === 0">        <img class="img-responsive img-rounded" src="assets/logo/square_filler.png" alt="pic-test" >        <p>more text here.</p>        <p>{{stickynotes[i].title}}</p>  </div>    <div class="item2" style="background-color: deepskyblue;" *ngif="i+1 < stickynotes.length && i%4 === 0">        <img class="img-responsive img-rounded" src="assets/logo/square_filler.png" alt="pic-test" >        <p>more text here.</p>        <p>{{stickynotes[i+1].title}}</p>  </div>    <!--2 more times until i+3-->

i wondering if there way create custom iterable directive without destroying grid property , without having item2 class coded 4 times (or other methods).

i have tried bootstrap row , col attributes, 4 columns, responsiveness wasn't nice grid layout.

any or suggestion appreciated!

*ngfor repeats element add to. put on div.item2 , should work:

<div class="wrapper" >     <div class="item2" *ngfor="let s of stickynotes; let = index" style="background-color: deepskyblue;" >         <img class="img-responsive img-rounded" src="assets/logo/square_filler.png" alt="pic-test" >         <p>more text here.</p>         <p>{{s.title}}</p>    </div> </div> 

the answer provided vega work too, <ng-container> not required(as long don't put structural directive on div.item2element), because asterisk syntactic sugar , expands something similar:

<div class="wrapper" >   <ng-template ngfor let-s [ngforof]="stickynotes" let-i="index">     <div class="item2" style="background-color: deepskyblue;">       <img class="img-responsive img-rounded" src="assets/logo/square_filler.png" alt="pic-test" >       <p>more text here.</p>       <p>{{s.title}}</p>     </div>   </ng-template> </div> 

the reason behind limit of 1 structural directive per element given here:

someday you'll want repeat block of html when particular condition true. you'll try put both *ngfor , *ngif on same host element. angular won't let you. may apply 1 structural directive element.

the reason simplicity. structural directives can complex things host element , descendents. when 2 directives lay claim same host element, 1 takes precedence? should go first, ngif or ngfor? can ngif cancel effect of ngfor? if (and seems should so), how should angular generalize ability cancel other structural directives?

there no easy answers these questions. prohibiting multiple structural directives makes them moot. there's easy solution use case: put *ngif on container element wraps *ngfor element. 1 or both elements can ng-container don't have introduce levels of html.


Comments

Popular posts from this blog

neo4j - finding mutual friends in a cypher statement starting with three or more persons -

php - How to remove letter in front of the word laravel -

minify - Minimizing css files -