Submitted by Monish on Tuesday, July 21, 2015 - 23:18.
Use Case :
Add pagination to display limited number of records in a VisualForce page.
Solution :
Query only limited set of records using the offset in SOQL.
Code :
Controller
Private Integer listSize=50; //listSize determines the number of records to display.
Private Integer accountCounter=0; // Determines how many records to be skipped
Private Integer accountTotalSize=0; // Stores the total number of records in the org
Public list<Account> allCompanyList = new list<Account>(); //Account list to display the current set of records
public PageReference accountList(){
if(allCompanyList !=null){
allCompanyList.clear();
}
if(accountTotalSize != 0){
accountTotalSize = [Select count() from Account ];
}
allCompanyList = [Select Id,Name from Account order by Name limit : listSize offset : accountCounter]; return null;}
//account navigation methods
//Diables the previous buttons if there is no record previous than current list
public Boolean getDisablePreviousAccount(){
if(accountCounter==0)
return true;
else
return false;
}
//Diables the next buttons if there is no record next to current list
public Boolean getDisableNextAccount(){
if((accountTotalSize <= listSize) || ((accountCounter + listSize) >= accountTotalSize))
return true;
else
return false;
}
//Displays the next set of records
public void nextAccount(){
accountCounter=accountCounter+listSize;
accountList();
}
//Displays the previous set of records
public void previousAccount(){
accountCounter=accountCounter-listSize;
accountList();
}
//Displays the last set of records irrespective of current position
public void nextLastAccount(){
if(math.mod(accountTotalSize, listSize) ==0){
accountCounter = ((accountTotalSize/listSize)-1)_listSize;
}
else if(math.mod(accountTotalSize, listSize) !=0) {
accountCounter = (accountTotalSize/listSize)_listSize;
} accountList();
}
//Displays the first set of records irrespective of current position
public void previousFirstAccount(){
accountCounter=0;
accountList();
}
VisualForce
<apex:outputPanel id="companys_navigation_panel"
rendered="{!allCompanyList != null && allCompanyList.size > 0}"
style="float:right;margin-right:-45px;">
<apex:commandButton value="<<" disabled="{!DisablePreviousAccount}"
action="{!previousFirstAccount}"
Rerender="companys_navigation_panel,companys_Info_Panel"
styleclass="bluerund">
</apex:commandButton>
<apex:commandButton value="<" disabled="{!DisablePreviousAccount}"
action="{!previousAccount}"
Rerender="companys_navigation_panel,companys_Info_Panel"
styleclass="bluerund">
</apex:commandButton>
<apex:commandButton value=">" disabled="{!DisableNextAccount}"
action="{!nextAccount}"
Rerender="companys_navigation_panel,companys_Info_Panel"
styleclass="bluerund">
</apex:commandButton>
<apex:commandButton value=">>" disabled="{!DisableNextAccount}"
action="{!nextLastAccount}"
Rerender="companys_navigation_panel,companys_Info_Panel"
styleclass="bluerund">
</apex:commandButton>
</apex:outputPanel>
Css :
.bluerund{
border-radius: 100% !important;
box-shadow: none !important;
border:1px solid #dbdbde !important;
background-image: none,-webkit-linear-gradient(top,rgba(255,255,255,.45) 0,rgba(255,255,255,0) 100%) !important;
background-image: none,-moz-linear-gradient(top,rgba(255,255,255,.45) 0,rgba(255,255,255,0) 100%) !important;
background-image: none,-o-linear-gradient(top,rgba(255,255,255,.45) 0,rgba(255,255,255,0) 100%) !important;
background-color: #f3f3f4;
color: #515967 !important;
background-image: none,linear-gradient(to bottom,rgba(255,255,255,.45) 0,rgba(255,255,255,0) 100%) !important;
width:22px;
height:22px;
margin-top:5px !important;
}body .btnDisabled{
opacity:0.5;
}
Output :
If u have added the css in your VF page then the output will be like this.
Limitation :
Offset only supports upto 2000 records..