All Projects → crazedVic → angularnestedformarrays

crazedVic / angularnestedformarrays

Licence: other
Angular 7 FormArrays nested within each other with FormGroups, dynamic, add, delete

Programming Languages

typescript
32286 projects
HTML
75241 projects
javascript
184084 projects - #8 most used programming language
CSS
56736 projects

Projects that are alternatives of or similar to angularnestedformarrays

libnfporb
Implementation of a robust no-fit polygon generation in a C++ library using an orbiting approach
Stars: ✭ 85 (+325%)
Mutual labels:  nesting
nest2D
Nest2D is a 2D bin packaging tool for python.
Stars: ✭ 42 (+110%)
Mutual labels:  nesting
gallia-core
A schema-aware Scala library for data transformation
Stars: ✭ 44 (+120%)
Mutual labels:  nesting
feathers-versionate
Create and work with nested services.
Stars: ✭ 29 (+45%)
Mutual labels:  nesting

Angular Reactive Forms with lots of Nesting

I looked everywhere for a solution I could just copy and paste into my project but with no luck, so here's my attempt at providing it for the next poor soul stuck in my situation. I've tried to keep the code as simple as possible.

You have a league

league_details: this.fb.group({
        name: "",
        founder: ""
      })

A league has teams

this.leagueForm = this.fb.group({
      league_details: this.fb.group({
        name: "",
        founder: ""
      }),
      teams: this.fb.array([this.teams])
    });

Teams have names and players

get teams(): FormGroup {
    return this.fb.group({
      team_name: "",
      players: this.fb.array([this.players])
    });
  }

Players have names and numbers

get players(): FormGroup {
    return this.fb.group({
      player_name: "",
      player_number: ""
    });
  }

You have a FormGroup

<form [formGroup]="leagueForm">

Show League Detail

<div class="form-header" formGroupName="league_details">
    <label>League Name <input formControlName="name"/></label>
    <label>Founder <input formControlName="founder"/></label>
  </div>

Show Teams

It's VITAL you have a wrapping div that defines formArrayName!

<div formArrayName="teams">
    <div class="teams" *ngFor="let team of leagueForm.get('teams').controls;
             let teamIndex = index" [formGroupName]="teamIndex">
      <label>Team Name <input formControlName="team_name"/></label>
      <!--  div with formArrayName='players' goes here (see below) -->
    </div>
</div>

Show Players inside Teams

Again, important you have a wrapping div with formArrayName. Also, you need to add players in context of the team. Notice in ngFor i am referencing the team variable defined in the ngFor loop above for teams. I will also need to reference this team when removing or adding players.

Also notice the <span> element, this is how you reference the field for validation functionality.

<div formArrayName="players">
    <div class="players" *ngFor="let player of team.get('players').controls;
        let playerIndex=index" [formGroupName]="playerIndex">
        <label>Player Name <input formControlName="player_name" />
        <label>Player Number <input formControlName="player_number"/></label>
        <span *ngIf="player.get('player_number').touched">touched!</span>
    </div>
</div>

Adding Teams

Place ADD button just above the div that defines the formArrayArrayName teams

 <button type="button" (click)="addTeam()">Add Team</button>
addTeam() {
    (this.leagueForm.get("teams") as FormArray).push(this.teams);
  }

Adding Players

Place ADD button just above the div that defines the formArrayName players, and remember to pass in the team variable from the enclosing ngFor loop.

<button type="button" (click)="addPlayer(team)">Add Player</button>
addPlayer(team) {
    team.get("players").push(this.players);
  }
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].