How to add a new row on click of a button in angular 4









up vote
1
down vote

favorite












enter image description hereI have the below code to create a row and a button Add Person. On click of Add person , i want to create one more row with Person 2, Person 3 and so on.
enter image description here . How can I achieve it.



<div class="splitTheBill-row">
<div class="splitTheBill-left">
<div class="userIcon">
<div class="splitTheBill-user">
<img src="assets/images/person.png" alt="My Profile">
</div>
<div class="input-field national-id pull-left">
<input id="form3" class="form-control" type="text">
<label for="form3" class="">Person 1</label>
</div>
</div>
</div>
<div class="splitTheBill-right">
<div class="input-field national-id pull-left">
<input id="form4" class="form-control" type="text">
</div>
</div>
</div>
<div class="addperson-btncont">
<div class="reg-submitCont">
<button url="#" class="btn-secondary waves-effect waves-light btn">Add Person</button>
</div>
</div>









share|improve this question























  • Can you post the form creation in the component, please ?
    – RoadEx
    18 hours ago










  • I am not using any forms to create this. This is a part of my HTML code
    – Nancy
    18 hours ago














up vote
1
down vote

favorite












enter image description hereI have the below code to create a row and a button Add Person. On click of Add person , i want to create one more row with Person 2, Person 3 and so on.
enter image description here . How can I achieve it.



<div class="splitTheBill-row">
<div class="splitTheBill-left">
<div class="userIcon">
<div class="splitTheBill-user">
<img src="assets/images/person.png" alt="My Profile">
</div>
<div class="input-field national-id pull-left">
<input id="form3" class="form-control" type="text">
<label for="form3" class="">Person 1</label>
</div>
</div>
</div>
<div class="splitTheBill-right">
<div class="input-field national-id pull-left">
<input id="form4" class="form-control" type="text">
</div>
</div>
</div>
<div class="addperson-btncont">
<div class="reg-submitCont">
<button url="#" class="btn-secondary waves-effect waves-light btn">Add Person</button>
</div>
</div>









share|improve this question























  • Can you post the form creation in the component, please ?
    – RoadEx
    18 hours ago










  • I am not using any forms to create this. This is a part of my HTML code
    – Nancy
    18 hours ago












up vote
1
down vote

favorite









up vote
1
down vote

favorite











enter image description hereI have the below code to create a row and a button Add Person. On click of Add person , i want to create one more row with Person 2, Person 3 and so on.
enter image description here . How can I achieve it.



<div class="splitTheBill-row">
<div class="splitTheBill-left">
<div class="userIcon">
<div class="splitTheBill-user">
<img src="assets/images/person.png" alt="My Profile">
</div>
<div class="input-field national-id pull-left">
<input id="form3" class="form-control" type="text">
<label for="form3" class="">Person 1</label>
</div>
</div>
</div>
<div class="splitTheBill-right">
<div class="input-field national-id pull-left">
<input id="form4" class="form-control" type="text">
</div>
</div>
</div>
<div class="addperson-btncont">
<div class="reg-submitCont">
<button url="#" class="btn-secondary waves-effect waves-light btn">Add Person</button>
</div>
</div>









share|improve this question















enter image description hereI have the below code to create a row and a button Add Person. On click of Add person , i want to create one more row with Person 2, Person 3 and so on.
enter image description here . How can I achieve it.



<div class="splitTheBill-row">
<div class="splitTheBill-left">
<div class="userIcon">
<div class="splitTheBill-user">
<img src="assets/images/person.png" alt="My Profile">
</div>
<div class="input-field national-id pull-left">
<input id="form3" class="form-control" type="text">
<label for="form3" class="">Person 1</label>
</div>
</div>
</div>
<div class="splitTheBill-right">
<div class="input-field national-id pull-left">
<input id="form4" class="form-control" type="text">
</div>
</div>
</div>
<div class="addperson-btncont">
<div class="reg-submitCont">
<button url="#" class="btn-secondary waves-effect waves-light btn">Add Person</button>
</div>
</div>






angular button click row add






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 18 hours ago

























asked 19 hours ago









Nancy

65214




65214











  • Can you post the form creation in the component, please ?
    – RoadEx
    18 hours ago










  • I am not using any forms to create this. This is a part of my HTML code
    – Nancy
    18 hours ago
















  • Can you post the form creation in the component, please ?
    – RoadEx
    18 hours ago










  • I am not using any forms to create this. This is a part of my HTML code
    – Nancy
    18 hours ago















Can you post the form creation in the component, please ?
– RoadEx
18 hours ago




Can you post the form creation in the component, please ?
– RoadEx
18 hours ago












I am not using any forms to create this. This is a part of my HTML code
– Nancy
18 hours ago




I am not using any forms to create this. This is a part of my HTML code
– Nancy
18 hours ago












2 Answers
2






active

oldest

votes

















up vote
4
down vote



accepted










You need to use *ngFor to loop through your persons, like so:



 <div class="splitTheBill-left">
<div *ngFor="let person of persons">
<div class="userIcon">
<div class="splitTheBill-user">
<img src="assets/images/person.png" alt="My Profile">
</div>
<div class="input-field national-id pull-left">
<input id="form3" class="form-control" type="text">
<label for="form3" class="">person.name</label>
</div>
</div>
</div>
</div>


Then, you can create an array in the TS that represents the people:



 persons = [
"name": "Person 1"
]


And to add a new row, all you need to do is push a new person into that array



 this.persons.push("name": "Person " + (this.persons.length + 1))


Take a look at the official tutorial for more info






share|improve this answer






















  • It adds Person 2 , Person 2 everytime on click of a button. Ideally i want Person 2, Person 3, Person 4, Person 5 ....
    – Nancy
    18 hours ago










  • @Nancy, using ReactiveForms will be a better approach for your case.
    – SiddAjmera
    18 hours ago











  • @Nancy That;'s because the name was hardcoded. I've just updated the answer
    – user184994
    18 hours ago










  • It creates Person 1 twice and then Person 2 , Person 3, Person 4... I have added a image as well.
    – Nancy
    18 hours ago










  • There should be a +1 in that code as well (I updated it), take a look at the example, it should be "Person " + (this.persons.length + 1)})
    – user184994
    18 hours ago


















up vote
1
down vote













I think what you're looking for here is a Reactive Form that could add person(s) on the fly to the form:



Here's a minimal Template:



<form [formGroup]="personsForm" (submit)="onFormSubmit()">
<div formArrayName="persons">
<div *ngFor="let person of persons; let i = index;" [formGroupName]="i">
Name: <input type="text" formControlName="name"><br>
Amount: <input type="text" formControlName="amount">
</div>
</div>
<button type="button" (click)="addPerson()">
Add Person
</button>
<button>
Submit
</button>
</form>


Here's the Component Class:



import Component from '@angular/core';
import FormGroup, FormControl, FormBuilder, FormArray from '@angular/forms';

@Component(...)
export class AppComponent
personsForm: FormGroup;

constructor(private fb: FormBuilder)

ngOnInit()
this.personsForm = this.fb.group(
persons: this.fb.array()
);


onFormSubmit()
alert(JSON.stringify(this.personsForm.value));


addPerson()
(<FormArray>this.personsForm.get('persons')).push(this.fb.group(
name: ,
amount:
));


get persons()
return (<FormArray>this.personsForm.get('persons')).controls;




Here's a Sample StackBlitz for your ref.






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',
    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%2f53222132%2fhow-to-add-a-new-row-on-click-of-a-button-in-angular-4%23new-answer', 'question_page');

    );

    Post as a guest






























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    4
    down vote



    accepted










    You need to use *ngFor to loop through your persons, like so:



     <div class="splitTheBill-left">
    <div *ngFor="let person of persons">
    <div class="userIcon">
    <div class="splitTheBill-user">
    <img src="assets/images/person.png" alt="My Profile">
    </div>
    <div class="input-field national-id pull-left">
    <input id="form3" class="form-control" type="text">
    <label for="form3" class="">person.name</label>
    </div>
    </div>
    </div>
    </div>


    Then, you can create an array in the TS that represents the people:



     persons = [
    "name": "Person 1"
    ]


    And to add a new row, all you need to do is push a new person into that array



     this.persons.push("name": "Person " + (this.persons.length + 1))


    Take a look at the official tutorial for more info






    share|improve this answer






















    • It adds Person 2 , Person 2 everytime on click of a button. Ideally i want Person 2, Person 3, Person 4, Person 5 ....
      – Nancy
      18 hours ago










    • @Nancy, using ReactiveForms will be a better approach for your case.
      – SiddAjmera
      18 hours ago











    • @Nancy That;'s because the name was hardcoded. I've just updated the answer
      – user184994
      18 hours ago










    • It creates Person 1 twice and then Person 2 , Person 3, Person 4... I have added a image as well.
      – Nancy
      18 hours ago










    • There should be a +1 in that code as well (I updated it), take a look at the example, it should be "Person " + (this.persons.length + 1)})
      – user184994
      18 hours ago















    up vote
    4
    down vote



    accepted










    You need to use *ngFor to loop through your persons, like so:



     <div class="splitTheBill-left">
    <div *ngFor="let person of persons">
    <div class="userIcon">
    <div class="splitTheBill-user">
    <img src="assets/images/person.png" alt="My Profile">
    </div>
    <div class="input-field national-id pull-left">
    <input id="form3" class="form-control" type="text">
    <label for="form3" class="">person.name</label>
    </div>
    </div>
    </div>
    </div>


    Then, you can create an array in the TS that represents the people:



     persons = [
    "name": "Person 1"
    ]


    And to add a new row, all you need to do is push a new person into that array



     this.persons.push("name": "Person " + (this.persons.length + 1))


    Take a look at the official tutorial for more info






    share|improve this answer






















    • It adds Person 2 , Person 2 everytime on click of a button. Ideally i want Person 2, Person 3, Person 4, Person 5 ....
      – Nancy
      18 hours ago










    • @Nancy, using ReactiveForms will be a better approach for your case.
      – SiddAjmera
      18 hours ago











    • @Nancy That;'s because the name was hardcoded. I've just updated the answer
      – user184994
      18 hours ago










    • It creates Person 1 twice and then Person 2 , Person 3, Person 4... I have added a image as well.
      – Nancy
      18 hours ago










    • There should be a +1 in that code as well (I updated it), take a look at the example, it should be "Person " + (this.persons.length + 1)})
      – user184994
      18 hours ago













    up vote
    4
    down vote



    accepted







    up vote
    4
    down vote



    accepted






    You need to use *ngFor to loop through your persons, like so:



     <div class="splitTheBill-left">
    <div *ngFor="let person of persons">
    <div class="userIcon">
    <div class="splitTheBill-user">
    <img src="assets/images/person.png" alt="My Profile">
    </div>
    <div class="input-field national-id pull-left">
    <input id="form3" class="form-control" type="text">
    <label for="form3" class="">person.name</label>
    </div>
    </div>
    </div>
    </div>


    Then, you can create an array in the TS that represents the people:



     persons = [
    "name": "Person 1"
    ]


    And to add a new row, all you need to do is push a new person into that array



     this.persons.push("name": "Person " + (this.persons.length + 1))


    Take a look at the official tutorial for more info






    share|improve this answer














    You need to use *ngFor to loop through your persons, like so:



     <div class="splitTheBill-left">
    <div *ngFor="let person of persons">
    <div class="userIcon">
    <div class="splitTheBill-user">
    <img src="assets/images/person.png" alt="My Profile">
    </div>
    <div class="input-field national-id pull-left">
    <input id="form3" class="form-control" type="text">
    <label for="form3" class="">person.name</label>
    </div>
    </div>
    </div>
    </div>


    Then, you can create an array in the TS that represents the people:



     persons = [
    "name": "Person 1"
    ]


    And to add a new row, all you need to do is push a new person into that array



     this.persons.push("name": "Person " + (this.persons.length + 1))


    Take a look at the official tutorial for more info







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited 18 hours ago

























    answered 18 hours ago









    user184994

    10.2k11426




    10.2k11426











    • It adds Person 2 , Person 2 everytime on click of a button. Ideally i want Person 2, Person 3, Person 4, Person 5 ....
      – Nancy
      18 hours ago










    • @Nancy, using ReactiveForms will be a better approach for your case.
      – SiddAjmera
      18 hours ago











    • @Nancy That;'s because the name was hardcoded. I've just updated the answer
      – user184994
      18 hours ago










    • It creates Person 1 twice and then Person 2 , Person 3, Person 4... I have added a image as well.
      – Nancy
      18 hours ago










    • There should be a +1 in that code as well (I updated it), take a look at the example, it should be "Person " + (this.persons.length + 1)})
      – user184994
      18 hours ago

















    • It adds Person 2 , Person 2 everytime on click of a button. Ideally i want Person 2, Person 3, Person 4, Person 5 ....
      – Nancy
      18 hours ago










    • @Nancy, using ReactiveForms will be a better approach for your case.
      – SiddAjmera
      18 hours ago











    • @Nancy That;'s because the name was hardcoded. I've just updated the answer
      – user184994
      18 hours ago










    • It creates Person 1 twice and then Person 2 , Person 3, Person 4... I have added a image as well.
      – Nancy
      18 hours ago










    • There should be a +1 in that code as well (I updated it), take a look at the example, it should be "Person " + (this.persons.length + 1)})
      – user184994
      18 hours ago
















    It adds Person 2 , Person 2 everytime on click of a button. Ideally i want Person 2, Person 3, Person 4, Person 5 ....
    – Nancy
    18 hours ago




    It adds Person 2 , Person 2 everytime on click of a button. Ideally i want Person 2, Person 3, Person 4, Person 5 ....
    – Nancy
    18 hours ago












    @Nancy, using ReactiveForms will be a better approach for your case.
    – SiddAjmera
    18 hours ago





    @Nancy, using ReactiveForms will be a better approach for your case.
    – SiddAjmera
    18 hours ago













    @Nancy That;'s because the name was hardcoded. I've just updated the answer
    – user184994
    18 hours ago




    @Nancy That;'s because the name was hardcoded. I've just updated the answer
    – user184994
    18 hours ago












    It creates Person 1 twice and then Person 2 , Person 3, Person 4... I have added a image as well.
    – Nancy
    18 hours ago




    It creates Person 1 twice and then Person 2 , Person 3, Person 4... I have added a image as well.
    – Nancy
    18 hours ago












    There should be a +1 in that code as well (I updated it), take a look at the example, it should be "Person " + (this.persons.length + 1)})
    – user184994
    18 hours ago





    There should be a +1 in that code as well (I updated it), take a look at the example, it should be "Person " + (this.persons.length + 1)})
    – user184994
    18 hours ago













    up vote
    1
    down vote













    I think what you're looking for here is a Reactive Form that could add person(s) on the fly to the form:



    Here's a minimal Template:



    <form [formGroup]="personsForm" (submit)="onFormSubmit()">
    <div formArrayName="persons">
    <div *ngFor="let person of persons; let i = index;" [formGroupName]="i">
    Name: <input type="text" formControlName="name"><br>
    Amount: <input type="text" formControlName="amount">
    </div>
    </div>
    <button type="button" (click)="addPerson()">
    Add Person
    </button>
    <button>
    Submit
    </button>
    </form>


    Here's the Component Class:



    import Component from '@angular/core';
    import FormGroup, FormControl, FormBuilder, FormArray from '@angular/forms';

    @Component(...)
    export class AppComponent
    personsForm: FormGroup;

    constructor(private fb: FormBuilder)

    ngOnInit()
    this.personsForm = this.fb.group(
    persons: this.fb.array()
    );


    onFormSubmit()
    alert(JSON.stringify(this.personsForm.value));


    addPerson()
    (<FormArray>this.personsForm.get('persons')).push(this.fb.group(
    name: ,
    amount:
    ));


    get persons()
    return (<FormArray>this.personsForm.get('persons')).controls;




    Here's a Sample StackBlitz for your ref.






    share|improve this answer
























      up vote
      1
      down vote













      I think what you're looking for here is a Reactive Form that could add person(s) on the fly to the form:



      Here's a minimal Template:



      <form [formGroup]="personsForm" (submit)="onFormSubmit()">
      <div formArrayName="persons">
      <div *ngFor="let person of persons; let i = index;" [formGroupName]="i">
      Name: <input type="text" formControlName="name"><br>
      Amount: <input type="text" formControlName="amount">
      </div>
      </div>
      <button type="button" (click)="addPerson()">
      Add Person
      </button>
      <button>
      Submit
      </button>
      </form>


      Here's the Component Class:



      import Component from '@angular/core';
      import FormGroup, FormControl, FormBuilder, FormArray from '@angular/forms';

      @Component(...)
      export class AppComponent
      personsForm: FormGroup;

      constructor(private fb: FormBuilder)

      ngOnInit()
      this.personsForm = this.fb.group(
      persons: this.fb.array()
      );


      onFormSubmit()
      alert(JSON.stringify(this.personsForm.value));


      addPerson()
      (<FormArray>this.personsForm.get('persons')).push(this.fb.group(
      name: ,
      amount:
      ));


      get persons()
      return (<FormArray>this.personsForm.get('persons')).controls;




      Here's a Sample StackBlitz for your ref.






      share|improve this answer






















        up vote
        1
        down vote










        up vote
        1
        down vote









        I think what you're looking for here is a Reactive Form that could add person(s) on the fly to the form:



        Here's a minimal Template:



        <form [formGroup]="personsForm" (submit)="onFormSubmit()">
        <div formArrayName="persons">
        <div *ngFor="let person of persons; let i = index;" [formGroupName]="i">
        Name: <input type="text" formControlName="name"><br>
        Amount: <input type="text" formControlName="amount">
        </div>
        </div>
        <button type="button" (click)="addPerson()">
        Add Person
        </button>
        <button>
        Submit
        </button>
        </form>


        Here's the Component Class:



        import Component from '@angular/core';
        import FormGroup, FormControl, FormBuilder, FormArray from '@angular/forms';

        @Component(...)
        export class AppComponent
        personsForm: FormGroup;

        constructor(private fb: FormBuilder)

        ngOnInit()
        this.personsForm = this.fb.group(
        persons: this.fb.array()
        );


        onFormSubmit()
        alert(JSON.stringify(this.personsForm.value));


        addPerson()
        (<FormArray>this.personsForm.get('persons')).push(this.fb.group(
        name: ,
        amount:
        ));


        get persons()
        return (<FormArray>this.personsForm.get('persons')).controls;




        Here's a Sample StackBlitz for your ref.






        share|improve this answer












        I think what you're looking for here is a Reactive Form that could add person(s) on the fly to the form:



        Here's a minimal Template:



        <form [formGroup]="personsForm" (submit)="onFormSubmit()">
        <div formArrayName="persons">
        <div *ngFor="let person of persons; let i = index;" [formGroupName]="i">
        Name: <input type="text" formControlName="name"><br>
        Amount: <input type="text" formControlName="amount">
        </div>
        </div>
        <button type="button" (click)="addPerson()">
        Add Person
        </button>
        <button>
        Submit
        </button>
        </form>


        Here's the Component Class:



        import Component from '@angular/core';
        import FormGroup, FormControl, FormBuilder, FormArray from '@angular/forms';

        @Component(...)
        export class AppComponent
        personsForm: FormGroup;

        constructor(private fb: FormBuilder)

        ngOnInit()
        this.personsForm = this.fb.group(
        persons: this.fb.array()
        );


        onFormSubmit()
        alert(JSON.stringify(this.personsForm.value));


        addPerson()
        (<FormArray>this.personsForm.get('persons')).push(this.fb.group(
        name: ,
        amount:
        ));


        get persons()
        return (<FormArray>this.personsForm.get('persons')).controls;




        Here's a Sample StackBlitz for your ref.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 18 hours ago









        SiddAjmera

        8,72621033




        8,72621033



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53222132%2fhow-to-add-a-new-row-on-click-of-a-button-in-angular-4%23new-answer', 'question_page');

            );

            Post as a guest














































































            這個網誌中的熱門文章

            How to combine Bézier curves to a surface?

            Mutual Information Always Non-negative

            Why am i infinitely getting the same tweet with the Twitter Search API?