Pagination using Visualforce and Apex Class

Apex Class :
public class accountPaginationCls
public Integer noOfRecordPerPage {get;set;}
public Integer selectedPage {get;set;}
public Map<Integer, List<Account >> mapAccounts{get;set;}
public List<selectoption> pagesOptions {get;set;}
public accountPaginationCls(ApexPages.StandardController controller) {
mapAccounts =new Map<Integer, List<Account >> ();
noOfRecordPerPage = 5;
selectedPage = 1;
List<Account> listAccounts = [select Name from Account order by Name asc];
pagesOptions = new List<SelectOption>();
integer total_no_of_pages = listAccounts.size()/noOfRecordPerPage;
if(math.mod(listAccounts.size(),noOfRecordPerPage) > 0){
total_no_of_pages = total_no_of_pages +1;
integer pageStartValue = 0;
integer pageEndValue = noOfRecordPerPage;
for(integer i = 0; i<total_no_of_pages ; i++){
integer counter = i+1;
pagesOptions.add(new SelectOption(counter+”,counter +”));
List<Account> AccountList = new List<Account>();
for(integer j = pageStartValue ; j< pageEndValue; j++){
catch(Exception e) {
pageStartValue = pageEndValue;
pageEndValue = noOfRecordPerPage*(i+2);
public void nextPage(){}

Visualforce Page :
<apex:page standardController=”Account” extensions=”accountPaginationCls” sidebar=”false” showHeader=”false”>
<apex:form id=”frm”>
<apex:pageBlock title=”Pagination” >
<apex:pageblockSection columns=”1″>
<apex:repeat value=”{!mapAccounts[selectedPage]}” var=”a”>
<apex:outputLabel value=”{!}”></apex:outputLabel>
<apex:selectList value=”{!selectedPage}” size=”1″ multiselect=”false”>
<apex:SelectOptions value=”{!pagesOptions}”></apex:SelectOptions>
<apex:actionSupport event=”onchange” action=”{!nextPage}” reRender=”frm” />

Demo :

Written by 

I am Sakthivel Madesh, a certified Salesforce Advanced Developer & Administrator working on Salesforce Technology since 2011. I am currently working in Techforce Services as Sr. Salesforce Developer. I have worked on multiple technologies Like PHP, Java, Salesforce, I am very passionate about Salesforce and to earn 15x Salesforce Certification. I love Trailhead for learning Salesforce Skill and Become a Trailhead 5x Ranger too. I Love to Learn & Share my Salesforce knowledge to Salesforce Learners/Beginner using My Blog and Salesforce Community.