To indicate if a row is being edited, I recommend setting a property on each row as demonstrated below:
https://stackblitz.com/edit/angular-usbhmd
For those who prefer not to click the link, here's the code.
Html
<table class="table table-hover">
<thead>
<tr>
<th>Domain</th>
<th>Registered Date</th>
<th>Action</th>
</tr>
<tr *ngFor="let domain of domains; let i = index">
<td>
<span *ngIf="!domain.editable">{{domain.name}}</span>
<input type="text" class="form-control" [(ngModel)]="domain.name" *ngIf="domain.editable"/>
</td>
<td>
<span *ngIf="!domain.editable">{{domain.reg_date}}</span>
<input type="date" class="form-control" [(ngModel)]="domain.reg_date" *ngIf="domain.editable"/>
</td>
<td><button class="btn btn-primary" (click)="editDomain(domain)">Edit</button></td>
</tr>
</thead>
<tbody>
</tbody>
</table>
Component
import { Component } from '@angular/core';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
name = 'Angular';
domains = [];
isVisible : boolean = false;
constructor(){
this.domains = [
{
"_id" : "12323vdfvd234",
"name" : "sixlogs.com",
"reg_date" : "2018-04-04",
"editable": false
},
{
"_id" : "12323vdfvd234",
"name" : "avanza.com",
"reg_date" : "2019-04-04",
"editable": false
},
{
"_id" : "12323vdfvd234",
"name" : "tps.com",
"reg_date" : "2018-04-04",
"editable": false
},
{
"_id" : "12323vdfvd234",
"name" : "utf.com",
"reg_date" : "2019-04-04",
"editable": false
}
]
}
editDomain(domain: any){
domain.editable = !domain.editable;
}
}
Included in the domains
array is the editable
property that toggles when the editDomain
function is triggered by clicking the button. By utilizing the editable
property, you can update your view to show input fields for each row.