Visualforce Pagination in Winter '09
by Jon Mountjoy on September 20, 2008 at 05:42 AM
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.

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
recordSetvarattribution 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()andnext()
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!
TrackBack
TrackBack URL for this entry: http://www.typepad.com/services/trackback/6a00d8341cded353ef010534b64a76970b
Listed below are links to weblogs that reference Visualforce Pagination in Winter '09:

Comments
Posted by Chirag Mehta on September 23, 2008 12:24 PM:
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
Posted by Nitin on September 29, 2008 05:22 PM:
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
Posted by Jon Mountjoy on September 30, 2008 03:06 AM:
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
Posted by Gareth Davies on October 6, 2008 01:27 AM:
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
Posted by Jon Mountjoy on October 6, 2008 01:38 AM:
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!
Posted by Ron Wild on October 9, 2008 02:08 PM:
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?
Posted by Andrew Waite on October 11, 2008 05:13 PM:
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.
Posted by Gareth Davies on October 17, 2008 07:24 AM:
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
Posted by Rohit on February 10, 2009 06:52 AM:
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
Posted by Jon Mountjoy on February 10, 2009 10:26 AM:
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
The comments to this entry are closed.