如何在Angular 4中实现向下滚动分页

Swapnil Yeole

我是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>  

如果有人知道,请分享。

Dhyey

我建议使用NGX无限滚动针对angular2-infinite-scroll由于新的功能和与AOT的兼容性。

首先,scrollWindow如果您不使用整个窗口,而是使用overflow: scroll属性来模拟可滚动的div,需要指定属性同样,在您的ScrollComponent课程中,您需要具有itemclass的属性而不是变量,因此应使用public item代替let item

如果已知列表的大小,则可以利用它infiniteScrollDisabled来提高性能。

同样因为将多个事物命名为item并将单个事物称为an在语法上是不正确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;
        }
    }
}

向下滚动列表,现在您将成功看到警报消息。

这是上面代码的有用的插件

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章