Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I split data which printed using ngfor to 3 columns in angular?

Data

const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30];

I want to split that data to 3 columns and want to appers as follow:

<ul>
    <li>1</li>
    <li>2</li>
    <li>3</li>
    ...
    <li>10</li>
</ul>
<ul>
    <li>11</li>
    <li>12</li>
    <li>13</li>
    ...
    <li>20</li>
</ul>
<ul>
    <li>21</li>
    <li>22</li>
    <li>23</li>
    ...
    <li>30</li>
</ul>

How can I split data when using ngfor?

<ul *ngFor="let item of arr">
    <li>{{item}}</li>
</ul>
like image 483
midstack Avatar asked Dec 02 '25 05:12

midstack


2 Answers

First of all, it should be like this :

<ul>
    <li *ngFor="let item of arr">{{item}}</li>
</ul>

And add this in your CSS :

ul{
    -webkit-columns: 3;
    -moz-columns: 3;
    columns: 3;
}
like image 81
Arcteezy Avatar answered Dec 03 '25 19:12

Arcteezy


You can use slice :

<ul>
  <li *ngFor="let item of arr.slice(0,10)">{{item}}</li>
</ul>

<ul>
  <li *ngFor="let item of arr.slice(10,20)">{{item}}</li>
</ul>

<ul>
 <li *ngFor="let item of arr.slice(20,30)>{{item}}</li>
</ul>
like image 20
Florian Avatar answered Dec 03 '25 19:12

Florian