vv_wow
vv_wow

Reputation: 23

In a table where rows are added dinamically, how to make different on-click function for every row in Angular?

I have a table and it's rows are added dinamically.

I have an array in the component.ts file and I use *ngFor on that array, so I get the same number of rows, as elements of array.

Thoses dynamically added rows are clickable. Clicking on the row will act like a dropdown, so another row will appear under it, and clicking on the same row as before, will close that dropdown, it will collapse.

Before the table-rows were added dinamically, I made 10 different row and I had 10 different "collapsed(1-2-3....)" variables, for every click event. Then, dropdowns on every row worked. After adding rows dinamically, the previous solution obviously doesn't work. After clicking on one row, all row do the same way(because I am using one variable).

Can you help me to fix it or tell me how to fix it? :)

<table class="table col-12">
    <tr class="d-flex col-12 clickable" (click)="collapsed=!collapsed" *ngFor="let log of logList">
        <td class="col-1">
            <i class="material-icons" *ngIf="!collapsed" aria-hidden="true">
                keyboard_arrow_down
            </i>
            <i class="material-icons" *ngIf="collapsed" aria-hidden="true">
                keyboard_arrow_right
            </i>
        <td>
        <td class="col-1">
            {{log.time}}
        </td>
        <td class="col-3">
            {{log.text}}
        </td>
        <td class="col-7">
            <!-- JSON file displayed-->
        </td>
    </tr>
    <tr class="d-flex col-12" *ngIf="!collapsed">
        <!-- dropdown row-->
        <td></td>
        <td></td>
        <td></td>
        <td></td>
    </tr>
</table>

Clicking on the rows, will change the icon in the first column as well.

Upvotes: 0

Views: 368

Answers (1)

Ben
Ben

Reputation: 4110

You can add a property on your component to store the selected item. And in you HTML, you can expend/collapse row depending on the selected item. And then fill the dropdown row with the selected item's data.

In your component ts:

public selectedLog: any;

In your html:

<table class="table col-12">
    <tr class="d-flex col-12 clickable" *ngFor="let log of logList" (click)="log == selectedLog ? selectedLog = null : selectedLog = log">
        <td class="col-1">
            <i class="material-icons" *ngIf="!selectedLog == log" aria-hidden="true">
                keyboard_arrow_down
            </i>
            <i class="material-icons" *ngIf="selectedLog != log" aria-hidden="true">
                keyboard_arrow_right
            </i>
        <td>
        <td class="col-1">
            {{log.time}}
        </td>
        <td class="col-3">
            {{log.text}}
        </td>
        <td class="col-7">
            <!-- JSON file displayed-->
        </td>
    </tr>
    <tr class="d-flex col-12" *ngIf="selectedLog != null">
        {{selectedLog.whateverProperty}}
        <!-- dropdown row-->
        <td></td>
        <td></td>
        <td></td>
        <td></td>
    </tr>
</table>

EDIT:

Here is the version with dropdown row under the selected row:

<table class="table col-12">
    <ng-container *ngFor="let log of logList">
        <tr class="d-flex col-12 clickable" (click)="log == selectedLog ? selectedLog = null : selectedLog = log">
            <td class="col-1">
                <i class="material-icons" *ngIf="!selectedLog == log" aria-hidden="true">
                    keyboard_arrow_down
                </i>
                <i class="material-icons" *ngIf="selectedLog != log" aria-hidden="true">
                    keyboard_arrow_right
                </i>
            <td>
            <td class="col-1">
                {{log.time}}
            </td>
            <td class="col-3">
                {{log.text}}
            </td>
            <td class="col-7">
                <!-- JSON file displayed-->
            </td>
        </tr>
        <tr class="d-flex col-12" *ngIf="selectedLog == log" colspan="4">
            {{selectedLog.whateverProperty}}
            <!-- dropdown row-->
            <td></td>
            <td></td>
            <td></td>
            <td></td>
        </tr>
    </ng-container>
</table>

Upvotes: 1

Related Questions