以下三个方法已经取出了我想要的对应的数据现在想要对这些数据分别进行分页处理我应该怎么修改代码 loadAll(): void { this.approvalRecordService.queryAuditing().subscribe((res: HttpResponse<IApprovalRecord[]>) => { this.auditingApprovalRecords = res.body ?? []; }); this.approvalRecordService.queryCreated().subscribe((res: HttpResponse<IApprovalRecord[]>) => { this.createdApprovalRecords = res.body ?? []; }); this.approvalRecordService.queryAudited().subscribe((res: HttpResponse<IApprovalRecord[]>) => { this.auditedApprovalRecords = res.body ?? []; }); }
时间: 2023-12-10 17:41:59 浏览: 60
你可以使用 Angular Material 提供的 `MatPaginator` 组件来实现分页。具体实现步骤如下:
1. 在组件的 HTML 模板中,添加 `MatPaginator` 组件。例如:
```html
<mat-paginator [length]="auditingApprovalRecords.length" [pageSize]="pageSize" [pageSizeOptions]="pageSizeOptions" (page)="onAuditingPageChange($event)">
</mat-paginator>
```
其中,`length` 表示数据总数,`pageSize` 表示每页显示的数据条数,`pageSizeOptions` 表示用户可选的每页数据条数选项,`page` 事件表示当用户点击页码时的回调函数。
2. 在组件的 TypeScript 文件中,定义相应的属性和方法。例如:
```typescript
import { Component, OnInit, ViewChild } from '@angular/core';
import { MatPaginator } from '@angular/material/paginator';
import { IApprovalRecord } from 'app/shared/model/approval-record.model';
import { ApprovalRecordService } from 'app/entities/approval-record/approval-record.service';
@Component({
selector: 'jhi-approval-record',
templateUrl: './approval-record.component.html',
})
export class ApprovalRecordComponent implements OnInit {
@ViewChild(MatPaginator) paginator!: MatPaginator;
auditingApprovalRecords: IApprovalRecord[] = [];
createdApprovalRecords: IApprovalRecord[] = [];
auditedApprovalRecords: IApprovalRecord[] = [];
pageSize = 10;
pageSizeOptions: number[] = [5, 10, 20];
constructor(private approvalRecordService: ApprovalRecordService) {}
ngOnInit(): void {
this.loadAll();
}
loadAll(): void {
this.approvalRecordService.queryAuditing().subscribe((res: HttpResponse<IApprovalRecord[]>) => {
this.auditingApprovalRecords = res.body ?? [];
this.auditingApprovalRecords.paginator = this.paginator; // 绑定分页组件
});
this.approvalRecordService.queryCreated().subscribe((res: HttpResponse<IApprovalRecord[]>) => {
this.createdApprovalRecords = res.body ?? [];
this.createdApprovalRecords.paginator = this.paginator; // 绑定分页组件
});
this.approvalRecordService.queryAudited().subscribe((res: HttpResponse<IApprovalRecord[]>) => {
this.auditedApprovalRecords = res.body ?? [];
this.auditedApprovalRecords.paginator = this.paginator; // 绑定分页组件
});
}
onAuditingPageChange(event: any): void {
this.auditingApprovalRecords.paginator = this.paginator;
}
onCreatedPageChange(event: any): void {
this.createdApprovalRecords.paginator = this.paginator;
}
onAuditedPageChange(event: any): void {
this.auditedApprovalRecords.paginator = this.paginator;
}
}
```
其中,`@ViewChild(MatPaginator) paginator!: MatPaginator` 表示获取 `MatPaginator` 组件的实例。在 `loadAll()` 方法中,将获取到的数据绑定到 `MatPaginator` 组件上。分别定义了三个回调函数 `onAuditingPageChange`、`onCreatedPageChange` 和 `onAuditedPageChange`,用来处理分页变化事件。
注意:在绑定分页组件时,需要先将获取到的数据赋值给对应的属性,再将属性绑定到分页组件上。
阅读全文
相关推荐
通过JS实现在页面分页.
<script>
var a = document.getElementById("all").getElementsByTagName("div");
var zz =new Array(a.length);
for(var i=0;i <a.length;i++){
zz[i]=a[i].innerHTML;
} //div的字符串数组付给zz
var pageno=1; //当前页
var pagesize=3; //每页多少条信息
if(zz.length%pagesize==0){
var pageall =zz.length/pagesize;
}else{
var pageall =parseInt(zz.length/pagesize)+1;
} //一共多少页
function change(e){
pageno=e;
if(e<1){ //如果输入页<1页
e=1;
pageno=1;//就等于第1页 , 当前页为1
}
if(e>pageall){//如果输入页大于最大页
e=pageall;
pageno=pageall;//输入页和当前页都=最大页
}
document.getElementById("all").innerHTML="";//全部清空
for(var i=0;i["+j+"]";
}else{
ye=ye+"["+j+"]";
}
}
document.getElementById("a1").innerHTML=pageall;
document.getElementById("a2").innerHTML=pageno;
document.getElementById("a3").innerHTML=ye;
}
change(1);
</script>