6
votes

I would like your comments and suggestion on this. I am doing the pagination for a page in jsf. The datatable is bound to a Backing Bean property through the "binding" attribute. I have 2 boolean variables to determine whether to render 'Prev' and 'Next' Button - which is displayed below the datatable. When either the 'Prev' or 'Next' button is clicked, In the backing bean I get the bound dataTable property and through which i get the "first" and "rows" attribute of the datatable and change accordingly. I display 5 rows in the page. Please comment and suggest if there any better ways. btw, I am not interested in any JSF Component libraries but stick to only core html render kit.

public String goNext()
{

    UIData htdbl = getBrowseResultsHTMLDataTable1();

    setShowPrev(true);
    //set Rows "0" or "5"
    if(getDisplayResults().size() - (htdbl.getFirst() +5)>5 )
    {
        htdbl.setRows(5);//display 5 rows
    }else if (getDisplayResults().size() - (htdbl.getFirst() +5)<=5) {
        htdbl.setRows(0);//display all rows (which are less than 5)
        setShowNext(false);
    }
    //set First
    htdbl.setFirst(htdbl.getFirst()+5);
    return "success";
}

public String goPrev()
{

    setShowNext(true);
    UIData htdbl = getBrowseResultsHTMLDataTable1();

    //set First
    htdbl.setFirst(htdbl.getFirst()-5);

    if(htdbl.getFirst()==0)
    {
        setShowPrev(false);
    }

    //set Rows - always display 5
    htdbl.setRows(5);//display 5 rows

    return "success";
}
2

2 Answers

1
votes

Please comment and suggest if there any better ways.

Well, that gives not much to answer on. It's at least not the way "I" would do, if you're asking for that. Long story short: Effective datatable paging and sorting. You only need Tomahawk (face it, it has its advantages). But if you're already on JSF2+Facelets instead of JSF1+JSP, then you can in fact also use ui:repeat and @ViewScoped instead of t:dataList and t:saveState.

0
votes

We can use 'Repeat' component - this is similar to dataList or dataTable component in Primefaces

<p:repeat id="repeatComponent" var="education" value="#{backingBean.educationList}" emptyMessage="No records found">
    <h:panelGroup>
        <p:outputLabel for="center" value="Education Center:" />
        <br />
        <h:panelGroup>
            <h:outputText id="center" value="#{education.centerName}">
            </h:outputText>
        </h:panelGroup>
    </h:panelGroup>
 </p:repeat>

This is similar to for loop in java

var - this act as loop iterator value - takes list object emptyMessage - takes String value, will get displayed when passed list object is empty