ion-vertual-scroll과 ion-infinite-scroll을 사용해 일정한 양의 데이터만큼만 보여주고 스크롤 할 경우 데이터를 추가로 로드하여 보여주는 스크롤을 쉽게 구현할 수 있다.
ionic angular의 경우 --type=angular --capacitor 옵션을 붙이는 것이 좋다. (capacitor는 웹 네이티브 앱을 빌드하기 위한 네이티브 런타임)
ionic start scroll-example blank --type=angular --capacitor
<ion-content [fullscreen]="true">
// ion-header ...
<ion-virtual-scroll [items]="items">
<ion-card *virtualItem="let item">
<ion-card-header>
<ion-card-title>{{ item.name }}</ion-card-title>
</ion-card-header>
<ion-card-content>
{{ item.content }}
</ion-card-content>
</ion-card>
</ion-virtual-scroll>
<ion-infinite-scroll threshold="100px" (ionInfinite)="loadData($event)">
<ion-infinite-scroll-content loadingSpinner="bubbles" loadingText="Loading more data...">
</ion-infinite-scroll-content>
</ion-infinite-scroll>
</ion-content>
import { Component, ViewChild } from '@angular/core';
import { IonVirtualScroll } from '@ionic/angular';
@Component({
selector: 'app-home',
templateUrl: 'home.page.html',
styleUrls: ['home.page.scss'],
})
export class HomePage {
items: any[] = [];
index = 0;
@ViewChild(IonVirtualScroll) virtualScroll: IonVirtualScroll;
constructor() {
this.getCardData();
}
getCardData() {
for (let i = 0; i < 10; i++) {
this.items.push({
name: this.index,
content: 'index: ' + this.index
});
this.index++;
}
}
loadData(event) {
// Using settimeout to simulate api call
setTimeout(() => {
// load more data
this.getCardData();
//Hide Infinite List Loader on Complete
event.target.complete();
//Rerender Virtual Scroll List After Adding New Data
this.virtualScroll.checkEnd();
// App logic to determine if all data is loaded
// and disable the infinite scroll
if (this.items.length == 100) {
event.target.disabled = true;
}
}, 500);
}
}