Please find the sample code below with a similar requirement.
Visual Force Page:
<apex:page title="Salesforce SOQL Offset Example Using Visualforce" controller="soql_offset_example" showHeader="false" sidebar="false" readOnly="true" cache="false">
<apex:sectionHeader subtitle="SOQL Offset Example" title="Square Root Table"/>
<apex:pageBlock >
<apex:pageBlockButtons location="top" >
<apex:outputPanel id="myButtons">
<apex:form >
<apex:commandButton action="{!Beginning}" title="Beginning" value="<<" disabled="{!disablePrevious}" reRender="myPanel,myButtons"/>
<apex:commandButton action="{!Previous}" title="Previous" value="<" disabled="{!disablePrevious}" reRender="myPanel,myButtons"/>
<apex:commandButton action="{!Next}" title="Next" value=">" disabled="{!disableNext}" reRender="myPanel,myButtons"/>
<apex:commandButton action="{!End}" title="End" value=">>" disabled="{!disableNext}" reRender="myPanel,myButtons"/>
</apex:form>
</apex:outputPanel>
</apex:pageBlockButtons>
<apex:pageBlockSection columns="1">
<p>This Visualforce Page demonstrates the usage of the "OFFSET" clause in a SOQL SELECT statement. You can "Paginate" through
a large result set quickly. This example allows a user to paginate up and down through a result set 20 records at a time.
Using the "VCR" buttons above, you can also quickly skip to the end or the beginning of the set.</p>
<p>The accompanying code can be viewed here: <a href="http://blog.redpointsolutions.com/bid/182738/Add-Pagination-to-your-Visualforce-Pages-using-the-SOQL-OFFSET-Clause" target="_blank">RedPoint Solutions BLOG</a></p>
</apex:pageBlockSection>
<apex:pageBlockSection title="Numbers and their Square Roots (Total List Size: {!total_size})" collapsible="false">
<apex:outputPanel id="myPanel">
<apex:pageMessages id="theMessages" />
<apex:pageBlockTable value="{!accounts}" var="acc" align="center">
<apex:column value="{!acc.Name}" />
<apex:column value="{!acc.AccountNumber}" />
<apex:facet name="footer">Showing Page # {!pageNumber} of {!totalPages}</apex:facet>
</apex:pageBlockTable>
</apex:outputPanel>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:page>
Apex Controller:
public with sharing class soql_offset_example {
private integer counter=0; //keeps track of the offset
private integer list_size=20; //sets the page size or number of rows
public integer total_size; //used to show user the total size of the list
public soql_offset_example() {
total_size = [select count() from Account]; //set the total size in the constructor
}
public Account[] getAccounts() {
try {
Account[] accounts = [select Id, Name, AccountNumber from Account order by Name limit :list_size offset :counter];
return accounts;
} catch (QueryException e) {
ApexPages.addMessages(e);
return null;
}
}
public PageReference Beginning() { //user clicked beginning
counter = 0;
return null;
}
public PageReference Previous() { //user clicked previous button
counter -= list_size;
return null;
}
public PageReference Next() { //user clicked next button
counter += list_size;
return null;
}
public PageReference End() { //user clicked end
counter = total_size - math.mod(total_size, list_size);
return null;
}
public Boolean getDisablePrevious() {
//this will disable the previous and beginning buttons
if (counter>0) return false; else return true;
}
public Boolean getDisableNext() { //this will disable the next and end buttons
if (counter + list_size < total_size) return false; else return true;
}
public Integer getTotal_size() {
return total_size;
}
public Integer getPageNumber() {
return counter/list_size + 1;
}
public Integer getTotalPages() {
if (math.mod(total_size, list_size) > 0) {
return total_size/list_size + 1;
} else {
return (total_size/list_size);
}
}
}
Please find the sample code below with a similar requirement.
Visual Force Page: Apex Controller: I also tested the above and below is the output:
Hope this helps.
Mark this as solved if it's resolved.
Thanks,
Nagendra