How to create a new row of cards using ngFor and bootstrap 4
Asked Answered
W

3

12

I'm trying to use the card group functionality of Bootstrap 4 with Angular ngFor.

Here is the HTML I have for now, but I can't find how to break to a new line after 3 items have been inserted:

<div class="row card-group">
    <div *ngFor="let presentation of presentations" class="col-4 card">
        <a [routerLink]="['/presentation', presentation._id]">{{presentation.title}}</a>
    </div>
</div>

I've seen other answer saying to use the clearfix class, but this has not worked so far for me.

Wolford answered 2/5, 2017 at 20:28 Comment(3)
Why use card-group? It doesn't allow wrapping the way a row>col does.Sicular
@zimSystem Because I want the cards to have the same height on within each row. So I basically want o mulitple rows of cardgroupsWolford
The row is already same height because BS4 uses flexbox. You don't need to iterate the row.Sicular
C
20

You need a wrapping div with the class col-4 arroud the <div> with card class. thats how grid layout works.

see using grid markup section here: https://v4-alpha.getbootstrap.com/components/card/#using-grid-markup

so:

<div class="row card-group">
    <div class="col-4"  *ngFor="let presentation of presentations">
        <div class="card">
            <a [routerLink]="['/presentation', presentation._id]">{{presentation.title}}</a>
        </div>
    </div>
</div>

sample plunker: https://plnkr.co/edit/8LDBMorXBB1OqI0bolS6?p=preview

Chigoe answered 2/5, 2017 at 20:46 Comment(3)
It works but if the card content of the card don't have the same height, the card group are not sized appropriately see plnkr.co/edit/QeFpHMVyQwo81SsGfeTd?p=previewWolford
yeah, that is not going to work without CSS changes. The only other option you have is to split the array into arrays of 3. here is a sample plunker: plnkr.co/edit/jUtAEEpyeGTYtVNUDLQS?p=preview. Note that with this approach, each 3 itesm will have the same height BUT not all items will. Note, expand the preview window to see it working.Chigoe
This helped a lot! ThanksAlon
W
1

Thanks to @zimSystem I found something that works.

<div class="row">
    <div *ngFor="let presentation of presentations" class="col-4 card">
        <a [routerLink]="['/presentation', presentation._id]"><img class="card-img-top" src="..." alt="Card image cap"></a>
        <div class="card-block">
            ...
        </div>
    </div>
</div>
Wolford answered 3/5, 2017 at 7:41 Comment(1)
Good job! I modified this way using your code - <div class="row"> <div class="col-3 card" *ngFor="let project of projects"> ... </div> </div>Matroclinous
S
0

The card-group is preventing the col's from wrapping to the next "line". Place the cards inside the columns, and instead use h-100 to make the cards full/same height. This way the Angular code won't need to iterate the row.

    <div class="row">
        <div class="col-4">
            <div class="card h-100">
                ..
            </div>
        </div>
        <div class="col-4">
            <div class="card h-100">
            ..
            </div>
        </div>
    </div>

http://www.codeply.com/go/yWdYL5GrTu

Sicular answered 2/5, 2017 at 20:56 Comment(3)
Unfortunately if the content of the text on each card is a different number of line, then the cards don't have the same height.Wolford
This works..but it stretches the height of the imageSacculate
@DaveAAA what image? there's no mention of an image in the question.Sicular

© 2022 - 2024 — McMap. All rights reserved.