function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
ranirani 

Pagination using custom controller

Hi, We are trying to acheive pagination using a custom controller and we are unable to complete the same. For instance we are trying to show the next hyperlink on the first page and then the second page should show both next & previous links if there is a third page and the last page should just show the previous link. Can we acheive this using VF page and apex code. If then how do we do it? Your help would be appreciated.
Imran MohammedImran Mohammed

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.

 

ranirani
Hey thank you so much it worked for me the other day. Its just the same code that I used but may be misplaced it. Thank you so much for your help.
shrey.tyagi88@tcs.comshrey.tyagi88@tcs.com

Hi,

 In all the blogs pagination is implemented on load of a page. Hence method is written is a constructor of the class. I wanna do this on click of a button, and not on page load. Can you plz help me with your class???

myatmyat

Hi,

 

I have added your code to extension controller except 'rerender' attributes in previous and next commandlink as I don't know what I have to add in.

 

But whatever I click either next or previous link, I'm getting the first page of data.

 

Your suggestions are very appreciated.

 

Thanks,

Myat

 

 

s_macs_mac

can somebody help with, the same visual force page even having links 1,2,3.... depending up on the size of  list.onclicking on link 1(pageno) first five records to be displayed,onclick on 2,nxt 5 records be displayed...so on

abhishek singh 497abhishek singh 497
Hello S_mac,

for showing 5 records we need to use offset.P lease go through the link below :

http://sfdcsrini.blogspot.com/2014/12/custom-controller-pagination-with.html

Let me know if you have any concerns.

Thanks & Regards,
Abhishek Singh.