1

i'm using dynamic table with each row having a button, on click of a button, i need data of the respective row button clicked.

I've tried (click) event binding on and i get output as undefined.

<table>
<thead> <tr> <th> Name </th> <th> Company </th> </tr> </thead>
<tr *ngFor = "let employee of employees" (click) = "removeEmployee(row)">
            <td> <input type="text" [(ngModel)]= "employee.name"></td>
            <td> <input type="text" [(ngModel)] = "employee.companyName"> </td>
</tr>
</table>

.ts file

removeEmployee(tr) {
      console.log(tr);
    }

expected: on click of button, table row data should output.

actual: undefined is displayed.

1
  • You should pass employee to function removeEmployee instead of row Commented Oct 24, 2019 at 11:24

2 Answers 2

5

change row in employee:

<tr *ngFor = "let employee of employees" (click) = "removeEmployee(employee)">
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, it's working. How to get rowIndex of respective row as well?
<tr *ngFor = "let employee of employees; let idx = index" (click) = "removeEmployee(employee)">
1

Here is your solution for it

<table>
<thead> <tr> <th> Name </th> <th> Company </th> </tr> </thead>
<tr *ngFor = "let employee of employees" (click) = "removeEmployee(employee)">
            <td> <input type="text" [(ngModel)]= "employee.name"></td>
            <td> <input type="text" [(ngModel)] = "employee.companyName"> </td>
</tr>
</table>

.ts file

removeEmployee(tr) {
      console.log(tr);
    }

Comments

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.