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>
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;
}
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With