Pagination in Visualforce using StandardSetController

The StandardSetController is a controller class that enables you to create a Visualforce page to showcase a list of records. It has built-in capabilities to handle pagination, thus simplifying displaying extensive data sets on a Visualforce page.

Here are some key features and functionalities of StandardSetController:

Pagination: StandardSetController automatically handles pagination, allowing you to display a subset of records on each page. It provides methods for navigating between pages, such as first(), previous(), next(), and last().

Sorting and Filtering: StandardSetController allows you to specify sorting and filtering criteria for the records displayed on the Visualforce page. You can define sorting order and filter conditions using the setSortOrder() and setFilterId() methods.

Record Navigation: You can easily navigate to the first, last, previous, or next page of records using methods provided by StandardSetController. This simplifies the implementation of navigation controls on the Visualforce page.

Data Access Control: StandardSetController respects the organization's sharing settings and field-level security. It ensures that users can only access records they have permission to view based on their profile and record-level security settings.

Integration with Standard Controllers: StandardSetController integrates seamlessly with standard controllers and extensions, allowing you to leverage the standard controller's functionality while benefiting from the pagination and navigation features provided by StandardSetController.

Apex Methods for Querying Records: StandardSetController provides methods such as getRecords() and getSelected() to retrieve the records currently displayed on the Visualforce page and the user has selected, respectively.

Here's a simple example of how to use StandardSetController for pagination in a Visualforce page:

Apex Class

Public class PaginationController { 

private ApexPages.StandardSetController setCon;

public PaginationController() {
    setCon = new ApexPages.StandardSetController(Database.getQueryLocator([SELECT Id, Name FROM Account ORDER BY Name]));
    setCon.setPageSize; // set page size to 10 records
}

public ApexPages.StandardSetController getSetCon() {
    return setCon;
}
 // Method to get the current list of records
public List<Account> getAccounts() {
    return (List<Account>)setCon.getRecords();
}

}

Visualforce Page

<apex:page controller="PaginationController"> apex:formapex:pageBlock<apex:pageBlockTable value="{!Accounts}" var="acc"> <apex:column value="{!acc.Name}"/> </apex:pageBlockTable>

        <apex:panelGrid columns="4">
            <apex:commandButton value="First" action="{!setCon.first}" disabled="{!NOT setCon.hasPrevious}" reRender="table"/>
            <apex:commandButton value="Previous" action="{!setCon.previous}" disabled="{!NOT setCon.hasPrevious}" reRender="table"/>
            <!-- Display the current page position in the set -->
            <apex:outputText>{!setCon.pageNumber} of {!setCon.resultSize/setCon.pageSize}</apex:outputText>
            <apex:commandButton value="Next" action="{!setCon.next}" disabled="{!NOT setCon.hasNext}" reRender="table"/>
            <apex:commandButton value="Last" action="{!setCon.last}" disabled="{!NOT setCon.hasNext}" reRender="table"/>
        </apex:panelGrid>
    </apex:pageBlock>
</apex:form>
</apex:page>









Comments

Popular posts from this blog

Introduction to Salesforce Agent Script: Build Predictable AI Agents

Anti-Patterns in Salesforce Agentforce: What to Avoid When Building AI Agents

MCP vs. Traditional APIs: Choosing the Right Integration Approach