Java paginators

I am writing a small web application with game 2 and java. I have a list of entities, and now I'm going to paginate them. I take the pagination component from Twitter Bootstrap, force the database query to return entity pages, and how should I use to create the page navigator?

I mean the following: for example, I have only 20 pages and I want to show the 12th. I have 5 positions to show buttons on the page. Are there some libs or snippets to create something like: 1 .. 11 12 13 .. 20. I know that this is not too complicated, and I implemented it in my previous project ... but have one jar that will allow this problem is cool for me. Are there any solutions?

+4
source share
2 answers

I can share with you what I have, I also use bootstrap

package models.helper; import java.util.ArrayList; import java.util.List; public class Pager<T> { public List<T> entities; public int page; public int pages; public String urlTemplate; public int resultsPerPage; public List<Integer> getPageList(){ List<Integer> pageList = new ArrayList<Integer>(pages); for(int i = 1; i<=pages;i++){ pageList.add(i); } return pageList; } public int getPrevious(){ return page -1; } public int getNext(){ return page +1; } public boolean isCurrent(Integer page){ return this.page == page - 1; } public boolean isLast(){ return page == pages -1; } public boolean isFirst(){ return page == 0; } public String getUrl(Integer page){ return urlTemplate .replace("-1", Integer.toString(resultsPerPage)) .replace("-2", Integer.toString(page-1)); } public String getNextUrl(){ if(isLast()){ return "#"; } return urlTemplate .replace("-1", Integer.toString(resultsPerPage)) .replace("-2", Integer.toString(getNext())); } public String getPreviousUrl(){ if(isFirst()){ return "#"; } return urlTemplate .replace("-1", Integer.toString(resultsPerPage)) .replace("-2", Integer.toString(getPrevious())); } } 

My service returns one of these objects, and in my template I have a pager tag

 @(pager:models.helper.Pager[_]) <div class="pagination"> <ul> <li class=" prev@if (pager.isFirst){ disabled}"> <a href="@pager.getPreviousUrl">&larr; Previous</a> </li> @for(page <- pager.getPageList){ <li @if(pager.isCurrent(page)){class="active"}> <a href="@pager.getUrl(page)">@page</a> </li> } <li class=" next@if (pager.isLast){ disabled}"> <a href="@pager.getNextUrl">Next &rarr;</a> </li> </ul> </div> 

the url template will be built in this way (scala) the first parameter is "resultsPerPage", and the second is the actual "page"

 pager.urlTemplate = routes.Application.myAction(-1, -2).url 

options -1 and -2 are a little ugly, but so far I have not found a better solution

+4
source

There is no support for paging in templates, as far as I know, however, fortunately, Ebean supports it, so you just need to do a little work in your templates.

Check the Example application for a database for computers , it displays paging for a list of computers (it also uses Twitter Bootstrap, so you just need to copy and paste the code)

Of course, you can also find an example application in the Play version that you downloaded to your computer.

+3
source

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


All Articles