MatPaginator angular material:我怎么知道我是否在 table 的最后一页?

MatPaginator angular material : How can i know if i am in the last page in the table?

我必须做下一个: 假设我有一个包含 20 条记录的 table,我在 5 条中显示它们,在下一页中显示另外 5 条记录等。所以我想知道当我在最后一页时的方法,调用我的API,另外20条记录显示在table.

我已经尝试过了,但是没有成功。我正在使用 angular material

这是我的代码html

<div class="col-12 col-md-6 text-right float-right">
  <mat-form-field>
      <input matInput (keyup)="applyFilter($event.target.value)" placeholder="Buscador" upperCase>
  </mat-form-field>
</div>


<table mat-table [dataSource]="usuarios" class="mat-elevation-z8" matSort>

  <ng-container matColumnDef="Cedula">
    <th mat-header-cell *matHeaderCellDef mat-sort-header="Cedula"> Cedula </th>
    <td mat-cell *matCellDef="let user">{{ user?.Cedula }}</td>
  </ng-container>

  <ng-container matColumnDef="Nombres">
    <th mat-header-cell *matHeaderCellDef mat-sort-header="Nombres"> Nombres </th>
    <td mat-cell *matCellDef="let user">{{ user?.Nombres }}</td>
  </ng-container>

  <ng-container matColumnDef="Apellidos">
    <th mat-header-cell *matHeaderCellDef mat-sort-header="Apellidos"> Apellidos </th>
    <td mat-cell *matCellDef="let user">{{ user?.Apellidos }} </td>
  </ng-container>

  <ng-container matColumnDef="Email">
    <th mat-header-cell *matHeaderCellDef mat-sort-header="Email"> Email </th>
    <td mat-cell *matCellDef="let user">{{ user?.Email }} </td>
  </ng-container>

  <ng-container matColumnDef="Contacto">
    <th mat-header-cell *matHeaderCellDef mat-sort-header="Contacto"> Contacto </th>
    <td mat-cell *matCellDef="let user">{{ user?.Contacto }} </td>
  </ng-container>


  <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
  <tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
</table>
<mat-paginator [pageSizeOptions]="pageSize" (page)="pageEvent = handlePage($event)" showFirstLastButtons></mat-paginator>

TS

    @Component({
  selector: 'app-asignar-reclamacion',
  templateUrl: './asignar-reclamacion.component.html',
  styleUrls: ['./asignar-reclamacion.component.scss']
})
export class AsignarReclamacionComponent implements OnInit {

  //atributos
  displayedColumns: string[] = ['Cedula', 'Nombres', 'Apellidos', 'Email', 'Contacto'];
  usuarios = new MatTableDataSource([]);
  pageSize: number[] = [3];
  @ViewChild(MatPaginator, { static: false }) paginator: MatPaginator;
  @ViewChild(MatSort, { static: false }) sort: MatSort;
  //listUsuarios:User[]= []
  pageEvent: PageEvent;

  constructor(private activatedRoute: ActivatedRoute,private userService : UserService) { }

  ngOnInit(): void {
    this.activatedRoute.data.subscribe((data) => {
      console.log(data)
      this.usuarios = new MatTableDataSource(data.usuarios.page);
    })

    

  }

  // pageEvent(event){
  //   console.log(event)
  // }

  ngAfterViewInit(): void {
    this.usuarios.sort = this.sort;
    this.usuarios.paginator = this.paginator;
    console.log(this.paginator)
  }

  handlePage(event){
    let size = event.length
    let lastPage = event.pageIndex;
    if(lastPage + 1 == size){
      console.log("ultima pagina")
    }


    console.log(event)
  }
  

  applyFilter(filterValue: string) {
    filterValue = filterValue.trim();
    filterValue = filterValue.toLowerCase();
    this.usuarios.filter = filterValue;
    if(this.usuarios.filteredData.length == 0){
      this.searchDB(filterValue)
    }
    console.log(this.usuarios.filteredData)
  }

  searchDB(text:string) {
    this.userService.getUserDataFilter(text).subscribe((data)=>{
      console.log(data)
    })
  }



}

要检查您是否在 table 的最后一页,您必须检查 table 是否有下一页。

通过使用 <mat-paginator></mat-paginator> 上存在的 (page) 事件,您可以检查 table 是否有下一页。

示例:

在你的 html:

<mat-paginator [pageSizeOptions]="[5, 10, 25, 100]" (page)="onPaginateChange($event)"></mat-paginator>

在你的 ts:

onPaginateChange(event) {
    console.log(this.dataSource.paginator.hasNextPage());
    console.log(event);
}

如果table没有下一页,上面的hasNextPage()函数将returnsfalse,在这种情况下你可以加载更多数据。