This is an example for offset usage in pagination.The controller looks like this
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 Number__c]; //set the total size in the constructor
}
public Number__c[] getNumbers() {
try {
Number__c[] numbers = [select Number__c, Square_Root__c
from Number__c
order by Number__c
limit :list_size
offset :counter];
return numbers;
} 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);
}
}
}
and the page looks like this
<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="{!numbers}" var="n" align="center"> <apex:column value="{!n.Number__c}" /> <apex:column value="{!n.Square_Root__c}" /> <apex:facet name="footer">Showing Page # {!pageNumber} of {!totalPages}</apex:facet> </apex:pageBlockTable> </apex:outputPanel> </apex:pageBlockSection> </apex:pageBlock> </apex:page>

No comments:
Post a Comment