MatPaginator角材料:我怎么知道我是否在表格的最后一页



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

我试过了,但没有成功。我正在使用角材料

这是我的代码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)
})
}

}

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

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

示例:

在您的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);
}

如果表没有下一页,上面的hasNextPage()函数将返回false,在这种情况下,您可以加载更多的数据。

最新更新