如何在以下代码中加入分页的功能 后端代码:@Query( "select approvalRecord from ApprovalRecord approvalRecord inner join ApprovalRecordItem approvalRecordItem on approvalRecord = approvalRecordItem.approvalRecord where approvalRecordItem.status <> 'AUDITING' and approvalRecordItem.createdBy.login = ?#{principal.username} order by approvalRecordItem.id desc" ) List<ApprovalRecord> findAuditedByCurrentUser(); @GetMapping("/approval-records/my-created") public List<ApprovalRecord> getAllCreatedApprovalRecords() { log.debug("REST request to get all getAllCreatedApprovalRecords"); return approvalRecordRepository.findByCreatedByIsCurrentUser(); } 前段代码: 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 16:39:39 浏览: 74
首先,你需要在后端代码中添加分页相关的参数和逻辑。你可以使用 Spring Data JPA 提供的 Pageable 接口来实现分页。修改你的查询方法,使其接受一个 Pageable 参数,然后在查询中使用该参数。例如:
```java
@Query("select approvalRecord from ApprovalRecord approvalRecord inner join ApprovalRecordItem approvalRecordItem on approvalRecord = approvalRecordItem.approvalRecord where approvalRecordItem.status <> 'AUDITING' and approvalRecordItem.createdBy.login = ?#{principal.username} order by approvalRecordItem.id desc")
List<ApprovalRecord> findAuditedByCurrentUser(Pageable pageable);
```
接下来,在前端代码中添加分页的 UI 组件和逻辑。你可以使用 Angular Material 提供的 MatPaginator 组件来实现分页。首先,在 HTML 模板中添加 MatPaginator 组件:
```html
<mat-paginator [length]="totalItems" [pageSize]="pageSize" [pageIndex]="pageIndex" [pageSizeOptions]="pageSizeOptions" (page)="loadPage($event)"></mat-paginator>
```
然后,在对应的 TypeScript 文件中添加分页相关的属性和方法:
```typescript
totalItems: number;
pageSize = 10;
pageSizeOptions = [5, 10, 20];
pageIndex = 0;
loadPage(event: PageEvent): void {
this.pageIndex = event.pageIndex;
this.pageSize = event.pageSize;
this.loadAll();
}
loadAll(): void {
this.approvalRecordService.queryAuditing({page: this.pageIndex, size: this.pageSize}).subscribe((res: HttpResponse<IApprovalRecord[]>) => {
this.auditingApprovalRecords = res.body ?? [];
this.totalItems = res.headers.get('X-Total-Count');
});
this.approvalRecordService.queryCreated({page: this.pageIndex, size: this.pageSize}).subscribe((res: HttpResponse<IApprovalRecord[]>) => {
this.createdApprovalRecords = res.body ?? [];
});
this.approvalRecordService.queryAudited({page: this.pageIndex, size: this.pageSize}).subscribe((res: HttpResponse<IApprovalRecord[]>) => {
this.auditedApprovalRecords = res.body ?? [];
});
}
```
在 loadAll 方法中,我们将分页相关的参数传递给查询方法,并在响应中获取分页相关的信息,比如总记录数。然后,我们将总记录数赋值给 totalItems 属性,以便 MatPaginator 组件能够正确显示分页信息。
最后,你需要在后端代码中修改查询方法,使其返回一个 Page 对象,而不是 List。例如:
```java
@Query("select approvalRecord from ApprovalRecord approvalRecord inner join ApprovalRecordItem approvalRecordItem on approvalRecord = approvalRecordItem.approvalRecord where approvalRecordItem.status <> 'AUDITING' and approvalRecordItem.createdBy.login = ?#{principal.username} order by approvalRecordItem.id desc")
Page<ApprovalRecord> findAuditedByCurrentUser(Pageable pageable);
```
这样,你就成功地实现了分页功能。