I want to display items names in a table but limit to number of rows is three. If there are more than 3 items then 4th item should be in 2nd column, if there are more than 6 items then 7th item should be in 3rd column.
I am using normal table.
<table>
<tr *ngFor="let item of group">
<td>{{item}}</td>
</tr>
</table>
Please let me know what condition I have to give, to limit number of rows and get them in columns based on number of items.
TIA!!
4
Answers
You can use CSS Grid to arrange this sort of layout using
grid-auto-flow
. To get you started here’s a few resources:Your layout using grid can be seen below. I’ve annotated the pertinent bits:
In this example, we wrap the table inside a div with the class table-container. The max-height property limits the height of the container, and if the content exceeds this height, it will enable vertical scrolling due to the overflow-y: auto property.
Adjust the max-height value according to your requirements. If the table exceeds the specified height, the container will display a scrollbar to allow scrolling through the rows.
Consider use Angular-Material…
https://material.angular.io/components/table/api
These and othe more features very helpful you can find
@Adam has posted the correct answer for this using CSS methods.
If you are looking to manually solve this through data manipulation, then this is a solution for you.
Now that we have data in 2d format (with correct number of rows and columns), you can display it in your HTML like:
It is possible to change ordering of elements appearing in a row (populate row first or populate columns first) just by tweaking outer and inner loops in code. Also you can change number of rows and size of your data to test it for various cases.
Demo here https://stackblitz.com/edit/angular-mgte7k?file=src%2Fusers%2Fusers.component.ts