Java 中的分页
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4719041/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pagination in Java
提问by user569125
I wrote paging logic:
我写了分页逻辑:
My requirement: total elements to display:100 per page,if i click next it should display next 100 records,if i click previous 100 records.
我的要求:要显示的总元素:每页 100 个,如果我点击下一步,它应该显示接下来的 100 条记录,如果我点击前 100 条记录。
Initial varaible values:
初始变量值:
- showFrom:1,
- showTo:100
- max elements:depends on size of data.
- pageSize:100.
- 显示来自:1,
- 显示到:100
- 最大元素:取决于数据的大小。
- 页面大小:100。
Code:
代码:
if(paging.getAction().equalsIgnoreCase("Next")){
paging.setTotalRec(availableList.size());
showFrom = (showTo + 1);
showTo = showFrom + 100- 1;
if(showTo >= paging.getTotalRec())
showTo = paging.getTotalRec();
paging.setShowFrom(showFrom);
paging.setShowTo(showTo);
}
else if(paging.getAction().equalsIgnoreCase("Previous")){
showTo = showFrom - 1;
showFrom = (showFrom - 100);
paging.setShowTo(showTo);
paging.setShowFrom(showFrom);
paging.setTotalRec(availableList.size());
}
Here i can remove and add the elements to the existing data.above code works fine if i add and remove few elements.but if i remove or add 100 elements at a time counts are not displaying properly above code works fine if i add and remove few elements.
在这里,我可以删除元素并将其添加到现有数据中。如果我添加和删除几个元素,上面的代码工作正常。但是如果我一次删除或添加 100 个元素,则计数显示不正确,如果我添加和删除,上面的代码工作正常几个元素。
采纳答案by brian_d
Some things to improve:
一些需要改进的地方:
- replace "magic" value of 100 with
final int PAGE_SIZE = 100;
- factor out redundant code.
- 用 100 替换“magic”值
final int PAGE_SIZE = 100;
- 剔除冗余代码。
paging.setShowTo(showTo); paging.setShowFrom(showFrom); paging.setTotalRec(availableList.size());
paging.setShowTo(showTo); paging.setShowFrom(showFrom); paging.setTotalRec(availableList.size());
to outside of if/else to make your logic clearer
到 if/else 之外,让你的逻辑更清晰
- Ensure that previous >= 0 and next <= number of records
- 确保前一个 >= 0 和下一个 <= 记录数
Edit:
编辑:
final int PAGE_SIZE = 100;
int numberOfPages = -1;
int currentPage = -1;
public void initializeVariables(){
paging.setTotalRec(availableList.size());
showFrom = 1;
showTo = PAGE_SIZE;
//keep track of how many pages there should be
numberOfPages = paging.getTotalRec()/PAGE_SIZE;
currentPage = 1;
}
public void handlePagingAction(){
if(paging.getAction().equalsIgnoreCase("Next")){
if(currentPage < numberOfPages){
++currentPage;
}
}else if(paging.getAction().equalsIgnoreCase("Previous")){
if(currentPage > 1){
--currentPage;
}
}
showFrom = (currentPage - 1) * PAGE_SIZE + 1;
if(showFrom < 0){
showFrom = 0
}
showTo = currentPage * PAGE_SIZE;
if(showTo >= paging.getTotalRec()){
showTo = paging.getTotalRec();
}
paging.setShowTo(showTo);
paging.setShowFrom(showFrom);
}
回答by Alexis Dufrenoy
Some remarks:
一些备注:
- In the line
showTo = showFrom + 100- 1;
, why minus 1? - If showTo is equal to paging.getTotalRec(), the next time you press Next, showFrom will be over paging.getTotalRec()
- In the Previous part, tests to prevent going below 0 are missing
- 在行中
showTo = showFrom + 100- 1;
,为什么是负 1? - 如果 showTo 等于 paging.getTotalRec(),则下次您按 Next 时,showFrom 将超过 paging.getTotalRec()
- 在上一部分中,缺少防止低于 0 的测试
Hope this helps...
希望这可以帮助...