How to add a new row on click of a button in angular 4
up vote
1
down vote
favorite
I 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.
. 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
add a comment |
up vote
1
down vote
favorite
I 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.
. 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
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
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I 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.
. 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
I 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.
. 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
angular button click row add
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
add a comment |
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
add a comment |
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
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
|
show 1 more comment
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.
add a comment |
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
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
|
show 1 more comment
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
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
|
show 1 more comment
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
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
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
|
show 1 more comment
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
|
show 1 more comment
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.
add a comment |
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.
add a comment |
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.
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.
answered 18 hours ago
SiddAjmera
8,72621033
8,72621033
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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