<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Mohammad Reza Mrg on Medium]]></title>
        <description><![CDATA[Stories by Mohammad Reza Mrg on Medium]]></description>
        <link>https://medium.com/@info-golab?source=rss-a010936c36f7------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*O6zVCom8BOoyxXS0SQ9lyQ.jpeg</url>
            <title>Stories by Mohammad Reza Mrg on Medium</title>
            <link>https://medium.com/@info-golab?source=rss-a010936c36f7------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 16:04:05 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@info-golab/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Ionic Virtual Scroll With Dynamic Multi Columns]]></title>
            <link>https://info-golab.medium.com/ionic-virtual-scroll-with-dynamic-multi-columns-703c5dd0d0e7?source=rss-a010936c36f7------2</link>
            <guid isPermaLink="false">https://medium.com/p/703c5dd0d0e7</guid>
            <category><![CDATA[column]]></category>
            <category><![CDATA[infinity]]></category>
            <category><![CDATA[virtual-scroll]]></category>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[ionic]]></category>
            <dc:creator><![CDATA[Mohammad Reza Mrg]]></dc:creator>
            <pubDate>Sun, 07 Feb 2021 11:52:41 GMT</pubDate>
            <atom:updated>2021-02-08T07:52:39.079Z</atom:updated>
            <content:encoded><![CDATA[<p>How Ionic Virtual Scroll Support Dynamic multiple Columns And Infinity Scroll</p><p>In my latest Project I Found My product list is so large and it’s slow on response. So I was going to use virtual scroll, But it dose not support more than one column or dynamic multiple columns.</p><p>So I tried to found a way to make<strong> ion-virtual-scroll to support more than one columns</strong> and maybe dynamic columns.</p><p>ion-virtual-scroll has an item list like [items]=”list”, and it’ll iterate over the list. to support multiple columns we must find how many item is in list and calculate their index, After that we can use virtual-list index to retrieve<strong><em> </em></strong>data from the list.</p><p>After this story we’ll have a virtual list like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jhEUAWOCPFOduFHApIYcQw.gif" /><figcaption>change on scree orientation</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UhWz0TIbOJeH-k_NS6KMyQ.gif" /><figcaption>change on screen resize</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*llA8mLPMIpVyeselImmNLw.gif" /><figcaption>ion-virtual-scroll support multiple columns and infinity</figcaption></figure><p><strong>Let’s do it!</strong></p><p>First Create an ionic angular project and then install faker for mocking data.</p><pre>npm install faker --save<br>npm install @types/faker --save</pre><p>inside your .ts file import faker</p><pre>import * as faker from &#39;faker&#39;;</pre><p>Add virtualScroll and infinity ViewChild to .ts file</p><p>import { IonInfiniteScroll, IonVirtualScroll } from &#39;@ionic/angular&#39;;</p><pre>export class Tab3Page {</pre><pre>@ViewChild(IonVirtualScroll) virtualScroll: IonVirtualScroll;</pre><pre>@ViewChild(IonInfiniteScroll) infiniteScroll: IonInfiniteScroll;</pre><pre>...</pre><p>We must to know max Width of items, so set <strong>itemMaxWidth </strong>as much as you need.</p><ul><li>For me it is <strong>itemMaxWidth = 360;</strong></li></ul><p>Listen to Screen resize:</p><pre>getScreenSize(event?) {</pre><pre>    this.screenWidth = window.innerWidth;</pre><pre>    this.externalCol = Math.trunc(this.screenWidth / this.itemMaxWidth);</pre><pre>    this.externalCol = this.externalCol &gt; 3 ? 3 : this.externalCol;</pre><pre>}</pre><p>Get List of Data from faker:</p><pre>getEmployees() {</pre><pre>    for (let i = 0; i &lt; 250; i++) {</pre><pre>            this.dataList.push({</pre><pre>            id: this.dataList.length,</pre><pre>            image: faker.image.image(),</pre><pre>            name: faker.name.firstName(),</pre><pre>            address: faker.address.streetAddress(),</pre><pre>            intro: faker.lorem.words()</pre><pre>        });</pre><pre>    }</pre><pre>// for triggering pipe when data source changed //</pre><pre>this.nextPipe = this.dataList.length;</pre><pre>}</pre><p>Make it infinity scroll:</p><pre>loadData(event: any) {</pre><pre>    setTimeout(() =&gt; {</pre><pre>    // load more data</pre><pre>    this.getEmployees();</pre><pre>    this.virtualScroll.checkEnd();  // trigger end of virtual list</pre><pre>    event.target.complete();</pre><pre>    if (this.dataList.length === 1000) {</pre><pre>    event.target.disabled = true;</pre><pre>    }}, 500);</pre><pre>}</pre><p>Generate a pipe with ionic cli command</p><pre>ionic generate pipe pipes/virtual-list-index</pre><p>Add VirtualListIndexPipe to PipesModule</p><p>Fetch item’s index of dataList with pipe like this:</p><pre>export class VirtualListIndexPipe implements PipeTransform {</pre><pre>transform(array: Array&lt;any&gt;, exteraCol: number, nextPipe: number): number[][] {</pre><pre>const range = (s: number, e: number) =&gt; e &gt; s ? [s, ...range(s + 1, e)] : [s];</pre><pre>const virtualList = [] as number[][];</pre><pre>let i = 0;</pre><pre>for (i; i &lt; (array.length - exteraCol); i += exteraCol + 1){</pre><pre>virtualList.push([...range(i, i + exteraCol)]);</pre><pre>}</pre><pre>// last 2d array maybe not full lenght</pre><pre>virtualList.push([...range(i, array.length - 1)]);</pre><pre>return virtualList;</pre><pre>}</pre><pre>}</pre><p>Import PipesModule to your component module:</p><p>And use that pipe in your virtual scroll items like this:</p><pre>&lt;ion-virtual-scroll approxItemHeight=&quot;62.4px&quot; [itemHeight]=&quot;itemHeightFn&quot;</pre><pre>[items]=&quot;dataList | virtualListIndex: externalCol : nextPipe&quot;&gt;</pre><p>Now our virtual list items has the value of datalist items refrence and we can use it to get data from dataList</p><pre>&lt;div *virtualItem=&quot;let item;let i = index&quot;&gt;</pre><pre>    &lt;ion-row&gt;</pre><pre>        &lt;ion-col *ngFor=&quot;let j of item;&quot;&gt;</pre><pre>            &lt;h3&gt;i = {{dataList[j].id}} &lt;/h3&gt;</pre><pre>        &lt;/ion-col&gt;</pre><pre>    &lt;/ion-row&gt;</pre><pre>&lt;/div&gt;</pre><p>Complete File should be like this:</p><p><strong>tab3.ts File</strong></p><pre>import { Component, ViewChild } from &#39;@angular/core&#39;;</pre><pre>import { IonInfiniteScroll, IonVirtualScroll } from &#39;@ionic/angular&#39;;</pre><pre>import * as faker from &#39;faker&#39;;</pre><pre>import { HostListener } from &#39;@angular/core&#39;;</pre><pre>@Component({</pre><pre>selector: &#39;app-tab3&#39;,</pre><pre>templateUrl: &#39;tab3.page.html&#39;,</pre><pre>styleUrls: [&#39;tab3.page.scss&#39;]</pre><pre>})</pre><pre>export class Tab3Page {</pre><pre>@ViewChild(IonVirtualScroll) virtualScroll: IonVirtualScroll;</pre><pre>@ViewChild(IonInfiniteScroll) infiniteScroll: IonInfiniteScroll;</pre><pre>dataList = [];</pre><pre>itemMaxWidth = 360; // virtual list item max width</pre><pre>externalCol = 0;  // how much columns should be add to virtual list</pre><pre>nextPipe = 0;</pre><pre>screenWidth: number;</pre><pre>constructor() {</pre><pre>this.getScreenSize();</pre><pre>this.getEmployees();</pre><pre>}</pre><pre>@HostListener(&#39;window:resize&#39;, [&#39;$event&#39;])</pre><pre>getScreenSize(event?) {</pre><pre>this.screenWidth = window.innerWidth;</pre><pre>this.externalCol = Math.trunc(this.screenWidth / this.itemMaxWidth);</pre><pre>this.externalCol = this.externalCol &gt; 3 ? 3 : this.externalCol;</pre><pre>}</pre><pre>getEmployees() {</pre><pre>for (let i = 0; i &lt; 250; i++) {</pre><pre>this.dataList.push({</pre><pre>id: this.dataList.length,</pre><pre>image: faker.image.image(),</pre><pre>name: faker.name.firstName(),</pre><pre>address: faker.address.streetAddress(),</pre><pre>intro: faker.lorem.words()</pre><pre>});</pre><pre>}</pre><pre>this.nextPipe = this.dataList.length;</pre><pre>}</pre><pre>itemHeightFn(item, index) { // better performance if setting item height</pre><pre>return 62.4;</pre><pre>}</pre><pre>loadData(event: any) {</pre><pre>setTimeout(() =&gt; {</pre><pre>// load more data</pre><pre>this.getEmployees();</pre><pre>this.virtualScroll.checkEnd();  // trigger end of virtual list</pre><pre>event.target.complete();</pre><pre>if (this.dataList.length === 1000) {</pre><pre>event.target.disabled = true;</pre><pre>}</pre><pre>}, 500);</pre><pre>}</pre><pre>}</pre><p><strong>tab3.html file</strong></p><pre>&lt;ion-header [translucent]=&quot;true&quot;&gt;</pre><pre>&lt;ion-toolbar&gt;</pre><pre>&lt;ion-title&gt;</pre><pre>Tab 3</pre><pre>&lt;/ion-title&gt;</pre><pre>&lt;/ion-toolbar&gt;</pre><pre>&lt;/ion-header&gt;</pre><pre>&lt;ion-content [fullscreen]=&quot;true&quot; class=&quot;ion-padding&quot;&gt;</pre><pre>&lt;ion-virtual-scroll approxItemHeight=&quot;62.4px&quot; [itemHeight]=&quot;itemHeightFn&quot;</pre><pre>[items]=&quot;dataList | virtualListIndex: externalCol : nextPipe&quot;&gt;</pre><pre>&lt;div *virtualItem=&quot;let item;let i = index&quot;&gt;</pre><pre>&lt;ion-row&gt;</pre><pre>&lt;ion-col *ngFor=&quot;let j of item;&quot;&gt;</pre><pre>&lt;h3&gt;i = {{dataList[j].id}} &lt;/h3&gt;</pre><pre>&lt;/ion-col&gt;</pre><pre>&lt;/ion-row&gt;</pre><pre>&lt;/div&gt;</pre><pre>&lt;/ion-virtual-scroll&gt;</pre><pre>&lt;ion-infinite-scroll threshold=&quot;150px&quot; (ionInfinite)=&quot;loadData($event)&quot;&gt;</pre><pre>&lt;ion-infinite-scroll-content loadingSpinner=&quot;bubbles&quot; loadingText=&quot;Loading more data...&quot;&gt;</pre><pre>&lt;/ion-infinite-scroll-content&gt;</pre><pre>&lt;/ion-infinite-scroll&gt;</pre><pre>&lt;/ion-content&gt;</pre><p><strong>Tab3PageModule:</strong></p><pre>import { PipesModule } from &#39;../pipes/pipes.module&#39;;</pre><pre>@NgModule({</pre><pre>imports: [</pre><pre>PipesModule,</pre><pre>...</pre><p><strong>VirtualListIndexPipe:</strong></p><pre>import { Pipe, PipeTransform } from &#39;@angular/core&#39;;</pre><pre>@Pipe({</pre><pre>name: &#39;virtualListIndex&#39;</pre><pre>})</pre><pre>export class VirtualListIndexPipe implements PipeTransform {</pre><pre>transform(array: Array&lt;any&gt;, exteraCol: number, nextPipe: number): number[][] {</pre><pre>const range = (s: number, e: number) =&gt; e &gt; s ? [s, ...range(s + 1, e)] : [s];</pre><pre>const virtualList = [] as number[][];</pre><pre>let i = 0;</pre><pre>for (i; i &lt; (array.length - exteraCol); i += exteraCol + 1){</pre><pre>virtualList.push([...range(i, i + exteraCol)]);</pre><pre>}</pre><pre>// last 2d array maybe not full lenght</pre><pre>virtualList.push([...range(i, array.length - 1)]);</pre><pre>return virtualList;</pre><pre>}</pre><pre>}</pre><p><strong>try to resize screen in stackblitz</strong>: <a href="https://stackblitz.com/edit/ionic-angular-v5-by5rkf?file=src%2Fapp%2Fapp.component.html">https://stackblitz.com/edit/ionic-angular-v5-by5rkf?file=src%2Fapp%2Fapp.component.html</a></p><p>It was useful for my project, I hope you liked it :D</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=703c5dd0d0e7" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>