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">← 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 →</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
source share