This article is useful whenever you have an issue to split full page content. Pagination concept is a help to avoid this kind of issue. Pagination is nothing but some time the full page has long content so we could not able to view and had a bitterness to see. In that situation, this article is a support to you.
Here we provide an example showing, the full page is divide which is showing how you set page result size and the starting point of the record at the run time.
Try the below code –
Visualforce page –
<apex:page controller=”AccountDetails” showHeader=”false” sidebar=”false”>
<apex:sectionHeader title=”Pagination Sample”/>
<apex:pageBlock >
<apex:form >
<apex:outputLabel >Starting Record</apex:outputLabel>
<apex:inputText value=”{!counter}” style=”margin-left:100px;”>
<apex:actionSupport event=”OnClick” reRender=”myPanel,myButtons” action=”{!getAccounts}”/>
</apex:inputText>
</apex:form>
<apex:form >
<apex:outputLabel style=”margin-top:10px;”>Records Limit</apex:outputLabel>
<apex:inputText value=”{!list_size}” style=”margin-left:110px;margin-top:10px;”>
<apex:actionSupport event=”onClick” reRender=”myPanel,myButtons” action=”{!getAccounts}”/>
</apex:inputText>
</apex:form>
</apex:pageBlock>
<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″>
</apex:pageBlockSection>
<apex:pageBlockSection title=”Result” collapsible=”false”>
<apex:outputPanel id=”myPanel”>
<apex:pageMessages id=”theMessages” /><p style=”color:blue;”>Total List Size: {!total_size}</p><br/>
<apex:pageBlockTable value=”{!accounts}” var=”acc” align=”center”>
<apex:column value=”{!acc.AccountNumber}” />
<apex:column value=”{!acc.Name}” />
<apex:facet name=”footer”>Page # {!pageNumber} of {!totalPages}</apex:facet>
</apex:pageBlockTable>
</apex:outputPanel>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:page>
Apex Class :-
public with sharing class AccountDetails{
public integer counter{set;get;} //keeps track of the offset
public integer list_size{set;get;} //sets the page size or number of rows
public integer total_size; //used to show user the total size of the list
public AccountDetails() {
counter=0; // starting position of a record
list_size=10; // set default size per page
total_size = [select count() from Account]; //set the total size in the constructor
}
public Account[] getAccounts() {
try {
Account[] accounts = [select Id, Name, AccountNumber,Type from Account order by Name limit :list_size offset :counter];
return accounts;
} catch (QueryException e) {
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 {
else {
return (total_size/list_size);
}
}
}
Result :-
Enter number of records you want to show in the each page,
Enter starting position of the record,
We are the ISV Partners and Please reach us for custom development => www.merfantz.com
———————— We hope it will help you —————————