0

I have requirement where i can add dynamic row or remove row, here is my code

ngOnInit{

this.rows.push({
   id:'',
   name:'',
  age:''
})

addRow(){
let a= {
   id:'',
   name:'',
  age:''
}
this.rows.push(a)
}

deleteRow(index){
this.rows.splice(index,1)
}

}

the issue is suppose i have three rows i have entred value in all three rows ,if i delete second rows and added third row, second and third row fields become blank

<div *ngFor="item in rows;let i =index;">
     <div>
<input type="text" name="name{i}" [(ngModel)]="item.name"> delete/add button here
    </div>
</div>.
3
  • Why did write your methods in ngOnInit? I hope it's a misprint Commented Nov 19, 2017 at 13:51
  • @DmitryGrinko, yes you are right its typo issue Commented Nov 19, 2017 at 13:52
  • where is your add/ delete html code ? Put so we can see what's wrong. Commented Nov 19, 2017 at 15:46

2 Answers 2

2
// TS File Code

import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
  // Initializtion
  public row: any = [{}];


  // Load on Component Initialization
  public ngOnInit() {

  }


  // Add New Row
  addRow() {
    this.row.push({});
  }

  // Delete Rows
  deleteRow(index: number) {
    this.row.splice(index, 1);
  }

  // Get All Row Values
  getRowValue() {
    console.log(this.row);
  }
}


// HTML File 

<h1>
  Creating Dynamic Textboxes (rows)
</h1>
<input type="button" value="Add Rows" (click)="addRow()"/>
<div *ngFor="let item of row; let i=index">
  <input type="text" name="name{{i}}" [(ngModel)]="item.name"> 
  <input type="button" value="Delete Rows" (click)="deleteRow(index)"/>
</div>

<input type="button" value="Get Rows Value" (click)="getRowValue()"/>

I hope these code would help you in solving your issue and hope these answer would be satisfactory

Thank You

Sign up to request clarification or add additional context in comments.

1 Comment

embed.plnkr.co/ybJBN6phN4fre9VUUi0J This is the plunkr link for the same. Hope it would help you
0

Your issue - first index of ngFor is zero. Anyway you shouldn't use index from ngFor in your case. Use id of your item.

delete(id){
   const index = this.rows.map(e => e.id).indexOf(id);
   if(index !== -1) {
       this.rows.splice(index, 1);
   }
}

your html:

<div *ngFor="let item of rows">
    <span>{{item.name}}</span>
    <span>{{item.age}}</span>
    <button (click)="delete(item.id)"></button>
<div>

Hope it helps

1 Comment

Hi Dmitry ,it didn't solve my problem deleteRow(index,id) { const index1 = this.rows.findIndex(x => x._id==id); if(index1 !== -1) { this.rows.splice(index1, 1); } };

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.