Pagination using Apex in Salesforce

Sample Code:

Visualforce page:
 
<apex:page controller="Sample" >
<apex:form >
    <apex:pageBlock id="details">
        <apex:pageblockTable value="{!memb}" var="m">
            <apex:column value="{!m.Name}"/>
            <apex:column value="{!m.Age__c}"/>           
        </apex:pageblockTable>
        <apex:pageblockButtons >
            <apex:commandButton value="<<" rerender="details" action="{!beginning}" disabled="{!prev}"/>
            <apex:commandButton value="<" rerender="details" action="{!previous}" disabled="{!prev}"/>
            <apex:commandButton value=">" rerender="details" action="{!next}" disabled="{!nxt}"/>
            <apex:commandButton value=">>" rerender="details" action="{!end}" disabled="{!nxt}"/>                                   
        </apex:pageblockButtons>
    </apex:pageBlock>
</apex:form>
</apex:page>




Apex Class:

public class Sample
{   
    private integer totalRecs = 0;    
    private integer index = 0;
    private integer blockSize = 5;        
   
    public sample()
    {
        totalRecs = [select count() from Member__c];       
    }   
   
    public List<Member__c> getMemb()
    {
        List<Member__c> membs = Database.Query('SELECT Name, Age__c FROM Member__c LIMIT :blockSize OFFSET :index');
        System.debug('Values are ' + membs);
        return membs;
    }   
   
    public void beginning()
    {
        index = 0;
    }
   
    public void previous()
    {
        index = index - blockSize;
    }
   
    public void next()
    {
        index = index + blockSize;
    }

    public void end()
    {
        index = totalrecs - math.mod(totalRecs,blockSize);
    }       
   
    public boolean getprev()
    {
        if(index == 0)
        return true;
        else
        return false;
    } 
   
    public boolean getnxt()
    {
        if((index + blockSize) > totalRecs)
        return true;
        else
        return false;
    }        
}


Output:



For demo, visit

http://infallibletechie-developer-edition.ap1.force.com/

9 comments:

  1. Fantastic stuff sir...
    if u provide some more explanation regarding logic that will help users to understand more..

    ReplyDelete
    Replies
    1. Instead of me explaining the logic, take some time to understand the Apex code.

      It is very simple and clear.

      Cheers!!!

      Delete
  2. I will add something
    If my totalrecs == 110 and my blockSize == 5 the result of the formula used to go to the end of the pages will make my index==110 and wont query my last page
    I fixed using the next

    public void end() { //user clicked end
    if(math.mod(totalRecs,blockSize)==0)
    index= totalrecs -blockSize;
    else
    index = totalrecs - math.mod(totalRecs,blockSize);
    }

    and


    public Boolean getnxt() { //this will disable the next and end buttons
    if (index + blockSize >= totalrecs) return true; else return false;
    }

    ReplyDelete
    Replies
    1. btw, thank you soo much for this post!! It was really really helpfull

      Delete
  3. Any pointers of how do i overcome the governor limit for Offset with this code or any ? I read that by Id field or autonumber it can be done , but how do i achieve it ?

    ReplyDelete
  4. Hi Magulan D,,,I have a Problem with pagination,,,I have aggregate results query and am able to display records in visualforce page,,when i tried to create a pagination for these records am getting an error like Aggregate results does not support queryMore() call,,,how to create a pagination for Aggregate results records,,,help me

    ReplyDelete
    Replies
    1. Hi,
      Create a wrapper class for the aggregate result.
      Create a list of wrapper class and use that in pagination.

      Cheers!!!

      Delete