1

I want to add the row dynamically which will contain a total of two or more rows in the HTML table

Html table

Whenever there are two rows or more rows in the form array another row should be added with the total.

This HTML table is dynamically generating the rows using ngFor over the array of objects.

Any suggestions.

1 Answer 1

1

You can add a table row after the ngFor and add ngIf to check if the number of objects in the array is greater than or equal to 2 (To show the total)

Your html code will look like this:

<tr *ngFor="let object of objects">
   <td> {{object.field1}} </td>
   <td> {{object.field2}} </td>
   <td> {{object.field3}} </td>
   <td> {{object.field4}} </td>
</tr>
<tr *ngIf="showTotal()">
   <td>Total</td>
   <td>...</td>
   <td>...</td>
   <td>...</td>
   <td>...</td>
</tr>

And in your .ts file:

showTotal(): boolean {
    return this.objects.length >= 2;
}

The row containing the total will be created only if the number of rows in the table (which is the number of objects) is greater than or equal to 2)

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

1 Comment

and when to add those totals for each td.

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.