[英]How to implement on scrolldown pagination in Angular 4
我是angular 4的新手。我想在angular 4中實現滾動分頁。最初,我想顯示20條記錄。 向下滾動后,我要顯示下一個20。我將執行相同的操作,直到列表結尾。
我嘗試使用“ angular2-infinite-scroll”實現它。 但最初我無法顯示前20條記錄以及滾動數據。
組件文件:
import { Component, OnInit } from '@angular/core';
import { InfiniteScrollModule } from 'angular2-infinite-scroll';
@Component({
selector: 'app-scroll',
templateUrl: `./scroll.component.html`,
styleUrls: ['./scroll.component.css']
})
export class ScrollComponent implements OnInit {
let item = [{
"Name": "XYz Compnay"
},
{
"Name": "XYz Company1"
}, {
"Name": "XYz Company2"
}, {
"Name": "XYz Company3"
}, {
"Name": "XYz Company4"
}, {
"Name": "XYz Company5"
}];
constructor() {}
ngOnInit() {}
onScroll () {
alert('scrolled!!');
}
}
HTML檔案:
<div
infinite-scroll
[infiniteScrollDistance]="2"
[infiniteScrollThrottle]="10"
(scrolled)="onScroll()"
>
<p *ngFor="let items of item">
{{items.Name}}
</p>
</div>
如果有人知道,請分享。
由於新功能和與AOT的兼容性,我建議對angular2-infinite-scroll
使用ngx-infinite-scroll 。
首先,如果您不使用整個窗口並使用overflow: scroll
屬性來模擬可滾動div,則需要指定scrollWindow
屬性。 同樣在ScrollComponent
類中,您需要將item
作為類的屬性而不是變量,因此您應該使用public item
而不是let item
。
如果已知列表的大小,則可以利用infiniteScrollDisabled
來提高性能。
此外,因為它是語法不正確命名多事情item
,並呼吁一件事情的item
。 我將item
更改為items
(您可以在html模板的ngFor循環中看到)
@Component({
selector: 'my-app',
styles: [`
.search-results {
height: 100px;
overflow: scroll;
}
`],
template: `
<div class="search-results"
infiniteScroll
[infiniteScrollDistance]="2"
[infiniteScrollThrottle]="50"
(scrolled)="onScroll()"
[infiniteScrollDisabled]="isFullListDisplayed"
[scrollWindow]="false">
<p *ngFor="let item of itemsToShow; let i = index">
{{i + ' ' + item.Name}}
</p>
</div>
`
})
export class AppComponent {
private noOfItemsToShowInitially: number = 5;
// itemsToLoad - number of new items to be displayed
private itemsToLoad: number = 5;
// 18 items loaded for demo purposes
private items = [
{
"Name": "XYz Company0"
},
{
"Name": "XYz Company1"
},
{
"Name": "XYz Company2"
},
{
"Name": "XYz Company3"
},
{
"Name": "XYz Company4"
},
{
"Name": "XYz Company5"
},
{
"Name": "XYz Company6"
},
{
"Name": "XYz Company7"
},
{
"Name": "XYz Company8"
},
{
"Name": "XYz Company9"
},
{
"Name": "XYz Company10"
},
{
"Name": "XYz Company11"
},
{
"Name": "XYz Company12"
},
{
"Name": "XYz Company13"
},
{
"Name": "XYz Company14"
},
{
"Name": "XYz Company15"
},
{
"Name": "XYz Company16"
},
{
"Name": "XYz Company17"
}
];
// List that is going to be actually displayed to user
public itemsToShow = this.items.slice(0, this.noOfItemsToShowInitially);
// No need to call onScroll if full list has already been displayed
public isFullListDisplayed: boolean = false;
onScroll() {
if (this.noOfItemsToShowInitially <= this.items.length) {
// Update ending position to select more items from the array
this.noOfItemsToShowInitially += this.itemsToLoad;
this.itemsToShow = this.items.slice(0, this.noOfItemsToShowInitially);
console.log("scrolled");
} else {
this.isFullListDisplayed = true;
}
}
}
向下滾動列表,現在您將成功看到警報消息。
這是上面代碼的有用的插件 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.