RecordSet – Setting the views, Page Size/Show More. - El Toro - Find articles about Visualforce, Apex, Force.com and Salesforce in general

Print Preview

RecordSet – Setting the views, Page Size/Show More.

Building Visualforce pages that can display multiple records of the same type are easy to build with the help of RecordSets which leverage Apex StandardSetControllers. The only thing this requires is to make a simple change in the <apex:page> tag like this:

<apex:page standardController="Account" recordSetVar="accounts" >

If you make this simple change, the records viewed are the ones selected on the current list view. As you know, when you click on an object's tab and click on the [Go] button, the previously selected listview will be used but this can be changed on a drop down list. The following visualforce a page with something similar.

This is the Visualforce code:

<apex:page standardController="Account" recordSetVar="accounts" tabstyle="account" extensions="listView">
    <apex:form >
        <apex:sectionHeader title="Listview Sample" subtitle="Accounts"/>
        <apex:pageBlock id="list">
            <apex:pageBlockButtons location="top">
                <apex:outputLabel value="View: "/>
                <apex:selectList value="{!filterId}" size="1">
                    <apex:actionSupport event="onchange" rerender="list"/>
                    <apex:selectOptions value="{!listviewoptions}"/>
                </apex:selectList>
            </apex:pageBlockButtons>
            <apex:pageBlockTable value="{!accounts}" var="acc">
                <apex:column >
                    <a href="/{!acc.ID}" target="ShowRecord"><apex:outputText value="{!acc.name}"/></a>
                </apex:column>
            </apex:pageBlockTable><br/>
            <apex:commandLink value="Show More ({!ResultSize}) >>" action="{!ShowAll}" rendered="{!ResultSize>PageSize}"/>
        </apex:pageBlock>
    </apex:form>
</apex:page>

And this is the Apex code:

public class listView {
    ApexPages.StandardSetController controller;
    public listView(ApexPages.StandardSetController controller) {
        this.controller = controller;
        controller.setPageSize(5);
    }
    public pageReference ShowAll() {
        controller.setPageSize(controller.getResultSize());
        return null;
    }
}

As you can see in the Visualforce code, we are using some merged fields that are not defined in the Apex extension, like {!filterId} and {!listViewOptions}... So where are those defined?

We are extending a StandardSetController which contains those get/set methods, for more information on these controllers, you can view these pages:

comments powered by Disqus

© El Toro . IT @ 2013
Andrés Pérez