tuazku
tuazku

Reputation: 129

Bookmarkable PrimeFaces datatable paginator

Is there any way to implement bookmarkable paginator for <p:dataTable> from PrimeFaces? For example, on <p:dataTable> with row="10" when I press the pagination button to second page the url becomes http://example.com/list.xhtml?first=10&max=10.

Upvotes: 4

Views: 443

Answers (1)

Ced
Ced

Reputation: 17417

Basically you have to create it yourself as far as I know. So if you want to have the same pagination template as in primefaces example you will have to recreate the css.

To create a custom one it is pretty simple: First paginator of datatable is set to false, because you gonna make a custom one. Then make two links on top of your dataTable:

this is from new.xhtml:

<div class="nextPrevBlock">
    <h:panelGroup rendered="#{page>0}" style="margin-right:1em;">
        <h:link outcome="new.xhtml">
            <f:param name="page" value="#{page - 1}"/>
            <div class="btnLink">
                <h:outputText value="#{strings.previous}"></h:outputText>
            </div>
        </h:link>
    </h:panelGroup>
    <h:panelGroup>
        <h:link outcome="new.xhtml">
            <f:param name="page" value="#{page + 1}"/>
            <div class="btnLink">
                <h:outputText value="#{strings.next}"/>
            </div>
        </h:link>
    </h:panelGroup>
</div>

Store the page in your Managed Bean with getters and setters. Then just query with the page:

//it's requestScoped
@EJB
private ElemService elementService;
private List<Element> elementList;
@PostConstruct
public void init() {
    FacesContext fc = FacesContext.getCurrentInstance();
    Map<String, String> paramsMap = fc.getExternalContext()
            .getRequestParameterMap();
    try {
        page = Integer.parseInt(paramsMap.get("page"));
        if (page < 0)
            page = 0;
    } catch (Exception e) {
      page = 0;
    }
    getElementsOfPage(page);
}
public void getElementsOfPage(int page) {
    elementList = elementService.getElemByPage(page);
}

Then in your service :

@Override
public List<Element> giveElementList(int page) {
    Query query = em.createQuery(SELECT_NEWEST_THREADS);
    query.setFirstResult(page * NUMBER_OF_ELEMENTS_PER_PAGE);
    query.setMaxResults(NUMBER_OF_ELEMENTS_PER_PAGE);
    return query.getResultList();
}

setFirstResult() is the first result so you have to put the value of the page times the elem by page. And setMaxResults is just the elem by page you want.

You will have to customize it though so it suits your needs. I hope this help.

Upvotes: 0

Related Questions