1. 程式人生 > >List進行分頁,並且獲取總頁數

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 }