Pagination becomes simple using this example.
public class OpportunityPagination{
public ApexPages.StandardSetController Con{
get {
if(con== null) {
con= new ApexPages.StandardSetController(Database.getQueryLocator([select name,closedate from Opportunity]));
}
con.setPageSize(5);
return con;
}
set;
}
public list<Opportunity> getOpportunities() {
return (List<Opportunity>) con.getRecords();
}
/*** Pagination control***/
// indicates whether there are more records after the current page set.
public Boolean hasNext {
get {
return con.getHasNext();
}
set;
}
// indicates whether there are more records before the current page set.
public Boolean hasPrevious {
get {
return con.getHasPrevious();
}
set;
}
// returns the page number of the current page set
public Integer pageNumber {
get {
return con.getPageNumber();
}
set;
}
// returns the first page of records
public void first() {
con.first();
}
// returns the last page of records
public void last() {
con.last();
}
// returns the previous page of records
public void previous() {
con.previous();
}
// returns the next page of records
public void next() {
con.next();
}
// returns the PageReference of the original page, if known, or the home page.
public void cancel() {
con.cancel();
}
//
/*** Pagination control***/
}
and the visual force page looks like this
<apex:page controller="OpportunityPagination" showheader="false"> <apex:form > <apex:pageblock id="ThePageBlock"> <apex:pageblocktable value="{!opportunities}" var="o"> <apex:column value="{!o.name}"></apex:column> <apex:column value="{!o.closedate}"> </apex:column> </apex:pageblocktable> <apex:panelgrid columns="5"> <apex:commandlink action="{!first}" rerender="ThePageBlock"> First</apex:commandlink> <apex:commandlink action="{!previous}" rendered="{!hasPrevious}" rerender="ThePageBlock">Previous</apex:commandlink> <apex:outputtext value="Page #{!pageNumber} "> <apex:commandlink action="{!next}" rendered="{!hasNext}" rerender="ThePageBlock">Next </apex:commandlink> <apex:commandlink action="{!last}" rerender="ThePageBlock">Last </apex:commandlink> </apex:outputtext> </apex:panelgrid> </apex:pageblock> </apex:form> </apex:page>

No comments:
Post a Comment