Activity Forums Salesforce® Discussions How can I use Custom Pagination in Salesforce?

  • Abhinav

    Member
    April 14, 2016 at 8:06 am
  • Sourabh

    Member
    April 14, 2016 at 8:10 am

    Hi Shubham ,

    Giving you an example of custom pagination, hope it will help:-

    VisualForce Page:-

    <apex:page controller=”Pagination” sidebar=”false” showHeader=”false”>
    <apex:form >
    <apex:pageBlock id=”details”>
    <apex:pageblockTable value=”{!acclist}” var=”acc”>
    <apex:column value=”{!acc.Name}”/>
    <apex:column value=”{!acc.website}”/>
    <apex:column value=”{!acc.AnnualRevenue}”/>
    <apex:column value=”{!acc.Description}”/>
    <apex:column value=”{!acc.Type}”/>
    </apex:pageblockTable>
    <apex:pageblockButtons >
    <apex:commandButton value=”First Page” rerender=”details” action=”{!FirstPage}” disabled=”{!prev}”/>
    <apex:commandButton value=”Previous” rerender=”details” action=”{!previous}” disabled=”{!prev}”/>
    <apex:commandButton value=”Next” rerender=”details” action=”{!next}” disabled=”{!nxt}”/>
    <apex:commandButton value=”Last Page” rerender=”details” action=”{!LastPage}” disabled=”{!nxt}”/>
    </apex:pageblockButtons>
    </apex:pageBlock>
    </apex:form></apex:page>

     

    Apex Controller:-

    public class Pagination
    {
    private integer totalRecs = 0;
    private integer OffsetSize = 0;
    private integer LimitSize= 10;
    public Pagination()
    {
    totalRecs = [select count() from account];
    }
    public List<account> getacclist()
    {
    List<account> acc = Database.Query(‘SELECT Name, website, AnnualRevenue, description, Type FROM account LIMIT :LimitSize OFFSET :OffsetSize’);
    System.debug(‘Values are ‘ + acc);
    return acc;
    }
    public void FirstPage()
    {
    OffsetSize = 0;
    }
    public void previous()
    {
    OffsetSize = OffsetSize – LimitSize;
    }public void next()
    {
    OffsetSize = OffsetSize + LimitSize;
    }public void LastPage()
    {
    OffsetSize = totalrecs – math.mod(totalRecs,LimitSize);
    }
    public boolean getprev()
    {
    if(OffsetSize == 0)
    return true;
    else
    return false;
    }
    public boolean getnxt()
    {
    if((OffsetSize + LimitSize) > totalRecs)
    return true;
    else
    return false;
    }
    }

  • Naman

    Member
    April 18, 2016 at 12:22 pm

    It depend on your data limit which pagination you have to implement.

    1. If your data limit is under 2000, Offset based pagination can be helpful.
    2. If more than 2000, you can create your own custom offset functionality in apex and use that on next button click.
  • Parul

    Member
    September 20, 2018 at 12:34 am

    Hi,

    Hi,

     

    Add the below code to you Apex controller

     

     

    public ApexPages.StandardSetController samplePagination{
    get {
    if(samplePagination== null) {
    samplePagination= new ApexPages.StandardSetController(Database.getQueryLocator(<<Add your SOQl query here>>));
    //set number of records you want to show on UI
    samplePagination.setPageSize(5);
    }
    return samplePagination;
    }
    set;
    }
    // indicates whether there are more records after the current page set.
    public Boolean hasNext {
    get {
    return samplePagination.getHasNext();
    }
    set;
    }

    // indicates whether there are more records before the current page set.
    public Boolean hasPrevious {
    get {
    return samplePagination.getHasPrevious();
    }
    set;
    }
    public ApexPages.StandardSetController samplePagination

    {

    get

    {

    if(samplePagination== null)

    {

    samplePagination= new ApexPages.StandardSetController(Database.getQueryLocator(<<Add your SOQl query here>>));

    //set number of records you want to show on UI

    samplePagination.setPageSize(5);

    }

    return samplePagination;

    }

    set;

    }

     

     

    public void previous()

    {

    samplePagination.previous();

    }

     

    public void next()

    {

    samplePagination.next();

    }

     

    public Boolean hasNext

    {
    get

    {

    return samplePagination.getHasNext();

    }

    set;

    }

     

    public Boolean hasPrevious

    {

    get

    {

    return samplePagination.getHasPrevious();

    }

    set;

    }

     

     

    Add the below code to your Visualforce page

     

     

    <apex:panelGrid columns="8" id="<<your id>>">

    <apex:commandLink action="{!previous}" rendered="{!hasPrevious}" rerender="{{Add the IDs which you want to rerender}}">

    <apex:outputLabel value="<<Previous" style="font-weight: bold;"/>

    </apex:commandlink>

    <apex:commandLink action="{!next}" rendered="{!hasNext}" rerender="{{<<Add the IDs which you want to rerender}}">

    <apex:outputLabel value="Next>>" style="font-weight: bold;"/>

    </apex:commandlink>

    </apex:panelGrid>

    Let me know if you have any queries.

  • shariq

    Member
    September 20, 2018 at 7:44 pm

    Hi,

    Note - 

    If your data limit is under 2000, Offset based pagination can be helpful.

    If more than 2000, you can create your own custom offset functionality in apex and use that on next button click.

    Hope this helps.

Log In to reply.

Popular Salesforce Blogs