0

I have a table in which I'm showing some data for each selected row like this

<table>
    <th>Col-1</th>
    <th>Col-2</th>
    <th></th>
    <tr *ngFor="let obj of data; let i=index">
    <td>{{obj.id}}</td>
    <td>{{obj.name}}</td>
    <button (click)="myFunction(i)">Click</button>
    <br>
    <div>Some dynamic data</div>
    </tr>
</table>

I want to display the div tag content for only clicked row and if I click other row button, that row with div should get displayed and previous div should be hidden. How do I achieve this? Please suggest

1
  • you can use a showRow boolean property to each row to toggle between them Commented Mar 1, 2018 at 7:13

2 Answers 2

1

One way to do it:

In your component, declare a variable

visibleRrowIndex: number = null;

In your html

<button (click)="visibleRowIndex=i">Click</button>
    <br>
    <div [hidden]="visibleRowIndex !==i">Some dynamic data</div>
Sign up to request clarification or add additional context in comments.

2 Comments

What do you call toggle effect? Like an animation? OP just wants to show/hide rows. Otherwise, he could use classes instead of hidden, and set CSS transitions on the classes
this is expected answer
1

If you want that a "click" on the same, toggle, change the (click).

<button (click)="visibleRowIndex!==i?visibleRowIndex=i:null">Click</button>

2 Comments

one small typo i reckon visibleRowIndex !==i ? visibleRowIndex = i : visibleRowIndex = null
::glups:: @thoniv, thanks, but should be visibleRowIndex=visibleRowIndex!==i?visibleRowIndex=i:-1, the ternay operator is: variable=(condition)?value if true:value if false -I know that work your code but we should use the ternary operator to equal a variable-. NOTE: I prefer use -1 when nothing is selected

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.