0

Hi I am using Angular8 and initially, it must be bound in a table all values and when click on edit, that particular row should be editable and one row at a time can be edited, I have attached the demo. And here I have added only one array data for demo purposes, but I need to bind multiple years so I tried with reactive forms to add input field and td based on click but couldn't bind values. So here in my case, I need to edit one row at a time.

HTML:

<ng-container *ngFor="let contact of yearManagement">
      <tr>
        <td></td>
        <td class="text-capitalize">{{contact.policyTypeName}}</td>
        <td>{{contact.quotes}}</td>
        <td>{{contact.policyCT}}</td>
        <td>{{contact.writtenPremium }}</td>
        <td class="width125">
          <button class="btn btn-outline-primary btn-table ml-1" title="Edit">Edit</button>
          <button class="btn btn-outline-primary btn-table ml-1" title="Delete"
                      (click)="deleteCommitment(contact)">Delete</button>
        </td>
      </tr>
    </ng-container>

Demo

5
  • stackblitz.com/edit/… I have updated the code. Does this solve your problem? Commented Jul 29, 2021 at 6:06
  • Thanks for response, ya to some extent this is solving my problem, i may need your help even more Commented Jul 29, 2021 at 6:21
  • Ok I am posting this as an answer please accept and upvote. Commented Jul 29, 2021 at 6:23
  • ya please post i will vote Commented Jul 29, 2021 at 6:25
  • @AmmarHussain if i click on close i have to revert values newly entered, but currently it is taking same updated value Commented Jul 29, 2021 at 13:03

1 Answer 1

1

Try with the following changes in html

<ng-container *ngFor="let contact of yearManagement">
      <tr>
        <td></td>
        <td class="text-capitalize">
          <ng-container *ngIf="editableRow !== contact.id; else newDeb">
            {{contact.policyTypeName}}
          </ng-container>
          <ng-template #newDeb>
            <input [(ngModel)]="contact.policyTypeName" />
          </ng-template>
        </td>
        <td>{{contact.quotes}}</td>
        <td>{{contact.policyCT}}</td>
        <td>{{contact.writtenPremium }}</td>
        <td class="width125">
          <button class="btn btn-outline-primary btn-table ml-1" title="Edit"
          (click)="editableRow = contact.id">Edit</button>
          <button class="btn btn-outline-primary btn-table ml-1" title="Delete"
                      (click)="deleteCommitment(contact)">Delete</button>
        </td>
      </tr>
    </ng-container>

And in typescript

export class AppComponent {
  editableRow = 0;
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the answer, i have added few buttons, when clicked on edit, edit should be hidden and show save and close button, when cliked on close, it must be back to binding from input box, stackblitz.com/edit/… via editrow can that be handled?
Have a look now.
how can we check, when clicking on close, does that row has got some edit or not?

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.