1

I want to loop my list in bootstrap columns.

  1. In the first row, I would like to show three columns
  2. In the second row, I would like to show two columns
  3. In the third row, I would like to show only one column

The code is below.

<div *ngFor="let watch of List;let i = index;" >  
    <div [ngClass]="{'col-xs-4 col-sm-4 col-md-4 col-lg-4': watch.rowtype == 'three-view', 'col-xs-6 col-sm-6 col-md-6 col-lg-6': watch.rowtype == 'two-view',  'col-xs-12 col-sm-12 col-md-12 col-lg-12': watch.rowtype == 'one-view'}"
         style="text-align:center; background-color:rgb(0, 183, 255);">{{watch.name}}
    </div>
    <div>{{watch.value}}</div>
</div>

It is displaying each column in a single row. Not showing like three, two and one.

3
  • 1
    did you define <div class="row"> before ngFor ? Commented Jan 21, 2019 at 7:34
  • @Robert If we define the row before ngFor all the columns are displaying in a single row. Commented Jan 21, 2019 at 8:01
  • Then you probably need another loop for the rows. Commented Jan 21, 2019 at 8:18

3 Answers 3

3

"If we define the row before ngFor all the columns are displaying in a single row."

Not if you add [ngClass] to the same element that is being iterated:

<div class="row">  
    <div *ngFor="let watch of List;let i = index;" [ngClass]="{'col-xs-4 col-sm-4 col-md-4 col-lg-4': watch.rowtype == 'three-view', 'col-xs-6 col-sm-6 col-md-6 col-lg-6': watch.rowtype == 'two-view',  'col-xs-12 col-sm-12 col-md-12 col-lg-12': watch.rowtype == 'one-view'}"
         style="text-align:center; background-color:rgb(0, 183, 255);">{{watch.name}}
    </div>
    <div>{{watch.value}}</div>
</div>

You also don't need those rowType fields, you could do something like this:

[ngClass]="{'col-xs-4 col-sm-4 col-md-4 col-lg-4': (index / 3 < 1)
Sign up to request clarification or add additional context in comments.

2 Comments

[ngClass]="{'col-xs-4 col-sm-4 col-md-4 col-lg-4': (index / 3 < 1) for three columns it will work and the two and single columns can we use this?
Well sure, I was just making an example. You can change the condition a bit, I'm on a mobile now so can't really code properly.
0

Hello you are missing the class "row" in you *ngFor-div:

<div class="row" *ngFor="let watch of List;let i = index;" >  
   <div [ngClass]="{'col-xs-4 col-sm-4 col-md-4 col-lg-4': watch.rowtype == 'three-view', 'col-xs-6 col-sm-6 col-md-6 col-lg-6': watch.rowtype == 'two-view',  'col-xs-12 col-sm-12 col-md-12 col-lg-12': watch.rowtype == 'one-view'}"
     style="text-align:center; background-color:rgb(0, 183, 255);">{{watch.name}}
</div>
   <div>{{watch.value}}</div>
</div>

Comments

0

May be div with {{watch.value}} should be inside <div [ngClass]=" like this?

            <div *ngFor="let watch of List;let i = index;" [ngClass]="{'col-xs-4 col-sm-4 col-md-4 col-lg-4': watch.rowtype == 'three-view', 'col-xs-6 col-sm-6 col-md-6 col-lg-6': watch.rowtype == 'two-view',  'col-xs-12 col-sm-12 col-md-12 col-lg-12': watch.rowtype == 'one-view'}"
                 style="text-align:center; background-color:rgb(0, 183, 255);">
                 <div>{{watch.name}}<div>
                 <div>{{watch.value}}</div>
            </div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.