0

I need to create a toggle button for my table (to show/hide) more details of selected row.

Given this is my table:

<table>
        <tr>
            <th>Company</th>
            <th>Contact</th>
            <th>Country</th>
        </tr>
        <tr *ngFor='let c of companies'>
            <td><button id="{{c.companyId}}" (click)="showDetail()">Details</button> </td>
            <td>{{ c.company}}</td>
            <td>{{ c.contact}}</td>
            <td>{{ c.country }}</td>
        </tr>
    </table>

When I click on "Details" button, need to show details inline in the table. This is kind of master detail grid approach in Kendo Grid. Any better approach using Angular2 & typescript to show details in easy way?

1
  • Can you create a working example in jsbin or jsfiddle ? Commented Apr 21, 2017 at 17:03

2 Answers 2

2

A small change from birwin's answer, as you cannot use template here without a directive, so use ng-container instead.

<ng-container *ngFor='let c of companies'>
    <tr>
        <td><button id="{{c.companyId}}" (click)="c.details = !c.details">Details</button> </td>
        <td>{{ c.company}}</td>
        <td>{{ c.contact}}</td>
        <td>{{ c.country }}</td>
    </tr>
    <tr *ngIf="c.details">
        <td>Details</td>
    </tr>
</ng-container>

Demo

Sign up to request clarification or add additional context in comments.

Comments

1

You could try something like this:

<table>
    <tr>
        <th>Company</th>
        <th>Contact</th>
        <th>Country</th>
    </tr>
    <ng-container *ngFor='let c of companies'>
        <tr>
            <td><button id="{{c.companyId}}" (click)="c.details = !c.details">Details</button> </td>
            <td>{{ c.company}}</td>
            <td>{{ c.contact}}</td>
            <td>{{ c.country }}</td>
        </tr>
        <tr *ngIf="c.details">
            <td>Details go here</td>
            <td>More details</td>
            <td>More details</td>
        </tr>
    </ng-container>
</table>

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.