如何使用材料角的分页器?

时间:2017-07-26 05:29:42

标签: angular pagination angular-material

我是角色新手并试图在我的应用中实现分页。我正在尝试使用this material component.

使用下面的代码,我可以在pagesize文件中获得pageSizeOptions.ts<md-paginator [length]="length" [pageSize]="pageSize" [pageSizeOptions]="pageSizeOptions" </md-paginator>

export class PaginatorConfigurableExample {

  length = 100;
  pageSize = 10;
  pageSizeOptions = [5, 10, 25, 100];

  }
}
nextPage

但是当我更改页面时,我似乎无法触发更改上表中数据的功能。另外,如何使用previousPagehasNextPagehasPreviousPagecanvas.setBackgroundImage(obj) 方法?

9 个答案:

答案 0 :(得分:28)

我在这里挣扎着。但我可以告诉你我做了一些研究。 基本上,您首先开始在 foo.template.ts 中添加页面@Output事件:

 <md-paginator #paginator
                [length]="length"
                [pageIndex]="pageIndex"
                [pageSize]="pageSize"
                [pageSizeOptions]="[5, 10, 25, 100]"
                (page)="pageEvent = getServerData($event)"
                >
 </md-paginator>

之后,您必须在 foo.component.ts 类和其他类中添加 pageEvent 属性来处理分页器要求:

pageEvent: PageEvent;
datasource: null;
pageIndex:number;
pageSize:number;
length:number;

并添加将获取服务器数据的方法:

ngOnInit() {
   getServerData(null) ...
}

public getServerData(event?:PageEvent){
  this.fooService.getdata(event).subscribe(
    response =>{
      if(response.error) {
        // handle error
      } else {
        this.datasource = response.data;
        this.pageIndex = response.pageIndex;
        this.pageSize = response.pageSize;
        this.length = response.length;
      }
    },
    error =>{
      // handle error
    }
  );
  return event;
}

因此,基本上每次单击分页符时,您都会激活getServerData(..)方法,该方法将调用 foo.service.ts 获取所需的所有数据。在这种情况下,您不需要处理 nextPage nextXXX 事件,因为它将在视图渲染时自动计算。

希望这可以帮到你。如果你有成功,请告诉我。 =]

答案 1 :(得分:11)

嘿所以我偶然发现了它并使其正常工作,这是如何:

在我的 component.html

<mat-paginator #paginator [pageSize]="pageSize" [pageSizeOptions]="[5, 10, 20]" [showFirstLastButtons]="true" [length]="totalSize"
    [pageIndex]="currentPage" (page)="pageEvent = handlePage($event)">
</mat-paginator>

在我的 component.ts

public array: any;
public displayedColumns = ['', '', '', '', ''];
public dataSource: any;    

public pageSize = 10;
public currentPage = 0;
public totalSize = 0;

@ViewChild(MatPaginator) paginator: MatPaginator;

constructor(private app: AppService) { }

ngOnInit() {
  this.getArray();
}

public handlePage(e: any) {
  this.currentPage = e.pageIndex;
  this.pageSize = e.pageSize;
  this.iterator();
}

private getArray() {
  this.app.getDeliveries()
    .subscribe((response) => {
      this.dataSource = new MatTableDataSource<Element>(response);
      this.dataSource.paginator = this.paginator;
      this.array = response;
      this.totalSize = this.array.length;
      this.iterator();
    });
}

private iterator() {
  const end = (this.currentPage + 1) * this.pageSize;
  const start = this.currentPage * this.pageSize;
  const part = this.array.slice(start, end);
  this.dataSource = part;
}

所有分页工作都是在iterator方法内完成的。此方法计算出skiptake,并将其分配给Material表的dataSource

答案 2 :(得分:7)

使用Slice Pipe将Angular Paginator与数据表链接的另一种方法。此处仅从服务器获取一次数据。

查看:

 <div class="col-md-3" *ngFor="let productObj of productListData | 
     slice: lowValue : highValue">
       //actual data dispaly  
 </div>

<mat-paginator [length]="productListData.length" [pageSize]="pageSize" 
   (page)="pageEvent = getPaginatorData($event)">
</mat-paginator> 

组件

    pageIndex:number = 0;
    pageSize:number = 50;
    lowValue:number = 0;
    highValue:number = 50;       

  getPaginatorData(event){
     console.log(event);
     if(event.pageIndex === this.pageIndex + 1){
        this.lowValue = this.lowValue + this.pageSize;
        this.highValue =  this.highValue + this.pageSize;
       }
    else if(event.pageIndex === this.pageIndex - 1){
       this.lowValue = this.lowValue - this.pageSize;
       this.highValue =  this.highValue - this.pageSize;
      }   
       this.pageIndex = event.pageIndex;
 }

答案 3 :(得分:2)

原始问题的问题在于您没有捕获“页面”事件,要解决此问题,您需要在md-paginator标记中添加(page)='yourEventHandler($event)'作为属性。

检查工作示例here

您还可以查看API文档here

答案 4 :(得分:2)

组件:

<SendOrder xmlns="http://wMobSrv/wMobService">
      <dsOrder>
        <xsd:schema>schema</xsd:schema>xml</dsOrder>
      <dsArticles>
        <xsd:schema>schema</xsd:schema>xml</dsArticles>
      <dsResult>
        <xsd:schema>schema</xsd:schema>xml</dsResult>
      <Database>string</Database>
</SendOrder>

HTML

import { Component,  AfterViewInit, ViewChild } from @angular/core;
import { MatPaginator } from @angular/material;

export class ClassName implements AfterViewInit {
    @ViewChild(MatPaginator) paginator: MatPaginator;
    length = 1000;
    pageSize = 10;
    pageSizeOptions: number[] = [5, 10, 25, 100];

    ngAfterViewInit() {
        this.paginator.page.subscribe(
           (event) => console.log(event)
    );
}

答案 5 :(得分:1)

这里要处理的棘手部分是页面事件。 我们可以按照角度材料文档来定义页面事件功能。

访问https://material.angular.io/components/paginator/examples 供参考。

在这方面,我将加倍努力。 相关HTML文件中的内容应如下所示,

<pre>
<mat-paginator
    [pageSizeOptions]="pageSizeOptions"
    [pageSize]="Size"
    (page)="pageEvent = pageNavigations($event)"
    [length]="recordCount">
</mat-paginator>
</pre>

然后转到相关的打字稿文件和以下代码,

声明

@Input('data') customers: Observable<Customer[]>;
pageEvent: PageEvent;
Page: number=0;
Size: number=2;
recordCount: number;
pageSizeOptions: number[] = [2,3,4,5];

页面导航的关键部分如下,

pageNavigations(event? : PageEvent){
    console.log(event);
    this.Page = event.pageIndex;
    this.Size = event.pageSize;
    this.reloadData();
  }

我们需要以下函数从后端调用数据。

reloadData() {

    this.customers = this.customerService.setPageSize(this.Page ,this.Size);
    this.customerService.getSize().subscribe(res => {
      this.recordCount = Number(res);
     
      console.log(this.recordCount);
    });
  }

在上述实现之前,我们的服务文件应包含以下服务调用

setPageSize(page: number, size: number): Observable<any> {
    return this.http.get(`${this.baseUrl}?pageSize=${size}&pageNo=${page}`);
  }

然后就可以在我们的应用程序中启用分页了。随时提出相关问题,谢谢。

答案 6 :(得分:0)

花了几个小时后,我认为这是应用分页的最佳方式。更重要的是它有效。 这是我的分页器代码 <mat-paginator #paginatoR [length]="length" [pageSize]="pageSize" [pageSizeOptions]="pageSizeOptions">

在我的组件@ViewChild(MatPaginator) paginator: MatPaginator;中查看子项,最后你必须将paginator绑定到表dataSource,这就是它的完成方式 ngAfterViewInit() {this.dataSource.paginator = this.paginator;} 容易吗?如果它适合你,那么将其标记为答案。

答案 7 :(得分:0)

花了几个小时后,这个问题已经解决,我已经开始工作了。相信这是解决角形材料分页的最简单方法。  -首先从处理(component.html)文件开始

 <mat-paginator [pageSizeOptions]="[2, 5, 10, 15, 20]" showFirstLastButtons>
 </mat-paginator>

然后在(component.ts)文件中执行

 import { MatPaginator } from '@angular/material/paginator';
 import { Component, OnInit, ViewChild } from '@angular/core';

 export interface UserData {
 full_name: string;
 email: string;
 mob_number: string;
 }

 export class UserManagementComponent implements OnInit{
  dataSource : MatTableDataSource<UserData>;

  @ViewChild(MatPaginator) paginator: MatPaginator;

  constructor(){
    this.userList();
   }

    ngOnInit() { }

   public userList() {

   this._userManagementService.userListing().subscribe(
      response => {

      console.log(response['results']);
      this.dataSource = new MatTableDataSource<UserData>(response['results']);
      this.dataSource.paginator = this.paginator;
      console.log(this.dataSource);

     },


      error => {});

     }

 }

记住,必须将分页模块导入当前正在工作的module(module.ts)文件中。

  import {MatPaginatorModule} from '@angular/material/paginator';

   @NgModule({
      imports: [MatPaginatorModule]
    })

希望它会为您工作。

答案 8 :(得分:0)

基于Wesley Coetzee的回答,我写了这个。希望它可以帮助任何使用此问题的人。我在列表中间交换了分页器大小时遇到​​了一些错误,这就是我提交答案的原因:

分页器html和列表

<mat-paginator [length]="localNewspapers.length" pageSize=20
               (page)="getPaginatorData($event)" [pageSizeOptions]="[10, 20, 30]"
               showFirstLastButtons="false">
</mat-paginator>
<mat-list>
   <app-newspaper-pagi-item *ngFor="let paper of (localNewspapers | 
                         slice: lowValue : highValue)"
                         [newspaper]="paper"> 
  </app-newspaper-pagi-item>

组件逻辑

import {Component, Input, OnInit} from "@angular/core";
import {PageEvent} from "@angular/material";

@Component({
  selector: 'app-uniques-newspaper-list',
  templateUrl: './newspaper-uniques-list.component.html',
})
export class NewspaperUniquesListComponent implements OnInit {
  lowValue: number = 0;
  highValue: number = 20;

  {...} // not relevant logic

  // used to build an array of papers relevant at any given time
  public getPaginatorData(event: PageEvent): PageEvent {
    this.lowValue = event.pageIndex * event.pageSize;
    this.highValue = this.lowValue + event.pageSize;
    return event;
  }

}