Visualforce Pagination in Winter ’09

I recently signed up for the Winter ’09 pre-release trial (details here, and also see Nick’s post), so I started playing around with all the new goodies that are coming down the line (while waiting for a flight to start my holiday!).

One feature that caught my eye was the new pagination support in Visualforce. It’s so darn simple to use.

Check out the following image, which shows a list of account names, with “next” and “previous” links.

pagination.jpg

Here’s the code (straight from the docs):

<apex:page standardController="Account" recordSetvar="accounts">
<apex:pageBlock title="ViewingAccounts">
<apex:form id="theForm">
<apex:pageBlockSection>
<apex:dataList var="a" value="{!accounts}" type="1">
{!a.name}
</apex:dataList>
</apex:pageBlockSection>
<apex:panelGrid columns="2">
<apex:commandLink action="{!previous}">Previous</apex:commandlink>
<apex:commandLink action="{!next}">Next</apex:commandlink>
</apex:panelGrid>
</apex:form>
</apex:pageBlock>
</apex:page>

I’ve highlighted the new bits:

  • The recordSetvar attribution on the page component. This associates the page with a new type of controller, the standard list controller. The attribute also indicates the variable name of the record collection. So for example, in this case it’s set to “accounts”, which is what the dataList component iterates over.
  • New methods, previous() and next()

You can easily control the number of records displayed in each page (it defaults to 25), by writing an extension:

public class tenPageSizeExt {
public tenPageSizeExt(ApexPages.StandardSetControllercontroller){
controller.setPageSize(10);
}
}

As you can see, this simply calls the (new) setPageSize() method on the controller.

Have fun, and sign up for the Winter ’09 webinar for an overview of all new features!

tagged , Bookmark the permalink. Trackbacks are closed, but you can post a comment.
  • http://www.chiragmehta.info/ Chirag Mehta

    Hi, I was wondering lack of pagination in VisualForce.
    Even to extent i developed complete custom code to perform pagination in a VisualForce page. Now when i am done with development Salesforce came out with pagination code in few liners -:)
    Anyhow simplicity means more for me which Salesforce delivered. Thanks Saleforce

  • Nitin

    Hi,
    I am news to Visual force, today when I was playing with the above Visual Force code, I was getting
    Unsupported attribute recordsetvar in at line 1 column 1
    Do you have any idea why I am getting this?
    Thanks,
    Nitin

  • http://developer.force.com/ Jon Mountjoy

    Hi Nitin
    Are you using the Winter ’09 pre-release trial, as I indicate above? (See links above on how to get an account).
    This is new functionality only available in Winter ’09 pre-release at the moment – so if you try this on a standard developer edition account it won’t work (yet).
    Regards,
    Jon

  • Gareth Davies

    This looks useful for VF pages that use standard controllers. What is the best way to achieve the same pagination in pages that use custom controllers?
    Regards
    Gareth

  • http://developer.force.com/ Jon Mountjoy

    Hi Gareth
    Check out the Visualforce documentation in the “Custom Controllers” section under “Building a Custom List Controller”. It seems that it is possible (I’ve yet to do it).
    In particular, it seems you implement a method something like:
    public ApexPages.StandardSetController setCon{) {…}
    Hope that helps. Leave an update if you get that working!

  • http://profile.typekey.com/neutronron/ Ron Wild

    I tried to implement a custom list controller based on the example in the visualforce developers guide. That didn’t work, so I tried the example verbatim – still no luck. The compiler complains about the field references: “Unknown Property ‘SObject.name’” Has anyone gotten this to work?

  • http://salesforce.com/developer Andrew Waite

    Sorry about that Ron. The documentation was wrong. You need to cast your records collection to the appropriate type in your custom controller, you can’t bind directly to the records collection from the StandardSetController instance as it returns the generic SObject.
    I’ve tried pasting in an example but the code is being escaped/munged to the point where it’s more confusing than helpful.
    See this thread for more information:
    http://community.salesforce.com/sforce/board/message?board.id=Visualforce&message.id=5702
    The docs will be updated shortly.

  • Gareth Davies

    I’ve been able to get the pagination to work with a custom controller, however there seem to be some limitations that restrict its use that i wanted to check to make sure that i am correct.
    Firstly, pagination only works if you instantiate the setController from a query locator using Database.getQueryLocator. If you instantiate using a list of sObjects then the pagination is ignored and the whole recordset is returned. This is kind of a shame as the getQueryLocator does not accept dynamic SOQL, so if you want to build a fairly sophisticated search facility with paginated results then it does not appear to be possible using this method.
    Secondly, you are also not able to use lists of wrapper classes that contain results sets. For example if you wanted to allow the user to select multiple records from a search result my understanding is that the only way to do this is to construct a wrapper class that contains both the sobject and a boolean field to indicate whether the record has been selected. Since a StandardSetController can only be instantiated using Sobjects, pagination again does not appear possible.
    Can you confirm if my understanding is correct and if it is, then how would i go about developing a paginated solution for these scenarios?
    Thanks
    Gareth

  • Rohit

    Can’t we use this in our VF page which have linked with a custom controller.
    Coz when I trying to submit this page gives me below warning:
    Save error: No standard controller was specified – recordSetVar cannot be used. Bugzilla SFDC/src/pages ProjectSiteList.page

  • http://developer.force.com Jon Mountjoy

    Hi Rohit
    My laptop imploded so I’m a little constrained on what I can do until I get a new client.
    Can you please ask this question on the developer boards. The Visualforce team are there, and can help out with questions like this. (Use the Visualforce board)
    Thanks!
    Jon

  • Patrick Bulacz

    Why isn’t the CampaignMember object supported with this new list controller release.

  • Prashanth

    Hi Jon,
    I tried to use StandardSetController to pull PricebookEntries from a Pricebook. But I got a Visualforce error saying ‘List controllers are not supported for PricebookEntry’.Is the new version supporting this at all?

  • http://developer.force.com/ Jon Mountjoy

    Sorry – currently only objects that support list views may be used with the standard list controller. I suggest posting such an idea on Ideas.

  • Patrick Bulacz

    No dynamic SOQL’s though for query locator?? That’s just crazy!

  • Richard Vanhook

    Hi Jon, I like the StandardSetController but I’ve found it inflexible in many cases so I created a custom “Paginator” class. Here’s a blog I did on it: http://richardvanhook.wordpress.com/2009/08/03/visualforce-pagination-with-apex-lang/
    Hope it helps.

  • dowithforce

    Hello ,
    Thanks for help to salesforce developer community.
    Following is my requirement:
    I have millions of contacts/leads. I want fetch it and do next previous pagination and page shows 200 records at one time. what I possibly done with 10000 records
    ApexPages.StandardSetController ssc =
    new ApexPages.StandardSetController(Database.getQueryLocator([SELECT name FROM contact]));
    example provided in manuals and on forum.
    I want to fetch next 10000 records and so on what I can do with StandardSetController or getQueryLocator?
    Can you please tell me with an example code to do it?
    Thanks in advance.

  • Satya

    ssc.setPageSize(200);
    ssc.getRecords();

  • Hui

    I can do paginatin at server side, but can do sort at the same time, do you know if this is a SF’s limitation?
    When I do pagination and sort at the same time in the sever side. I can’t use two column name in “order by” clause. One of this two column is used for pagination, because SF’s can’t use ‘… limit 10,20′, he can only use ‘… limit 10′, so I have create a column to act as a index, when I do pagination, this index column must be used after ‘order by’ clause. So at this time, I can’t do sort job, because sort job also need I to add column name after ‘order by’ clause.
    I’m using a custome page to show data in SF, but seems hardly to do pagination and sort at the same time. do you think so too?