I have a table where data is populating. Each row has an edit link. I want to edit only a particular row on click of edit link. Right now its’ showing edit option for all the rows.
Also I want to show the text in a input box on click of edit.
Here is my code.
JavaScript
x
23
23
1
<tr *ngFor="let row of backendData.report" class="hover-highlight">
2
3
<td class="benchmark_name">
4
{{row.name}}
5
</td>
6
<td>
7
{{row.value}}
8
</td>
9
<td>
10
{{row.description}}
11
</td>
12
<td>
13
<button *ngIf="enableEdit" (click)="enableEdit=false" class="btn page-secondary-action-btn" ng-click="cancel()">Cancel</button>
14
<button *ngIf="enableEdit" id="saveBtn" class="btn page-primary-action-btn" (click)="saveSegment()" type="submit">Save</button>
15
<a class="table-row-action edit-action" *ngIf="!enableEdit" (click)="enableEdit = true">
16
<i class="fa fa-pencil" uib-tooltip="Edit" tooltip-trigger="mouseenter" tooltip-append-to-body="true" tooltip-placement="left"></i>
17
</a>
18
</td>
19
<td>
20
21
</td>
22
</tr>
23
My current output looks like this
Advertisement
Answer
Here is the solution
html
JavaScript
1
23
23
1
<tr *ngFor="let row of backendData; index as i;" class="hover-highlight">
2
3
<td class="benchmark_name">
4
{{row.name}}
5
</td>
6
<td>
7
{{row.value}}
8
</td>
9
<td>
10
{{row.description}}
11
</td>
12
<td>
13
<button *ngIf="enableEdit && enableEditIndex == i" (click)="enableEdit=false" class="btn page-secondary-action-btn" ng-click="cancel()">Cancel</button>
14
<button *ngIf="enableEdit && enableEditIndex == i" id="saveBtn" class="btn page-primary-action-btn" (click)="saveSegment()" type="submit">Save</button>
15
<a href="#" class="table-row-action edit-action" *ngIf="!enableEdit" (click)="enableEditMethod($event, i)">
16
edit
17
</a>
18
</td>
19
<td>
20
21
</td>
22
</tr>
23
ts file
JavaScript
1
34
34
1
import { Component } from '@angular/core';
2
3
@Component({
4
selector: 'my-app',
5
templateUrl: './app.component.html',
6
styleUrls: [ './app.component.css' ]
7
})
8
export class AppComponent {
9
name = 'Angular';
10
enableEdit = false;
11
enableEditIndex = null;
12
backendData = [{
13
"name": 'Target',
14
"value": '100',
15
"description": 'abc'
16
},
17
{
18
"name": 'Size',
19
"value": '20',
20
"description": 'def'
21
},
22
{
23
"name": 'Industry',
24
"value": '40',
25
"description": 'ghi'
26
}]
27
28
enableEditMethod(e, i) {
29
this.enableEdit = true;
30
this.enableEditIndex = i;
31
console.log(i, e);
32
}
33
}
34
Let me know if you have any doubt.