Add a row to table on click of add button the new row is form fields in each cell












1















Actually on add click I need to add row its getting added but I am not able to get the ngmodel object Or Is there any other best way to implement using reactive forms so finally requirement is to get a row on add click and get the form value best way of implementation or please modify the above code



Stackblitz Link










share|improve this question



























    1















    Actually on add click I need to add row its getting added but I am not able to get the ngmodel object Or Is there any other best way to implement using reactive forms so finally requirement is to get a row on add click and get the form value best way of implementation or please modify the above code



    Stackblitz Link










    share|improve this question

























      1












      1








      1








      Actually on add click I need to add row its getting added but I am not able to get the ngmodel object Or Is there any other best way to implement using reactive forms so finally requirement is to get a row on add click and get the form value best way of implementation or please modify the above code



      Stackblitz Link










      share|improve this question














      Actually on add click I need to add row its getting added but I am not able to get the ngmodel object Or Is there any other best way to implement using reactive forms so finally requirement is to get a row on add click and get the form value best way of implementation or please modify the above code



      Stackblitz Link







      angular angular-reactive-forms angular-forms






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 28 '18 at 6:18









      Balaji VBalaji V

      576




      576
























          2 Answers
          2






          active

          oldest

          votes


















          1














          Check live demo here.



          http://keepnote.cc/code/form-group-with-formarray-adding-dyamic-row-angular-5-6



          Please check this.



          We have to use FormBuilder and FormBuilder.array for dynamic rows.



          html



          <form [formGroup]="carForm" (ngSubmit)="saveIntergration()">
          <div formArrayName="details" class="form-group" *ngFor="let field of carForm.get('details').controls; let ind = index;">
          <div [formGroupName]="ind">
          Type:
          <input type="text" formControlName="type">

          model:
          <input type="text" formControlName="model">

          year:
          <input type="text" formControlName="year">

          make:
          <input type="text" formControlName="make">

          color
          <input type="text" formControlName="color">

          plateNumber
          <input type="text" formControlName="plateNumber">

          <hr>
          </div>
          </div>
          </form>

          <button (click)="addRow()">Add New</button>

          <pre>
          {{carForm.value | json}}
          </pre>


          ts



          import { Component, ViewChild } from '@angular/core';
          import { FormBuilder, FormGroup, FormControl, Validators, FormArray } from '@angular/forms';

          @Component({
          selector: 'app-root',
          templateUrl: './app.component.html'

          })
          export class AppComponent {
          public carForm: FormGroup;
          constructor(private fb: FormBuilder) {

          const items = ;
          items.push(this.fb.group({
          type: ,
          model: ,
          year: ,
          make: ,
          color: ,
          plateNumber:
          }));

          this.carForm = this.fb.group({
          details: this.fb.array( items )
          });
          }

          addRow() {
          const details = this.carForm.get('details') as FormArray;
          details.push(this.createItem());
          }

          createItem(): FormGroup {
          return this.fb.group({
          type: ,
          model: ,
          year: ,
          make: ,
          color: ,
          plateNumber:
          });
          }
          }





          share|improve this answer


























          • Thanks bro it worked also my work is done how to make the form displayed only on add click initially I am getting a row is it possible ?

            – Balaji V
            Nov 28 '18 at 6:54











          • Cool.! Please accept that answer. In constructor loaded one items.

            – Paresh Gami
            Nov 28 '18 at 6:55











          • Load one item means

            – Balaji V
            Nov 28 '18 at 6:59











          • Explain me in details. make the form displayed only on add click initially I am getting a row is it possible

            – Paresh Gami
            Nov 28 '18 at 7:00











          • So before add the form getting initialized is possible to initial after add click

            – Balaji V
            Nov 28 '18 at 7:03



















          0















          Reactive Forms in Angular: Dynamically Creating Form Fields With FormArray.




          Please alter your code as per given sample.



          HTML:



          <form [formGroup]="orderForm" (submit)="onSubmit()">
          <div class="row">
          <button type="button" (click)="addItem()">add</button></div>
          <div *ngFor="let item of data" class="row">
          <div class="col">{{item.Type}}</div>
          <div class="col">{{item.Model}}</div>
          <div class="col">{{item.Year}}</div>
          <div class="col">{{item.Make}}</div>
          <div class="col">{{item.Color}}</div>
          <div class="col">{{item.PlateNumber}}</div>
          <div class="col">{{item.StateRegistered}}</div>
          </div>
          <div formArrayName="items" *ngFor="let item of orderForm.get('items').controls; let i = index;" class="row">
          <div [formGroupName]="i">
          <input formControlName="Type">
          <input formControlName="Model">
          <input formControlName="Year">
          <input formControlName="Make">
          <input formControlName="Color">
          <input formControlName="PlateNumber">
          <input formControlName="StateRegistered">
          </div>
          </div>
          <div class="row">
          <button type="submit">Submit</button>
          </div>
          </form>


          TS:



          import { Component, OnInit } from '@angular/core';
          import { FormGroup, FormArray, FormBuilder } from '@angular/forms';

          @Component({
          selector: 'app-stack53513269',
          templateUrl: './stack53513269.component.html',
          styleUrls: ['./stack53513269.component.css']
          })
          export class Stack53513269Component implements OnInit {
          orderForm: FormGroup;
          items: FormArray;
          data: any = ;
          constructor(private formBuilder: FormBuilder) {
          this.data.push({ Type: 'SUV', Model: '500', Year: '2009', Make:'Mahinda', Color:'Red', PlateNumber:'RJ-15', StateRegistered:'Rajsthan' });
          }

          ngOnInit() {
          this.orderForm = this.formBuilder.group({
          items: this.formBuilder.array([this.createItem()])
          });
          }

          createItem(): FormGroup {
          return this.formBuilder.group({
          Type:'', Model:'', Year:'', Make:'', Color:'', PlateNumber:'', StateRegistered:''
          });
          }

          addItem(): void {
          this.items = this.orderForm.get('items') as FormArray;
          this.items.push(this.createItem());
          }

          onSubmit() {
          this.items = this.orderForm.get('items') as FormArray;
          console.log(this.items.value);
          }
          }





          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53513269%2fadd-a-row-to-table-on-click-of-add-button-the-new-row-is-form-fields-in-each-cel%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            Check live demo here.



            http://keepnote.cc/code/form-group-with-formarray-adding-dyamic-row-angular-5-6



            Please check this.



            We have to use FormBuilder and FormBuilder.array for dynamic rows.



            html



            <form [formGroup]="carForm" (ngSubmit)="saveIntergration()">
            <div formArrayName="details" class="form-group" *ngFor="let field of carForm.get('details').controls; let ind = index;">
            <div [formGroupName]="ind">
            Type:
            <input type="text" formControlName="type">

            model:
            <input type="text" formControlName="model">

            year:
            <input type="text" formControlName="year">

            make:
            <input type="text" formControlName="make">

            color
            <input type="text" formControlName="color">

            plateNumber
            <input type="text" formControlName="plateNumber">

            <hr>
            </div>
            </div>
            </form>

            <button (click)="addRow()">Add New</button>

            <pre>
            {{carForm.value | json}}
            </pre>


            ts



            import { Component, ViewChild } from '@angular/core';
            import { FormBuilder, FormGroup, FormControl, Validators, FormArray } from '@angular/forms';

            @Component({
            selector: 'app-root',
            templateUrl: './app.component.html'

            })
            export class AppComponent {
            public carForm: FormGroup;
            constructor(private fb: FormBuilder) {

            const items = ;
            items.push(this.fb.group({
            type: ,
            model: ,
            year: ,
            make: ,
            color: ,
            plateNumber:
            }));

            this.carForm = this.fb.group({
            details: this.fb.array( items )
            });
            }

            addRow() {
            const details = this.carForm.get('details') as FormArray;
            details.push(this.createItem());
            }

            createItem(): FormGroup {
            return this.fb.group({
            type: ,
            model: ,
            year: ,
            make: ,
            color: ,
            plateNumber:
            });
            }
            }





            share|improve this answer


























            • Thanks bro it worked also my work is done how to make the form displayed only on add click initially I am getting a row is it possible ?

              – Balaji V
              Nov 28 '18 at 6:54











            • Cool.! Please accept that answer. In constructor loaded one items.

              – Paresh Gami
              Nov 28 '18 at 6:55











            • Load one item means

              – Balaji V
              Nov 28 '18 at 6:59











            • Explain me in details. make the form displayed only on add click initially I am getting a row is it possible

              – Paresh Gami
              Nov 28 '18 at 7:00











            • So before add the form getting initialized is possible to initial after add click

              – Balaji V
              Nov 28 '18 at 7:03
















            1














            Check live demo here.



            http://keepnote.cc/code/form-group-with-formarray-adding-dyamic-row-angular-5-6



            Please check this.



            We have to use FormBuilder and FormBuilder.array for dynamic rows.



            html



            <form [formGroup]="carForm" (ngSubmit)="saveIntergration()">
            <div formArrayName="details" class="form-group" *ngFor="let field of carForm.get('details').controls; let ind = index;">
            <div [formGroupName]="ind">
            Type:
            <input type="text" formControlName="type">

            model:
            <input type="text" formControlName="model">

            year:
            <input type="text" formControlName="year">

            make:
            <input type="text" formControlName="make">

            color
            <input type="text" formControlName="color">

            plateNumber
            <input type="text" formControlName="plateNumber">

            <hr>
            </div>
            </div>
            </form>

            <button (click)="addRow()">Add New</button>

            <pre>
            {{carForm.value | json}}
            </pre>


            ts



            import { Component, ViewChild } from '@angular/core';
            import { FormBuilder, FormGroup, FormControl, Validators, FormArray } from '@angular/forms';

            @Component({
            selector: 'app-root',
            templateUrl: './app.component.html'

            })
            export class AppComponent {
            public carForm: FormGroup;
            constructor(private fb: FormBuilder) {

            const items = ;
            items.push(this.fb.group({
            type: ,
            model: ,
            year: ,
            make: ,
            color: ,
            plateNumber:
            }));

            this.carForm = this.fb.group({
            details: this.fb.array( items )
            });
            }

            addRow() {
            const details = this.carForm.get('details') as FormArray;
            details.push(this.createItem());
            }

            createItem(): FormGroup {
            return this.fb.group({
            type: ,
            model: ,
            year: ,
            make: ,
            color: ,
            plateNumber:
            });
            }
            }





            share|improve this answer


























            • Thanks bro it worked also my work is done how to make the form displayed only on add click initially I am getting a row is it possible ?

              – Balaji V
              Nov 28 '18 at 6:54











            • Cool.! Please accept that answer. In constructor loaded one items.

              – Paresh Gami
              Nov 28 '18 at 6:55











            • Load one item means

              – Balaji V
              Nov 28 '18 at 6:59











            • Explain me in details. make the form displayed only on add click initially I am getting a row is it possible

              – Paresh Gami
              Nov 28 '18 at 7:00











            • So before add the form getting initialized is possible to initial after add click

              – Balaji V
              Nov 28 '18 at 7:03














            1












            1








            1







            Check live demo here.



            http://keepnote.cc/code/form-group-with-formarray-adding-dyamic-row-angular-5-6



            Please check this.



            We have to use FormBuilder and FormBuilder.array for dynamic rows.



            html



            <form [formGroup]="carForm" (ngSubmit)="saveIntergration()">
            <div formArrayName="details" class="form-group" *ngFor="let field of carForm.get('details').controls; let ind = index;">
            <div [formGroupName]="ind">
            Type:
            <input type="text" formControlName="type">

            model:
            <input type="text" formControlName="model">

            year:
            <input type="text" formControlName="year">

            make:
            <input type="text" formControlName="make">

            color
            <input type="text" formControlName="color">

            plateNumber
            <input type="text" formControlName="plateNumber">

            <hr>
            </div>
            </div>
            </form>

            <button (click)="addRow()">Add New</button>

            <pre>
            {{carForm.value | json}}
            </pre>


            ts



            import { Component, ViewChild } from '@angular/core';
            import { FormBuilder, FormGroup, FormControl, Validators, FormArray } from '@angular/forms';

            @Component({
            selector: 'app-root',
            templateUrl: './app.component.html'

            })
            export class AppComponent {
            public carForm: FormGroup;
            constructor(private fb: FormBuilder) {

            const items = ;
            items.push(this.fb.group({
            type: ,
            model: ,
            year: ,
            make: ,
            color: ,
            plateNumber:
            }));

            this.carForm = this.fb.group({
            details: this.fb.array( items )
            });
            }

            addRow() {
            const details = this.carForm.get('details') as FormArray;
            details.push(this.createItem());
            }

            createItem(): FormGroup {
            return this.fb.group({
            type: ,
            model: ,
            year: ,
            make: ,
            color: ,
            plateNumber:
            });
            }
            }





            share|improve this answer















            Check live demo here.



            http://keepnote.cc/code/form-group-with-formarray-adding-dyamic-row-angular-5-6



            Please check this.



            We have to use FormBuilder and FormBuilder.array for dynamic rows.



            html



            <form [formGroup]="carForm" (ngSubmit)="saveIntergration()">
            <div formArrayName="details" class="form-group" *ngFor="let field of carForm.get('details').controls; let ind = index;">
            <div [formGroupName]="ind">
            Type:
            <input type="text" formControlName="type">

            model:
            <input type="text" formControlName="model">

            year:
            <input type="text" formControlName="year">

            make:
            <input type="text" formControlName="make">

            color
            <input type="text" formControlName="color">

            plateNumber
            <input type="text" formControlName="plateNumber">

            <hr>
            </div>
            </div>
            </form>

            <button (click)="addRow()">Add New</button>

            <pre>
            {{carForm.value | json}}
            </pre>


            ts



            import { Component, ViewChild } from '@angular/core';
            import { FormBuilder, FormGroup, FormControl, Validators, FormArray } from '@angular/forms';

            @Component({
            selector: 'app-root',
            templateUrl: './app.component.html'

            })
            export class AppComponent {
            public carForm: FormGroup;
            constructor(private fb: FormBuilder) {

            const items = ;
            items.push(this.fb.group({
            type: ,
            model: ,
            year: ,
            make: ,
            color: ,
            plateNumber:
            }));

            this.carForm = this.fb.group({
            details: this.fb.array( items )
            });
            }

            addRow() {
            const details = this.carForm.get('details') as FormArray;
            details.push(this.createItem());
            }

            createItem(): FormGroup {
            return this.fb.group({
            type: ,
            model: ,
            year: ,
            make: ,
            color: ,
            plateNumber:
            });
            }
            }






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Feb 7 at 16:55

























            answered Nov 28 '18 at 6:46









            Paresh GamiParesh Gami

            3,43331429




            3,43331429













            • Thanks bro it worked also my work is done how to make the form displayed only on add click initially I am getting a row is it possible ?

              – Balaji V
              Nov 28 '18 at 6:54











            • Cool.! Please accept that answer. In constructor loaded one items.

              – Paresh Gami
              Nov 28 '18 at 6:55











            • Load one item means

              – Balaji V
              Nov 28 '18 at 6:59











            • Explain me in details. make the form displayed only on add click initially I am getting a row is it possible

              – Paresh Gami
              Nov 28 '18 at 7:00











            • So before add the form getting initialized is possible to initial after add click

              – Balaji V
              Nov 28 '18 at 7:03



















            • Thanks bro it worked also my work is done how to make the form displayed only on add click initially I am getting a row is it possible ?

              – Balaji V
              Nov 28 '18 at 6:54











            • Cool.! Please accept that answer. In constructor loaded one items.

              – Paresh Gami
              Nov 28 '18 at 6:55











            • Load one item means

              – Balaji V
              Nov 28 '18 at 6:59











            • Explain me in details. make the form displayed only on add click initially I am getting a row is it possible

              – Paresh Gami
              Nov 28 '18 at 7:00











            • So before add the form getting initialized is possible to initial after add click

              – Balaji V
              Nov 28 '18 at 7:03

















            Thanks bro it worked also my work is done how to make the form displayed only on add click initially I am getting a row is it possible ?

            – Balaji V
            Nov 28 '18 at 6:54





            Thanks bro it worked also my work is done how to make the form displayed only on add click initially I am getting a row is it possible ?

            – Balaji V
            Nov 28 '18 at 6:54













            Cool.! Please accept that answer. In constructor loaded one items.

            – Paresh Gami
            Nov 28 '18 at 6:55





            Cool.! Please accept that answer. In constructor loaded one items.

            – Paresh Gami
            Nov 28 '18 at 6:55













            Load one item means

            – Balaji V
            Nov 28 '18 at 6:59





            Load one item means

            – Balaji V
            Nov 28 '18 at 6:59













            Explain me in details. make the form displayed only on add click initially I am getting a row is it possible

            – Paresh Gami
            Nov 28 '18 at 7:00





            Explain me in details. make the form displayed only on add click initially I am getting a row is it possible

            – Paresh Gami
            Nov 28 '18 at 7:00













            So before add the form getting initialized is possible to initial after add click

            – Balaji V
            Nov 28 '18 at 7:03





            So before add the form getting initialized is possible to initial after add click

            – Balaji V
            Nov 28 '18 at 7:03













            0















            Reactive Forms in Angular: Dynamically Creating Form Fields With FormArray.




            Please alter your code as per given sample.



            HTML:



            <form [formGroup]="orderForm" (submit)="onSubmit()">
            <div class="row">
            <button type="button" (click)="addItem()">add</button></div>
            <div *ngFor="let item of data" class="row">
            <div class="col">{{item.Type}}</div>
            <div class="col">{{item.Model}}</div>
            <div class="col">{{item.Year}}</div>
            <div class="col">{{item.Make}}</div>
            <div class="col">{{item.Color}}</div>
            <div class="col">{{item.PlateNumber}}</div>
            <div class="col">{{item.StateRegistered}}</div>
            </div>
            <div formArrayName="items" *ngFor="let item of orderForm.get('items').controls; let i = index;" class="row">
            <div [formGroupName]="i">
            <input formControlName="Type">
            <input formControlName="Model">
            <input formControlName="Year">
            <input formControlName="Make">
            <input formControlName="Color">
            <input formControlName="PlateNumber">
            <input formControlName="StateRegistered">
            </div>
            </div>
            <div class="row">
            <button type="submit">Submit</button>
            </div>
            </form>


            TS:



            import { Component, OnInit } from '@angular/core';
            import { FormGroup, FormArray, FormBuilder } from '@angular/forms';

            @Component({
            selector: 'app-stack53513269',
            templateUrl: './stack53513269.component.html',
            styleUrls: ['./stack53513269.component.css']
            })
            export class Stack53513269Component implements OnInit {
            orderForm: FormGroup;
            items: FormArray;
            data: any = ;
            constructor(private formBuilder: FormBuilder) {
            this.data.push({ Type: 'SUV', Model: '500', Year: '2009', Make:'Mahinda', Color:'Red', PlateNumber:'RJ-15', StateRegistered:'Rajsthan' });
            }

            ngOnInit() {
            this.orderForm = this.formBuilder.group({
            items: this.formBuilder.array([this.createItem()])
            });
            }

            createItem(): FormGroup {
            return this.formBuilder.group({
            Type:'', Model:'', Year:'', Make:'', Color:'', PlateNumber:'', StateRegistered:''
            });
            }

            addItem(): void {
            this.items = this.orderForm.get('items') as FormArray;
            this.items.push(this.createItem());
            }

            onSubmit() {
            this.items = this.orderForm.get('items') as FormArray;
            console.log(this.items.value);
            }
            }





            share|improve this answer




























              0















              Reactive Forms in Angular: Dynamically Creating Form Fields With FormArray.




              Please alter your code as per given sample.



              HTML:



              <form [formGroup]="orderForm" (submit)="onSubmit()">
              <div class="row">
              <button type="button" (click)="addItem()">add</button></div>
              <div *ngFor="let item of data" class="row">
              <div class="col">{{item.Type}}</div>
              <div class="col">{{item.Model}}</div>
              <div class="col">{{item.Year}}</div>
              <div class="col">{{item.Make}}</div>
              <div class="col">{{item.Color}}</div>
              <div class="col">{{item.PlateNumber}}</div>
              <div class="col">{{item.StateRegistered}}</div>
              </div>
              <div formArrayName="items" *ngFor="let item of orderForm.get('items').controls; let i = index;" class="row">
              <div [formGroupName]="i">
              <input formControlName="Type">
              <input formControlName="Model">
              <input formControlName="Year">
              <input formControlName="Make">
              <input formControlName="Color">
              <input formControlName="PlateNumber">
              <input formControlName="StateRegistered">
              </div>
              </div>
              <div class="row">
              <button type="submit">Submit</button>
              </div>
              </form>


              TS:



              import { Component, OnInit } from '@angular/core';
              import { FormGroup, FormArray, FormBuilder } from '@angular/forms';

              @Component({
              selector: 'app-stack53513269',
              templateUrl: './stack53513269.component.html',
              styleUrls: ['./stack53513269.component.css']
              })
              export class Stack53513269Component implements OnInit {
              orderForm: FormGroup;
              items: FormArray;
              data: any = ;
              constructor(private formBuilder: FormBuilder) {
              this.data.push({ Type: 'SUV', Model: '500', Year: '2009', Make:'Mahinda', Color:'Red', PlateNumber:'RJ-15', StateRegistered:'Rajsthan' });
              }

              ngOnInit() {
              this.orderForm = this.formBuilder.group({
              items: this.formBuilder.array([this.createItem()])
              });
              }

              createItem(): FormGroup {
              return this.formBuilder.group({
              Type:'', Model:'', Year:'', Make:'', Color:'', PlateNumber:'', StateRegistered:''
              });
              }

              addItem(): void {
              this.items = this.orderForm.get('items') as FormArray;
              this.items.push(this.createItem());
              }

              onSubmit() {
              this.items = this.orderForm.get('items') as FormArray;
              console.log(this.items.value);
              }
              }





              share|improve this answer


























                0












                0








                0








                Reactive Forms in Angular: Dynamically Creating Form Fields With FormArray.




                Please alter your code as per given sample.



                HTML:



                <form [formGroup]="orderForm" (submit)="onSubmit()">
                <div class="row">
                <button type="button" (click)="addItem()">add</button></div>
                <div *ngFor="let item of data" class="row">
                <div class="col">{{item.Type}}</div>
                <div class="col">{{item.Model}}</div>
                <div class="col">{{item.Year}}</div>
                <div class="col">{{item.Make}}</div>
                <div class="col">{{item.Color}}</div>
                <div class="col">{{item.PlateNumber}}</div>
                <div class="col">{{item.StateRegistered}}</div>
                </div>
                <div formArrayName="items" *ngFor="let item of orderForm.get('items').controls; let i = index;" class="row">
                <div [formGroupName]="i">
                <input formControlName="Type">
                <input formControlName="Model">
                <input formControlName="Year">
                <input formControlName="Make">
                <input formControlName="Color">
                <input formControlName="PlateNumber">
                <input formControlName="StateRegistered">
                </div>
                </div>
                <div class="row">
                <button type="submit">Submit</button>
                </div>
                </form>


                TS:



                import { Component, OnInit } from '@angular/core';
                import { FormGroup, FormArray, FormBuilder } from '@angular/forms';

                @Component({
                selector: 'app-stack53513269',
                templateUrl: './stack53513269.component.html',
                styleUrls: ['./stack53513269.component.css']
                })
                export class Stack53513269Component implements OnInit {
                orderForm: FormGroup;
                items: FormArray;
                data: any = ;
                constructor(private formBuilder: FormBuilder) {
                this.data.push({ Type: 'SUV', Model: '500', Year: '2009', Make:'Mahinda', Color:'Red', PlateNumber:'RJ-15', StateRegistered:'Rajsthan' });
                }

                ngOnInit() {
                this.orderForm = this.formBuilder.group({
                items: this.formBuilder.array([this.createItem()])
                });
                }

                createItem(): FormGroup {
                return this.formBuilder.group({
                Type:'', Model:'', Year:'', Make:'', Color:'', PlateNumber:'', StateRegistered:''
                });
                }

                addItem(): void {
                this.items = this.orderForm.get('items') as FormArray;
                this.items.push(this.createItem());
                }

                onSubmit() {
                this.items = this.orderForm.get('items') as FormArray;
                console.log(this.items.value);
                }
                }





                share|improve this answer














                Reactive Forms in Angular: Dynamically Creating Form Fields With FormArray.




                Please alter your code as per given sample.



                HTML:



                <form [formGroup]="orderForm" (submit)="onSubmit()">
                <div class="row">
                <button type="button" (click)="addItem()">add</button></div>
                <div *ngFor="let item of data" class="row">
                <div class="col">{{item.Type}}</div>
                <div class="col">{{item.Model}}</div>
                <div class="col">{{item.Year}}</div>
                <div class="col">{{item.Make}}</div>
                <div class="col">{{item.Color}}</div>
                <div class="col">{{item.PlateNumber}}</div>
                <div class="col">{{item.StateRegistered}}</div>
                </div>
                <div formArrayName="items" *ngFor="let item of orderForm.get('items').controls; let i = index;" class="row">
                <div [formGroupName]="i">
                <input formControlName="Type">
                <input formControlName="Model">
                <input formControlName="Year">
                <input formControlName="Make">
                <input formControlName="Color">
                <input formControlName="PlateNumber">
                <input formControlName="StateRegistered">
                </div>
                </div>
                <div class="row">
                <button type="submit">Submit</button>
                </div>
                </form>


                TS:



                import { Component, OnInit } from '@angular/core';
                import { FormGroup, FormArray, FormBuilder } from '@angular/forms';

                @Component({
                selector: 'app-stack53513269',
                templateUrl: './stack53513269.component.html',
                styleUrls: ['./stack53513269.component.css']
                })
                export class Stack53513269Component implements OnInit {
                orderForm: FormGroup;
                items: FormArray;
                data: any = ;
                constructor(private formBuilder: FormBuilder) {
                this.data.push({ Type: 'SUV', Model: '500', Year: '2009', Make:'Mahinda', Color:'Red', PlateNumber:'RJ-15', StateRegistered:'Rajsthan' });
                }

                ngOnInit() {
                this.orderForm = this.formBuilder.group({
                items: this.formBuilder.array([this.createItem()])
                });
                }

                createItem(): FormGroup {
                return this.formBuilder.group({
                Type:'', Model:'', Year:'', Make:'', Color:'', PlateNumber:'', StateRegistered:''
                });
                }

                addItem(): void {
                this.items = this.orderForm.get('items') as FormArray;
                this.items.push(this.createItem());
                }

                onSubmit() {
                this.items = this.orderForm.get('items') as FormArray;
                console.log(this.items.value);
                }
                }






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 28 '18 at 7:26









                Sanjay KatiyarSanjay Katiyar

                46018




                46018






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53513269%2fadd-a-row-to-table-on-click-of-add-button-the-new-row-is-form-fields-in-each-cel%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Contact image not getting when fetch all contact list from iPhone by CNContact

                    count number of partitions of a set with n elements into k subsets

                    A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks