Skip to content

Instantly share code, notes, and snippets.

@lewdlime
Last active December 19, 2018 16:07
Show Gist options
  • Save lewdlime/20ac0512ebc7ccf52691 to your computer and use it in GitHub Desktop.
Save lewdlime/20ac0512ebc7ccf52691 to your computer and use it in GitHub Desktop.
Salesforce Pagination Demo
global class CustomIterable implements Iterator<List<RecordWrapper>> {
// NOTE: 'Record' should be replaced with the API name of the object you want to have
// pagination for.
// The Iterator interface is used for pagination
List<RecordWrapper>InnerList {get; set;}
// Holds all the values
List<RecordWrapper>ListRequested {get; set;}
// Holds the value to be displayed on the Page
Integer i {get; set;}
public Integer setPageSize {get; set;}
// Constructor
public CustomIterable(List<RecordWrapper>lstAccWr) {
InnerList = new List<RecordWrapper>();
ListRequested = new List<RecordWrapper>();
InnerList = lstAccWr;
setPageSize = 10;
i = 0;
}
global boolean hasNext() {
if (i >= InnerList.size()) {
return false;
} else {
return true;
}
}
global boolean hasPrevious() {
if (i <= setPageSize) {
return false;
} else {
return true;
}
}
global List<RecordWrapper> next() {
ListRequested = new List<RecordWrapper>();
Integer startNumber;
Integer size = InnerList.size();
if (hasNext()) {
if (size <= (i + setPageSize)) {
startNumber = i;
i = size;
} else {
i = (i + setPageSize);
startNumber = (i - setPageSize);
}
for (Integer start = startNumber; start < i; start++) {
ListRequested.add(InnerList[start]);
}
}
return ListRequested;
}
global List<RecordWrapper> previous() {
ListRequested = new List<RecordWrapper>();
Integer size = InnerList.size();
if (i == size) {
if (math.mod(size, setPageSize) > 0) {
i = size - math.mod(size, setPageSize);
} else {
i = (size - setPageSize);
}
} else {
i = (i - setPageSize);
}
for(Integer start = (i - setPageSize); start < i; ++start) {
ListRequested.add(InnerList[start]);
}
return ListRequested;
}
}
public without sharing class CustomPaginationDemo {
// NOTE: 'Record' should be replaced with the API name of the object you want to have
// pagination for.
public List<ContactWrapper> lstWrapper {get; set;}
// Wrapper list to hold all the values
public List<ContactWrapper> lstSetController {get; set;}
// Wrapper list to hold the values which are displayed on UI
public static String compareField {get; set;}
// Holds the value of field on which the list needs to be sorted
public static String sortOrder {get; set;}
// Sorting order (ASC/DESC)
CustomIterable obj;
// Instance of Iterator
// Constructor
public CustomPaginationDemo() {
lstWrapper = new List<RecordWrapper>();
lstSetController = new List<RecordWrapper>();
List<Record> lstRecord = [SELECT Id, Name FROM Record LIMIT 20];
// Generating the list of records
for (Record r : lstRecord) {
lstWrapper.add(new RecordWrapper(rec, false));
// Adding the values to wrapper
}
obj = new CustomIterable(lstWrapper);
//Initializing the iterator
if(lstWrapper.size() > 5) {
obj.setPageSize = 5;
} else {
obj.setPageSize = lstWrapper.size() ;
}
// Table size
next();
}
public Boolean hasNext {
get {
return obj.hasNext();
}
set;
}
public Boolean hasPrevious {
get {
return obj.hasPrevious();
}
set;
}
public void next() {
lstSetController = obj.next();
}
public void previous() {
lstSetController = obj.previous();
}
// Method used to sort
public void sortWrpLst() {
ContactWrapper.compareField = compareField;
// Assigning values to ContactWrapper for Sorting
ContactWrapper.sortOrder = sortOrder;
// Assigning values to ContactWrapper for Sorting
lstWrapper.sort();
// Sorting th wrapper list
obj = new CustomIterable(lstWrapper);
obj.setPageSize = 5;
next();
}
}
<apex:page controller="CustomPaginationDemo" showHeader="false" sidebar="false">
<!-- NOTE: 'Record' should be replaced with the API name of the object you want to have pagination for. -->
<apex:form>
<apex:pageBlock id="ThePage">
<apex:pageBlockSection columns="1">
<apex:pageBlockTable value="{!lstSetController}" var="obj">
<apex:column headerValue="Select"><apex:inputCheckbox value="{!obj.isSelected}"/></apex:column>
<apex:column >
<apex:facet name="header">
<apex:commandLink value="Name {!IF(compareField=='Name',IF(sortOrder='asc','▼','▲'),")}" action="{!sortWrpLst}" reRender="ThePage">
<apex:param name="compareField" value="Name" assignTo="{!compareField}" />
<apex:param name="orderType" value="{!IF(sortOrder='asc', 'desc', 'asc')}" assignTo="{!sortOrder}" />
</apex:commandLink>
</apex:facet>
<apex:outputField value="{!obj.cont.Name}"/>
</apex:column>
</apex:pageBlockTable>
<apex:outputPanel >
<apex:commandButton value="Previous" action="{!previous}" disabled="{!!hasPrevious}" reRender="ThePage" />
<apex:commandButton value="Next" action="{!next}" disabled="{!!hasNext}" reRender="ThePage" />
</apex:outputPanel>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
global class RecordWrapper implements Comparable {
// NOTE: 'Record' should be replaced with the API name of the object you want to have
// pagination for.
// The compareble interface is used for sorting the Wrapper List
// Wrapper Structure Strats {{{
// this structure can be customized as per requirement
public Boolean isSelected {get; set;}
public Record rec {get; set;}
// Wrapper Structure Ends }}}
public static String compareField {get; set;}
// Variable to hold the value of Field / Column for Sorting
public static String sortOrder {get; set;}
// Variable to hold the order for Sorting (ASC/DESC)
// Constructor
public RecordWrapper(Record rec, Boolean isSelected) {
this.rec = rec;
this.isSelected = isSelected;
}
// Compareto is a required method that is used when we implement comparable interface.
public Integer compareTo(Object vRec) {
RecordWrapper recToCompare = (RecordWrapper) vRec;
if ((String) rec.get(compareField) > (String) recToCompare.rec.get(compareField)) {
return sortOrder.equals('ASC') ? 1 : 0;
} else {
return sortOrder.equals('ASC') ? 0 : 1;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment