Pagination in java?

I need the numbers displayed in this format.

1 2 3 4 5 ^
where if i press 5 then it should display from 5 to 10

5 6 7 8 9 10

until maximum records are available. I just want to know how to do this to display numbers.

+2
source share
4 answers

Usually you want your database to do the hard work of pagination and sorting. With MySQL, for example, you can get a results page sorted by date by adding

ORDER BY date ASC LIMIT 5,5 

to the end of your SQL query. If you use hibernation, you can do this in a vendor-independent way using the API criteria:

 List<MyDomainClass> results = (List<MyDomainClass>) getSession() .createCriteria(MyDomainClass.class) .setFirstResult(5) .setMaxResults(5) .addOrder(Order.asc("date")) .list(); 

To display paginated navigation, you also need to calculate the total number of results to find out how many pages there are:

 int count = (int) getSession() .createCriteria(MyDomainClass.class) .setProjection(Projections.rowCount()) .list().get(0); 

You can add restrictions for both searches, for example, if you want to filter out the last name you could add:

 .add(Restrictions.eq("lastname", "Smith") 

(This needs to be added to both the count query and the list query).

When you know the total number of results, the number of pages and the number of the current page, you can calculate the range of such results:

 // TODO: execute the count query here to determine totalResults int totalPages = Math.ceil(totalResults / pageSize); // show first page by default int firstResult = 0; if (currentPage >= 0 && currentPage < totalPages) { firstResult = currentPage * pageSize; } // the number of items might be less than the page size (ie on the last page) int count = Math.min(pageSize, totalResults - firstResult); // TODO: execute the list query here to get a page of data 

How you show navigation is up to you. Some platforms have standard components. Otherwise, you will have to think about how to handle a huge number of pages. A general approach is to show a range of, for example, 10 page numbers and a forward / reverse jump to start / go to end links.

Hope this helps.

+8
source

We cannot give the specific / exact answer that you expected. Here we can give you some tips on some logical ways to fulfill your request.

First of all, read some documents to create a well-designed structure for your pagination. Here are some links that can help you design your pagination.

For my personal experience in developing my pagination, big data will be retrieved from the database. I will use LIMIT in my sql query to consolidate the results. And I will do the following.

  • First, I have a class / object model that will contain the pages that will be displayed at my presentation level.
  • Request processing will be processed at the DAO / my business level.
  • Now at my Services / Logic level, the assignment of values ​​to my model will be handled. This is where data transfer and request will also be performed.
  • And in my view layer, which will display the results of the query and the parameter for all links paginated.

I know that my explanation is not clear, but I hope that you will call it that.

And now you can ask what is appropriate for your question. This is the process of the presentation interacting with your business layer. I mean the stream, how do you get the result you need if the page number was called. It seems not quite clear that I will make pseudo codes that are more related to your question.

Since you do not have (first | Previous | page not here ... | Next | Last) in your pagination. I will make it as short as possible.

First page load with default pages. Assuming page data is taken from a database and using LIMIT

  • DAO / Business - SELECT pagesContent / link FROM someTables LIMIT 0.5; ..
  • Services / Logic - Put the links for page 1, etc ... and put it in the pagination model.
  • Presentation / Display - Display the pages that are stored in the pagination model. Now on page 5 or the last page, you will receive a request that you pass to another parameter that changes the initial limit of the request, and you can also pass the nth page.

The logic here is that from your pagination screen you need to pass a value that your logical level understands what the next pages will display. Another way - you can set a condition on the logical level how to count pages, if it is not in the range of 1 - 4 or something else that needs to be checked.

Hope this helps.

+4
source

Last week I found that they have great tools and gadgets, so check out http://www.primefaces.org/showcase/ui/datatableHome.jsf if you do this on the desktop of an application that you can watch on javaFX http://javafx.com/

+2
source

If you have a list of entries, you can paginate using the subList method List .

List.subList (fromIndex, endIndex);

returns a representation of part of this list between the specified fromIndex, inclusive and toIndex, exclusive

0
source

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


All Articles