1

Created a table in angular containing car names. Each raw contains a reorder button, input field, and an edit button. Values in the input fields were populated using *ngFor. Initially all the input fields are disabled. When I click the edit button, then the corresponding input field needs to be enabled. But here all the input fields are enabled. I have gone through so many questions were asked. But could not find any solution. It would be helpful if someone can help. Thanks in advance. html

<table mat-table width="1500">
<tr>
 <th>Cars</th>
 <td>
  <ul *ngFor='let car of Car'>
   <li >
      <button mat-icon-button color="Green">
         <mat-icon>reorder</mat-icon>
      </button>
      <input mat-input #a id={{car.id}} value={{car.name}} class="text- width" 
        [disabled]=is_edit>
      <button mat-icon-button color="Green" (click)="isDisable()">
         <mat-icon>edit</mat-icon>
       </button>
   </li>
  </ul>
 </td>
</tr>
</table>

.ts file

export class CarComponent implements OnInit {
  is_edit: boolean;
  public Car: any[] = [{ id: '1', name: 'Maruti' }, { id: '2', name: 'Volkswagen' }, { id: '3', name: 'Ford' }]; 

  public ngOnInit(): void {
    this.is_edit = true;
  }

 public isDisable(): void {
    this.is_edit = false;
  }
}

1 Answer 1

2

Here, you need to have is_edit for every row. So, that you can update while click button for respective field. And you need pass index value of array in edit button click inside isDisable function, using that index you need to object car object.

Your, .ts should be

export class CarComponent implements OnInit {

  public Car: any[] = [{ id: '1', name: 'Maruti', is_edit: true }, { id: '2', name: 'Volkswagen', is_edit: true }, { id: '3', name: 'Ford', is_edit: true }]; 

  public ngOnInit(): void {
  }

 public isDisable(index: number): void {
    this.Car[index].is_edit = false;
  }
}

and your html should be

<table mat-table width="1500">
<tr>
 <th>Cars</th>
 <td>
  <ul *ngFor='let car of Car; let index as carIndex'>
   <li >
      <button mat-icon-button color="Green">
         <mat-icon>reorder</mat-icon>
      </button>
      <input mat-input #a id={{car.id}} value={{car.name}} class="text- width" 
        [disabled]=car.is_edit>
      <button mat-icon-button color="Green" (click)="isDisable(carIndex)">
         <mat-icon>edit</mat-icon>
       </button>
   </li>
  </ul>
 </td>
</tr>
</table>
Sign up to request clarification or add additional context in comments.

10 Comments

Hi, make sure you update the Car object public Car: any[] = [{ id: '1', name: 'Maruti', is_edit: false }, { id: '2', name: 'Volkswagen', is_edit: false }, { id: '3', name: 'Ford', is_edit: false }]; like this
I have tried this code. When loading the page a the input fields are enabled. I need them as disabled initially. Then the edit but needs to enable the corresponding input field.
make default value as true in is_edit for all car objects like this public Car: any[] = [{ id: '1', name: 'Maruti', is_edit: true }, { id: '2', name: 'Volkswagen', is_edit: true }, { id: '3', name: 'Ford', is_edit: true }];
It is not working. Shows an error like 'Cannot set property 'is_edit' of undefined at ..'
Thank you @madhan kumar. I got the result by replacing [disabled]=car.is_edit.
|

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.