Activity › Forums › Salesforce® Discussions › How to build custom pagination in salesforce?
-
How to build custom pagination in salesforce?
Posted by Amresh Tiwari on March 28, 2018 at 2:49 PMDescription: The pagination limit size will be 30.
Parul replied 7 years, 8 months ago 5 Members · 6 Replies -
6 Replies
- [adinserter block='9']
-
Hi Amresh,
With the help following code, you can build custom pagination using the custom controller in Visualforce Page:
Visualforce Page:
<apex:page controller=”AccountMultipleSearchWithPagenationCLS” action=”{!searchAcc}” >
<script type=”text/javascript”>
window.onload=function() {
// document.getElementById(“{!$Component.thePb.thepbs.accName}”).focus();
}
</script>
<apex:form >
<apex:pageBlock id=”thePb” title=”Account Details To Search”>
<apex:pageblockSection id=”thepbs”>
<apex:inputField value=”{!acc.Created_From_Date__c}” />
<apex:inputField value=”{!acc.Created_To_Date__c}”/>
<apex:inputField value=”{!acc.Name}” required=”false” id=”accName”/>
<apex:inputfield value=”{!acc.accountNumber}”/>
</apex:pageblockSection>
<apex:pageblockButtons location=”bottom”>
<apex:commandButton value=”Search” action=”{!searchAcc}” />
</apex:pageblockButtons>
</apex:pageBlock><apex:pageBlock title=”Account Details” id=”noRec” rendered=”{! IF( accountList != null && accountList.size ==0 , true, false)}” >
<apex:outputPanel >
<h1>No Records Found </h1>
</apex:outputPanel>
</apex:pageBlock>
<apex:pageBlock title=”Account Details” id=”details” rendered=”{! IF( accountList != null && accountList.size >0, true, false)}” ><apex:pageBlockTable value=”{!accountList}” var=”a”>
<apex:column headerValue=”Account Name”>
<apex:outputLink target=”_blank” value=”/{!a.id}”>{!a.Name}</apex:outputLink>
</apex:column>
<apex:column value=”{!a.accountNumber}” headerValue=”Account Number”/>
<apex:column value=”{!a.Industry}” headerValue=”Industry”/>
<apex:column value=”{!a.AnnualRevenue}” headerValue=”Annual Revenue”/>
<apex:column value=”{!a.Phone}” headerValue=”Phone”/>
<apex:column value=”{!a.website}” headerValue=”Web”/>
</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>Controller Class:
public with sharing class AccountMultipleSearchWithPagenationCLS {
public Account acc{get;set;}
public List<Account> accountList {get;set;}
// create a list of strings to hold the conditions
List<string> conditions = new List<string>();
private integer totalRecs = 0;
private integer OffsetSize = 0;
private integer LimitSize= 10;public AccountMultipleSearchWithPagenationCLS(){
system.debug(‘==>AccountMultipleSearchWithPagenationCLS is calling==>’);
acc = new Account();
//accountList = new List<Account>();
}public void searchAcc(){
totalRecs = 0;
OffsetSize = 0;
if(accountList !=null && accountList.size()>0){
accountList=null;
}
searchAccounts ();
conditions.clear();
}
public Void searchAccounts(){System.debug(‘Total Records is ==>’+totalRecs);
System.debug(‘OffsetSize is ==>’+OffsetSize);if(accountList != null && !accountList.isEmpty()){
accountList.clear();
}
String strQuery =’SELECT Id,Name,AccountNumber,CreatedDate,Phone,Website,Industry,AnnualRevenue From Account’;
if(acc.Created_From_Date__c !=null){
String fromDate = acc.Created_From_Date__c+”;
fromDate = fromDate.split(‘ ‘,0)[0]+’T00:00:00.000Z’;
conditions.add(‘CreatedDate >=’+fromDate);
}if(acc.Created_To_Date__c !=null){
String toDate = acc.Created_To_Date__c+”;
toDate = toDate.split(‘ ‘,0)[0]+’T23:59:59.000Z’;
conditions.add(‘createdDate <=’+toDate);
}if(acc.Name !=null && acc.Name !=”){
conditions.add(‘Name Like \’%’ +acc.Name +’%\’ ‘);
}
if(acc.AccountNumber !=null && acc.AccountNumber !=”){
conditions.add(‘AccountNumber Like\’%’ +acc.AccountNumber +’%\’ ‘);
}if (conditions.size() > 0) {
strQuery += ‘ WHERE ‘ + conditions[0];
for (Integer i = 1; i < conditions.size(); i++)
strQuery += ‘ AND ‘ + conditions[i];
}
if(totalRecs !=null && totalRecs ==0){
List<Account> accTemp = Database.query(strQuery);
totalRecs = (accTemp !=null &&accTemp.size()>0)?accTemp.size():0;
}system.debug(‘strQuery ==>’+strQuery );
// add sort and limits at the end
strQuery += ‘ ORDER BY Name ASC, CreatedDate DESC LIMIT :LimitSize OFFSET :OffsetSize’;accountList =Database.query(strQuery);
//conditions.clear();
//return accountList.size();
}public void FirstPage()
{
OffsetSize = 0;
searchAccounts();
}
public void previous()
{
OffsetSize = (OffsetSize-LimitSize);
searchAccounts();
}
public void next()
{
OffsetSize = OffsetSize + LimitSize;
searchAccounts();
}
public void LastPage()
{
OffsetSize = totalrecs – math.mod(totalRecs,LimitSize);
searchAccounts();
}
public boolean getprev()
{if(OffsetSize == 0){
return true;
}
else {return false;
}
}
public boolean getnxt()
{
if((OffsetSize + LimitSize) > totalRecs){return true;
}
else {return false;
}
}
} -
<apex:page standardcontroller=”Account” recordsetvar=”accounts”>
<apex:pageblock title=”Account List”>
<apex:form>
<apex:pageblocksection>
<apex:datatable value=”{!accounts}” var=”a”>
<apex:column value=”{!a.name}”></apex:column>
</apex:datatable>
</apex:pageblocksection>
<apex:panelgrid columns=”2″>
<apex:commandlink action=”{!previous}”>Previous</apex:commandlink>
<apex:commandlink action=”{!next}”>Next</apex:commandlink>
</apex:panelgrid>
</apex:form>
</apex:pageblock>
</apex:page> -
Here is the code, you can use
Apex Class
public class paginationStandard {
public ApexPages.StandardSetController setCon {
get {
if(setCon == null) {
setCon = new ApexPages.StandardSetController(Database.getQueryLocator(
[select name,closedate ,type,leadsource,stageName, amount from Opportunity]));
}
return setCon;
}
set;
}
public List<Opportunity> getOpportunities() {
setCon.setpagesize(10);
return (List<Opportunity>) setCon.getRecords();
}
}
Visualforce Page
<apex:page controller=”paginationStandard”>
<apex:form >
<apex:pageBlock >
<apex:pageBlockTable value=”{!Opportunities}” var=”o”>
<apex:column value=”{!o.name}”/>
<apex:column value=”{!o.closedate}”/>
<apex:column value=”{!o.stageName}”/>
<apex:column value=”{!o.Amount}”/>
<apex:column value=”{!o.Type}”/>
<apex:column value=”{!o.LeadSource}”/>
</apex:pageBlockTable>
<apex:commandButton rendered=”{!setCon.hasPrevious}” value=”Previous” action=”{!setCon.previous}”/>
<apex:commandButton rendered=”{!setCon.hasNext}” value=”Next” action=”{!setCon.next}”/>
<apex:commandButton rendered=”{!setCon.hasPrevious}” value=”first” action=”{!setCon.first}”/>
<apex:commandButton rendered=”{!setCon.hasNext}” value=”Last” action=”{!setCon.last}”/>
<apex:outputText rendered=”{!(setCon.pageNumber * setCon.pageSize) < setCon.ResultSize}” value=”{!setCon.pageNumber * setCon.pageSize} Of {!setCon.ResultSize}”></apex:outputText>
<apex:outputText rendered=”{!(setCon.pageNumber * setCon.pageSize) >= setCon.ResultSize}” value=”{!setCon.ResultSize} Of {!setCon.ResultSize}”></apex:outputText>….
</apex:pageBlock>
</apex:form>
</apex:page>
-
Hi,
Please take reference from the following code to understand pagination
<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>
—————————————————————————————————————————————————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;
}
}
Log In to reply.