0

I'm trying to build a 'edit in place' input form , to change the name of a product when the user clicks on the field. My problem is the name field in question is printed out using a ngFor loop and I don't know how many there will be, it will be different for each user. Is there a way to dynamically change the nameField and then refer to the dynamic name in the form? I guess something like: nameField1, nameField2 etc etc, but made using loop from the ngFor="let item of crudService.subscriptions.subscriptions

html:

<tr *ngFor="let item of crudService.subscriptions.subscriptions; index as i">
    <td>
      <div *ngIf="!showEditName">
        <div>
          {{ item.productName }}
        </div>
        <div>
          <button class="btn-sc btn-blac" (click)="editName(item.productName, i)">Edit</button>
        </div>
      </div>

      <div class="col" *ngIf="showEditName">
        <div class="md-form">
          <input mdbInput mdbValidate type="text" class="form-control" [formControl]="nameField" />
          <label for="editName">EditName</label>
        </div>
        <div>
          <button class="btn-sc btn-blac" (click)="editName()">save</button>
          <button class="btn-sc btn-blac" (click)="editName()">cancel</button>
        </div>
      </div>
    </td>
</tr>

component.ts:

import { FormControl } from "@angular/forms";

export class SubscriptionsComponent implements OnInit {

nameField = new FormControl("");
showEditName: boolean = false;

  editName(itemName: string, subIndex: number) {
    console.log(subIndex);
    this.nameField.setValue(itemName);
    this.showEditName = !this.showEditName;
  }
}

I've seen formArray with FormGroup builder in Angular but I'm finding it hard to understand and I'm not sure if its what I need.

1 Answer 1

1

You can edit the name with index value so that you track which index name is getting edited. Because only way to track the edit input by index value.

If you want FormArray example i can provide you if below solution won't work for you

<tr *ngFor="let item of crudService.subscriptions.subscriptions; index as i">
<td>
  <div *ngIf="indexToEdit != i"> // make sure to track by index
    <div>
      {{ item.productName }}
    </div>
    <div>
      <button class="btn-sc btn-blac" (click)="editName(item.productName, i)">Edit</button>
    </div>
  </div>

  <div class="col" *ngIf="indexToEdit == i">
    <div class="md-form">
      <input mdbInput mdbValidate type="text" class="form-control" [formControl]="nameField" />
      <label for="editName">EditName</label>
    </div>
    <div>
      <button class="btn-sc btn-blac" (click)="editName()">save</button>
      <button class="btn-sc btn-blac" (click)="editName()">cancel</button>
    </div>
  </div>
</td>

Then make changes in ts file

import { FormControl } from "@angular/forms";

export class SubscriptionsComponent implements OnInit {

 nameField = new FormControl("");
 indexToEdit: number = null;

 editName(itemName: string, subIndex: number) {
   console.log(subIndex);
   this.nameField.setValue(itemName);
   this.indexToEdit = subIndex; // set index value and reset it after name edited
 }
}
Sign up to request clarification or add additional context in comments.

1 Comment

It would be nice to see a FormArray version, although this works, I have another field doing the same to change the price, the code might be neater using FormArray

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.