Skip to content

Instantly share code, notes, and snippets.

@brianmfear
Created September 5, 2017 16:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save brianmfear/6897fa70764a8ddc5ef067b8fc174d34 to your computer and use it in GitHub Desktop.
Save brianmfear/6897fa70764a8ddc5ef067b8fc174d34 to your computer and use it in GitHub Desktop.
public class ServerSide50KPagination {
Id[] recordIds;
public Integer maxPage { get; set; }
public Integer pageNumber { get; set; }
public Integer pageSize { get; set; }
public Account[] records { get; set; }
public ServerSide50KPagination() {
recordIds = new Id[0];
for(Account record: [SELECT Id FROM Account ORDER BY Name]) {
recordIds.add(record.Id);
}
pageSize = 10;
maxPage = (recordIds.size()+(pageSize-1)) / pageSize;
}
public void firstPage() {
pageNumber = 1;
loadPage();
}
public void previousPage() {
pageNumber = Math.max(1, pageNumber - 1);
loadPage();
}
public void nextPage() {
pageNumber = Math.min(maxPage, pageNumber + 1);
loadPage();
}
public void lastPage() {
pageNumber = maxPage;
loadPage();
}
public void loadPage() {
Id[] currentPage = new Id[0];
records = new Account[0];
for(Integer index = (pageNumber-1)*pageSize,
size = Math.min(pageNumber*pageSize, recordIds.size());
index < size; index++) {
currentPage.add(recordIds[index]);
}
Map<Id, Account> accounts = new Map<Id, Account>(
[SELECT Name, Industry FROM Account WHERE Id = :currentPage]
);
for(Id recordId: currentPage) {
records.add(accounts.get(recordId));
}
}
public void changePageSize() {
maxPage = (recordIds.size()+(pageSize-1)) / pageSize;
firstPage();
}
public void refreshView() {
loadPage();
}
}
<apex:page controller="ServerSide50KPagination">
<apex:form id="form">
<apex:actionStatus id="status" startText="Working..." />
<apex:pageBlock rendered="{!pageNumber!=null}">
<apex:pageBlockButtons>
<apex:commandButton status="status" reRender="form" action="{!firstPage}" value="First"/>
<apex:commandButton status="status" reRender="form" action="{!previousPage}" value="Previous"/>
{!pageNumber} / {!maxPage}
<apex:commandButton status="status" reRender="form" action="{!nextPage}" value="Next"/>
<apex:commandButton status="status" reRender="form" action="{!lastPage}" value="Last"/>
Page Size:
<apex:selectList value="{!pageSize}" size="1" multiselect="false">
<apex:selectOption itemValue="10" itemLabel="10" />
<apex:selectOption itemValue="25" itemLabel="25" />
<apex:selectOption itemValue="50" itemLabel="50" />
<apex:selectOption itemValue="100" itemLabel="100" />
<apex:actionSupport status="status" action="{!changePageSize}" reRender="form" event="onchange" />
</apex:selectList>
</apex:pageBlockButtons>
<apex:pageBlockTable value="{!records}" var="acct">
<apex:column value="{!acct.Name}" />
<apex:column value="{!acct.Industry}" />
</apex:pageBlockTable>
</apex:pageBlock>
<apex:actionFunction status="status" name="refreshPage" action="{!firstPage}" rendered="{!pageNumber=null}" reRender="form"/>
</apex:form>
<script>
if(refreshPage) {
refreshPage();
}
</script>
</apex:page>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment