Friday, 4 January 2013

Pagination using standardset controller



public class opportunityList2Con123 {
  // ApexPages.StandardSetController must be instantiated
   
  // for standard list controllers
       
    public ApexPages.StandardSetController setCon {
        get {
            if(setCon == null) {
                setCon = new ApexPages.StandardSetController(Database.getQueryLocator(
                      [select name,closedate from Opportunity]));
            }
            return setCon;
        }
        set;
    }

    // Initialize setCon and return a list of records
   
    public List<Opportunity> getOpportunities() {
         setCon.setpagesize(5);
         return (List<Opportunity>) setCon.getRecords();
    }
   
}
============
Page
===========

<apex:page controller="opportunityList2Con123">
    <apex:form >
        <apex:pageBlock >
            <apex:pageBlockTable value="{!opportunities}" var="o">
                <apex:column value="{!o.name}"/>
                <apex:column value="{!o.closedate}"/>
            </apex:pageBlockTable>
           
            <apex:commandButton rendered="{!setCon.hasPrevious}" value="first" action="{!setCon.first}"/>
            <apex:commandButton rendered="{!setCon.hasPrevious}" value="Previous" action="{!setCon.previous}"/>
            <apex:outputText rendered="{!(setCon.pageNumber * setCon.pageSize) < setCon.ResultSize}" value="{!setCon.pageNumber * setCon.pageSize} Of {!setCon.ResultSize}"></apex:outputText>
            <apex:outputText rendered="{!(setCon.pageNumber * setCon.pageSize) >= setCon.ResultSize}" value="{!setCon.ResultSize} Of {!setCon.ResultSize}"></apex:outputText>
           
            <apex:commandButton rendered="{!setCon.hasNext}" value="next" action="{!setCon.next}"/>
           
            <apex:commandButton rendered="{!setCon.hasNext}" value="last" action="{!setCon.last}"/>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Output:
=======