Java – how to write paging logic?
Can anyone provide some ideas / logic to write paging logic for the search page I'm studying?
<prevIoUs 1 |2 |3 | 4| 5 | 6 | 7 | 8 | 9 | 10 next>
Assuming that the total number of pages is 15, when the user clicks next, I need to display the following
<prevIoUs 2 |3 |4 |5 |6 |7 |8 |9 |10 |11 next>
At any time, I only need to display 10 pages in paging
#set($start = 1) #set($end = $Integer.parseInt($searchTO.getPagination().getNumberofPages())) #set($range = [$start..$end]) #set($iter = 1) #foreach($i in $range) #foreach($link in $searchTO.getPagination().getDirectPageLinks()) #if($i == $iter) #if ($Integer.parseInt($searchTO.getPagination().getPageNumber())==$iter) <a class="search_current" href="/?_page=SEARCH&_action=SEARCH$link">$i  |</a> #else <a href="/?_page=SEARCH&_action=SEARCH$link">$i  |</a> #end #set($iter = 1) #break #else #set($iter=$iter+1) #end #end #end
Solution
Here's how I'll do it:
public abstract class Filter{ /** Member identifier for the current page number */ private int currentPageNo; /** Member identifier for the current start page number in the page navigation */ private int currentStartPageNo; /** Member identifier for the current end page number in the page navigation */ private int currentEndPageNo; /** Member identifier for the number of elements on a page */ private int elementsPerPage; /** Member identifier for the number of pages you have in the navigation (i.e 2 to 11 or 3 to 12 etc.) */ private int pageNumberInNavigation; public abstract Query createCountQuery(); public abstract Query createQuery(); public void setCurrentPageNo(){ //Your code here //Validation,variable setup } public Long getAllElementsCount(){ //Now this depends on the presistence framework you use,this code is //just for guidance and has Hibernate-like Syntax Query query = createCountQuery(); List list = query.list(); return !list.isEmpty() && list.get(0) != null ? query.list().get(0) : 0; } public List getElements(){ //Now this depends on the presistence framework you use,this code is //just for guidance and has Hibernate-like Syntax Query query = createQuery(); int from = ((currentPageNo - 1) * elementsPerPage); query.setFirstResult(from); query.setMaxResults(elementsPerPage); //If you use Hibernate,you don't need to worry for null check since if there are no results then an empty collection is returned return query.list(); } public List getAllElements(){ Query query = createQuery(); return query.list(); } public void refresh(){ //Your code here } public List next(){ //Move to the next page if exists setCurrentPageNo(getCurrentPageNo() + 1); getElements(); } public List previoius(){ //Move to the prevIoUs page if exists setCurrentPageNo(getCurrentPageNo() - 1); getElements(); } }
You can have special filter subclasses (depending on what you want to retrieve), and each subclass will implement its createcountquery () and createquery ()
You can then put the filter in the velocity context, and then you can retrieve all the information you need from this class
When you set the current page, you will update all other information you need (i.e. currentstartpageno, currentendpageno)
You can also use the refresh () method to restore the filter to its original state
Of course, when users navigate on the search page to which the filter belongs, you should keep instances of the same filter in the session (I mean your web framework, such as struts, turbo, etc.)
It's just a guideline, an idea, it's not fully written executable code, it's just an example that lets you start moving in one direction
I also recommend a jQuery plug-in called jqgrid, which has paging support (although you must have one to support data retrieval) and more cool things You can use it to display data in a grid I have no problem using it with velocity It has many useful functions, such as filter toolbar, editable cell, JSON data transfer, XML and so on To be honest, I don't know if it has the pagination you need (I only use one page displayed in the navigation, and you can't click the page, just use the next and previous button to navigate), but it may support that