List進行分頁,並且獲取總頁數


 1    /**
 2      * list分頁展示
 3      */
 4     public  List<ChecklistDto> getListPage(int page, int pageSize, List<ChecklistDto> list) {
 5         if (list == null || list.size() == 0) {
 6             throw new ServiceException("分頁數據不能為空!");
 7         }
 8         int totalCount = list.size();
 9         page = page - 1;
10         int fromIndex = page * pageSize;
11         //分頁不能大於總數
12         if(fromIndex>=totalCount) {
13             throw new ServiceException("頁數或分頁大小不正確!");
14         }
15 
16         int toIndex = ((page + 1) * pageSize);
17         if (toIndex > totalCount) {
18             toIndex = totalCount;
19         }
20         return list.subList(fromIndex, toIndex);
21     }
22 
23     /**
24      * 返回總頁數
25      */
26     public int getPages(List<ChecklistDto> obj,Integer pageSize){
27         int count = obj.size()/pageSize;
28         if(obj.size() == 0){
29             return 0;
30         }
31         if(obj.size() <= pageSize){
32             return 1;
33         }else if(count % pageSize == 0){
34             return count;
35         }else {
36             return count + 1;
37         }
38 
39     }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM