Angular: Insert new .row on ngFor condition

2019-08-03 07:19发布

问题:

I'm new to Angular so I decided to try their tutorial, but with some small alterations. Basically I have the following array:

export const HEROES: Hero[] = [
  { id: 11, name: 'Mr. Nice' },
  { id: 12, name: 'Narco' },
  { id: 13, name: 'Bombasto' },
  { id: 14, name: 'Celeritas' },
  { id: 15, name: 'Magneta' },
  { id: 16, name: 'RubberMan' },
  { id: 17, name: 'Dynama' },
  { id: 18, name: 'Dr IQ' },
  { id: 19, name: 'Magma' },
  { id: 20, name: 'Tornado' }
];

And the objective is to list them on screen using *ngFor, so I looked up for some ways to do it and this was the most logical thing for me to do, but it's not working.

<div class="container-fluid bg-secondary padding-sm margin-sm rounded">
    <div class="row">

        <div *ngFor="let hero of heroes; let index = index">

            <div class="col-lg-4 bg-lightblue rounded">

                <p class="text-secondary lead">
                    <span  class="text-info text-uppercase lead font-weight-bold">
                        {{ hero.name}}
                    </span> 

                    Details
                </p>

                <p>
                    <span class="font-weight-bold text-success">
                        ID: 
                    </span>

                    {{ hero.id }}
                </p>

                <p>
                    <span class="font-weight-bold text-success">
                        Name: 
                    </span>

                    {{ hero.name }}
                </p>

                <label class="font-weight-bold">Change name: 
                    <input [(ngModel)]="hero.name" class="input-md" type="text" placeholder="name">
                </label>

            </div>

            {{ (index + 1) % 3 }}

            <div class="row" *ngIf="(index + 1) % 3 == 0"></div>

        </div>

    </div>
</div>

What I'm attempting to do is to start cycling through the list and when it reaches 3 columns, it adds a row by checking if (index + 1) % 3 equals 0 and continues adding columns on the next row. Right now it's not adding the row when the condition in ngIf is true. What am I doing wrong?

回答1:

declare heroes and update it as you want to display in HTML i.e. 3 columns in a row

heroes = [];
HEROES.forEach((hero, index) => {
    if(index % 3 == 0) {
        let row = [];
        row.push(hero);
        this.heroes.push(row);
    } else {
        this.heroes[this.heroes.length - 1].push(hero);
    }
});

it will generate heroes as:

[ 
  [ { "id": 11, "name": "Mr. Nice" }, { "id": 12, "name": "Narco" }, { "id": 13, "name": "Bombasto" } ], 
  [ { "id": 14, "name": "Celeritas" }, { "id": 15, "name": "Magneta" }, { "id": 16, "name": "RubberMan" } ], 
  [ { "id": 17, "name": "Dynama" }, { "id": 18, "name": "Dr IQ" }, { "id": 19, "name": "Magma" } ], [ { "id": 20, "name": "Tornado" } ] 
]

then you can update HTML as you want.

HTML

<div class="row" *ngFor="let rowData of heroes">
    <div class="col-lg-4 bg-lightblue rounded" *ngFor="let hero of rowData">
        <p class="text-secondary lead">
            <span  class="text-info text-uppercase lead font-weight-bold">{{ hero.name}}</span> Details
        </p>
        <p>
            <span class="font-weight-bold text-success">ID: </span>{{ hero.id }}
        </p>
        <p>
            <span class="font-weight-bold text-success">Name: </span>{{ hero.name }}
        </p>
        <label class="font-weight-bold">Change name: 
            <input [(ngModel)]="hero.name" class="input-md" type="text" placeholder="name">
        </label>
    </div>
</div>