How to write pagination logic?

Can someone provide any idea / logic for writing pagination logic for the search page I'm working on? I have a total number of pages for this search - 10 records per page . Also, I sent both the previous and the next page number (without problems writing logic, all I need to do is to pull out this information and fill it out. I also get information about which page I am on. I can only display 10 pages for example below

<previous 1 |2 |3 | 4| 5 | 6 | 7 | 8 | 9 | 10 next> 

Tell me if the total number of pages is 15, and when the user clicks the "Next" button, I need to display this as

 <previous 2 |3 |4 |5 |6 |7 |8 |9 |10 |11 next> 

At any moment I need to show only 10 pages paginated.

  #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 &nbsp|</a> #else <a href="/?_page=SEARCH&_action=SEARCH$link">$i &nbsp|</a> #end #set($iter = 1) #break #else #set($iter=$iter+1) #end #end #end 
+6
source share
3 answers

Here's how I implement it: As a rule, it is recommended that you create a filter class that filters data and contains information related to pagination for you. I am using something like this:

 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 (ie 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 subclasses of filters (depending on what you want to get), and each subclass will implement its createCountQuery() and createQuery() .

You would put your Filter in the Velocity context, and you could get all the necessary information from this class.

When you set the current current pf page, you update all the information you need (i.e. currentStartPageNo, currentEndPageNo).

You can also use the refresh() method to return the filter to its original state.

And, of course, you must save an instance of the same filter in the session (I mean a web framework such as Struts, Turbine, etc.) when the user goes to the search page to which Filter belongs.

This is just a guide, an idea, this is not a fully written executable code, just an example to get you started.

I will also recommend you a jQuery plugin called jqGrid , which has pagination support (although you must have support for retrieving data) and much more cool stuff. You can use it to display your data in a grid. I use it with Velocity without any problems. It has many very nice and useful features, such as a filter toolbar, editable cells, data transfer in JSON , XML , etc. Honestly, I don’t know if it has pagination as you need (I use it with only one page displayed in the navigation, and you cannot click on the page, just use the following prev buttons to navigate), but it has there may be support for that.

+9
source

Pagination on the server side . To show all the data on one page, divide them into parts to attract the attention of users. I use display-tag (To use this, maintain a single field of the POJO class to view all records)

 <form:form commandName="empdto" action="" method="post"> <h2 align="center">Employee List</h2> <display:table id="row_id" export="false" name="empdto.empList" requestURI="/list.form" pagesize="15" cellpadding="2px;" cellspacing="2px;" > <!-- class="its" --> <display:column property="id" title="ID" sortable="false" style="border:1;" /> <display:column property="name" title="Name" sortable="true" style="border:1;" /> <!-- sortProperty="name.firstname" --> <display:column property="age" title="Age" sortable="true" style="border:1;" /> <display:column property="salary" title="Salary" sortable="true" style="border:1;" /> <display:column property="address" title="Address" sortable="true" style="border:1;" /> <!-- style="width:100px" --> <display:column title="Edit"> <c:set var="clm_id" value="${row_id.id}" /> <a href="${pageContext.request.contextPath}/editemp.form?id=${clm_id}" >Edit</a> </display:column> <display:column title="Delete"> <c:set var="clm_id" value="${row_id.id}" /> <a href="${pageContext.request.contextPath}/deleteEmp.form?id=${clm_id}" onclick="selectobjID(${clm_id})">Delete </a> </display:column> </display:table> </form:form> <%@taglib uri="http://displaytag.sf.net" prefix="display" %> 
+2
source

Pay attention to the existing question: Pagination in Java

Another pagination using JPanel, change this according to your code.

The concept of pagination remains the same for all languages, and the implementation of the code requires minor modifications based on languages.

Java pagination

Refer to another existing question for more information:

pagination in java?

Other URLs of external sites: -

+1
source

Source: https://habr.com/ru/post/899606/


All Articles