anusha(salesforce developer)

Thursday, 15 September 2016

Pagination using Apex in Salesforce

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:

No comments:

Post a Comment