How to implement Paging in Apex Salesforce

H

Put this code in your class & replace your query for myRequestsSetCon:

public ApexPages.StandardSetController myRequestsSetCon {
get {
if(myRequestsSetCon == null) {
myRequestsSetCon = new ApexPages.StandardSetController(Database.getQueryLocator([
select Id, Name, Merchant_Name__c, Requested_Pricing_Change_Effective_Date__c, Status__c, CreatedDate
from Global_Merchant_Pricing_Change__c
where Status__c != ‘Draft’ order by CreatedDate desc
]));

//for(Global_Merchant_Pricing_Change__c c: myRequestsSetCon)
// c.CreatedDate = c.CreatedDate;

// sets the number of records in each page set
myRequestsSetCon.setPageSize(10);
}
return myRequestsSetCon;
}
set;
}

// indicates whether there are more records after the current page set.
public Boolean hasNext {
get {
return myRequestsSetCon.getHasNext();
}
set;
}

// indicates whether there are more records before the current page set.
public Boolean hasPrevious {
get {
return myRequestsSetCon.getHasPrevious();
}
set;
}

// returns the page number of the current page set
public Integer pageNumber {
get {
return myRequestsSetCon.getPageNumber();
}
set;
}

// returns the total page numbers of set
public Integer totalPages {
get {
Integer currPageNumber = myRequestsSetCon.getPageNumber();
myRequestsSetCon.last();
Integer totalPages = myRequestsSetCon.getPageNumber();
myRequestsSetCon.setpageNumber(currPageNumber);
return totalPages;
}
set;
}

// returns the first page of records
public void first() {
myRequestsSetCon.first();
}

// returns the last page of records
public void last() {
myRequestsSetCon.last();
}

// returns the previous page of records
public void previous() {
myRequestsSetCon.previous();
}

// returns the next page of records
public void next() {
myRequestsSetCon.next();
}

This is corresponding Visualforce page code:

<apex:form >
<apex:outputPanel id=”RecentRequestsSection”>
<apex:pageBlock rendered=”{!ISNULL(SelectedRequest)}” mode=”maindetail”>
<apex:pageblockTable var=”r” value=”{!myRequests}” width=”100%” style=”margin-top:10px;”>
<apex:column headerValue=”Request Number” style=”text-align:left”>
<apex:outputLink value=”/apex/GNSWeb_GMP_View_Request?id={!r.Id}”>{!r.Name}</apex:outputLink>
</apex:column>
<apex:column value=”{!r.Merchant_Name__c}” headerValue=”Merchant”/>
<apex:column headerValue=”Effective Date”>
<apex:outputText value=”{0,date,MMMM’ ‘d’ ‘yyyy}”>
<apex:param value=”{!r.Requested_Pricing_Change_Effective_Date__c}” />
</apex:outputText>
</apex:column>
<apex:column value=”{!r.Status__c}” headerValue=”Status”/>
<apex:column headerValue=”Created Date”>
<apex:outputText value=”{0,date,MMMM’ ‘d’ ‘yyyy}”>
<apex:param value=”{!r.CreatedDate}” />
</apex:outputText>
</apex:column>
</apex:pageblockTable>
</apex:pageBlock>

<apex:panelGrid columns=”7″ rendered=”{!ISNULL(SelectedRequest)}”>
<apex:commandLink action=”{!first}” reRender=”RecentRequestsSection”>First</apex:commandlink>
<apex:commandLink action=”{!previous}” reRender=”RecentRequestsSection” rendered=”{!hasPrevious}”>Previous</apex:commandlink>
<apex:outputText value=”Previous” rendered=”{!NOT(hasPrevious)}”/>
<apex:commandLink action=”{!next}” reRender=”RecentRequestsSection” rendered=”{!hasNext}”>Next</apex:commandlink>
<apex:outputText value=”Next” rendered=”{!NOT(hasNext)}”/>
<apex:commandLink action=”{!last}” reRender=”RecentRequestsSection”>Last</apex:commandlink>
<apex:outputLabel value=”[Page {!pageNumber} of {!totalPages}]” />
</apex:panelGrid>
</apex:outputPanel>
</apex:form>

About the author

Nishant Bamb
By Nishant Bamb

Category