I have an array that I am looping over to create a table of records. Now, for each row in the table, I have provided an 'EDIT' button and a Cancel button. Clicking on the edit button will show textboxes populated with the actual values and will give the user an option to save. Clicking the cacncel button will collapse the editable row and will display the actual table row. Now, I am using the following arrangement to facilitate this:
<tr *ngFor="let item of mf.data; let i = index" [class.active]="i == selectedRow">
<td>
<span [hidden]="edit && i==selectedRow" [innerText]="item.firstName"></span>
<span [hidden]="i!==selectedRow"><input type="text" #fn id="fn" name="fn" [value]="item.firstName"/></span>
</td>
Now, the problem : 1. I click the EDIT button and make some modifications to the textbox value - 2. Then I click on the cancel button. 3. I again click on the EDIT button. Now, I was hoping to see the actual data in the textbox. Instead I see the modified value (per step 2).
I want that the value of textbox should always refresh to the value in the everytime I click on EDIT button.
EDIT: I've seen that if instead of using [hidden], I use
<span *ngIf="i==selectedRow"><input type="text" #fn id="fn" name="fn" [value]="item.firstName"/></span>
then I am able to get the textbox value reset, but then I am unable to access the textbox value in the Typescript file. I access the values file like this:
<img (click)="updateContact(fn.value) .../>
EDIT: Updated with the complete table:
<tbody>
<tr *ngFor="let item of mf.data; let i = index" [class.active]="i == selectedRow">
<tr><td>ass</td><tr>
<td>
<span *ngIf="i !== selectedRow" [innerText]="item.firstName"></span>
<span *ngIf="edit && i == selectedRow">
<input (blur)="updateEditableValues(fn.value, undefined)" type="text" #fn [value]="item.firstName"/>
</span>
<!--<input type="hidden" #firstName [(ngModel)]="firstNameVal" />-->
</td>
<td>
<span *ngIf="i !== selectedRow" [innerText]="item.lastName"></span>
<span *ngIf="edit && i == selectedRow" ><input (blur)="updateEditableValues(undefined, ln.value)" type="text" #ln value="{{item.lastName}}"/></span>
<!--<input type="hidden" #lastName [(ngModel)]="lastNameVal" />-->
</td>
<td>
{{item.email}}
<span [hidden]="true"><input #email type="text" value="{{item.email}}"/></span>
</td>
<td>{{item.contactStatus}}</td>
<td>
<!--<a [hidden]="edit && i==selectedRow" href="javascript:void(0);" (click)="setClickedRow(i)">EDIT</a>-->
<img height="20" width="20" *ngIf="i !== selectedRow" src="https://cdn2.iconfinder.com/data/icons/circle-icons-1/64/pencil-64.png"
(click)="setClickedRow(i)" />
<div *ngIf="edit && i == selectedRow" >
<img (click)="updateContact(i, fn.value, ln.value)" height="20" width="20" src="https://cdn4.iconfinder.com/data/icons/gnome-desktop-icons-png/PNG/48/Dialog-Apply-48.png"
/>
<img (click)="closeEdit()" height="20" width="20" src="https://cdn4.iconfinder.com/data/icons/common-toolbar/36/Cancel-48.png"
/>
</div>
</td>
</tr>
</tbody>
Update 2
updateContact(rowIndex, firstName, lastName, email){ alert(firstName) alert(lastName) this.data.forEach((item, index) => { if (item.email === email) { this.data[index].firstName = firstName; this.data[index].lastName = lastName; this.edit = false; this.selectedRow = -1; return false; } }); }