若依框架对拼接的list集合进行分页
废话少说,直接干货。
第一种分页:简单的说,对查询结果进行分页,主要是startPage()方法,自己点进去看看
/**
* 获取用户列表
*/
@PreAuthorize(hasPermi = "system:user:list")
@GetMapping("/list")
public TableDataInfo list(SysUser user) {
startPage();
List<SysUser> list = userService.selectUserList(user);
return getDataTable(list);
}
/**
* 设置请求分页数据
*/
protected void startPage() {
PageDomain pageDomain = TableSupport.buildPageRequest();
Integer pageNum = pageDomain.getPageNum();
Integer pageSize = pageDomain.getPageSize();
if (StringUtils.isNotNull(pageNum) && StringUtils.isNotNull(pageSize)) {
String orderBy = SqlUtil.escapeOrderBySql(pageDomain.getOrderBy());
PageHelper.startPage(pageNum, pageSize, orderBy);
}
}
第二种分页:首先对list集合进行拼接,然后再进行分页,这就和第一种方法不同了。hoho ,下面我先说前端vue代码,再说后端java代码
前端(vue):
<el-pagination
@size-change="getListSizeChange"
@current-change="getListCurrentChange"
:current-page="queryParams.pageNum"
:page-sizes="[10, 12, 15, 20]"
:page-size="queryParams.pageSize"
layout="total, sizes, prev, pager, next, jumper"
:total="total"/>
data() {
return {
queryParams: {
module: '',
taskName: '',
pageNum: 1,
pageSize: 10
}
}
},
methods:{
getListSizeChange(newPageSize) {
this.queryParams.pageSize = newPageSize;
this.getList();
},
getListCurrentChange(newPageSize) {
this.queryParams.pageNum = newPageSize;
this.getList();
}
}
后端(java):
@ApiOperation(value = "待办任务", notes = "待办任务")
@GetMapping("/todoTask/list")
@ApiImplicitParams({
@ApiImplicitParam(name = "module", value = "模块名称"),
@ApiImplicitParam(name = "taskName", value = "标题"),
@ApiImplicitParam(name = "pageNum", value = "当前记录起始页"),
@ApiImplicitParam(name = "pageSize", value = "每页显示条数")
})
public TableDataInfo getTodoItems(@RequestParam(value = "module", required = false) String module, @RequestParam(value = "taskName", required = false) String taskName) {
/**第一步:pageNum和pageSize是从前端数据里传进来的分页对象的属性**/
PageDomain pageDomain = TableSupport.buildPageRequest();
Integer pageNum = pageDomain.getPageNum();
Integer pageSize = pageDomain.getPageSize();
String userName = SecurityUtils.getUsername();
List<TodoTaskDTO> result = new ArrayList<TodoTaskDTO>();
/**第二步:过滤数据**/
TodoTaskDTO vo = new TodoTaskDTO();
vo.setTitle(taskName);
vo.setType(module);
vo.setAssignee(userName);
/**第三步:拼接list集合**/
List<TodoTaskDTO> safeTaskList = taskCenterService.getSafeHiddenTodoItem(vo);
List<TodoTaskDTO> riskTaskList = taskCenterService.getRiskTodoItem(vo);
List<TodoTaskDTO> accTaskList = taskCenterService.getAccTodoItem(vo);
List<TodoTaskDTO> accReportTaskList = taskCenterService.getAccReportTodoItem(vo);
List<TodoTaskDTO> emergencyTaskList = taskCenterService.getEmergencyTodoItem(vo);
result.addAll(safeTaskList);
result.addAll(riskTaskList);
result.addAll(accTaskList);
result.addAll(accReportTaskList);
result.addAll(emergencyTaskList);
/**第四步:获取处理好的list集合**/
int num = result.size();
result = result.stream().skip((pageNum - 1) * pageSize).limit(pageSize).collect(Collectors.toList());
TableDataInfo rspData = new TableDataInfo();
rspData.setCode(0);
rspData.setRows(result);
rspData.setTotal(num);
return rspData;
}
最终显示效果:
总结:需要可以自己处理好了,自定义返回数据即可。主要代码如下:
int num = result.size();
result = result.stream().skip((pageNum - 1) * pageSize).limit(pageSize).collect(Collectors.toList());
TableDataInfo rspData = new TableDataInfo();
rspData.setCode(0);
rspData.setRows(result);
rspData.setTotal(num);
再见,再见,再见!
本文来自博客园,作者:星星之草%,转载请注明原文链接:https://www.cnblogs.com/zhaodefu/p/16575935.html