function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion

Paging help

Hi there,


I am trying to implement paging and I am not sure if this is doable and where to start.


Here is what I need to do:


I have a visualforce page that custom controller.  In the custom controller I have two set of arrays, and both of them hold CampaignMembers.  I am displaying both records on the UI. I want to do paging for both of them. Is it possible to do paging like this and where do I start? Thanks.




Look at StandardSetController and QueryLocator.  StandardSetController has all of the built in stuff for paging.


Keep in mind 2 things though:  1) StandardSetController only paginates when called with a QueryLocator, and 2) QueryLocators expire in 15 minutes so you have to make a method of keeping the QueryLocator active.


(Hint:  actionPoller set to 10min interval calling a method that calls the next method, then the prev method on the StandardSetController)





Do I need to use QueryLocator. I have the data already, so is there a way to page the records that I already have?

Unfortunately you have to use QueryLocator otherwise it'll just show all the items in a single page.  Also it is better practice to use a QueryLocator if you have a large set of data, otherwise you might run into governor limits such as "heap stack".  I've learned that one from experience a few times :)


You can pass filters to the QueryLocator constructor.  If you have a Set of Ids you could use them to make the query. 





There are attributes on the iteration components in Visualforce to declare what row number to start with but as others have noted, this will break down with larger data sets and you will effectively be penalized for the rows you aren't displaying to the user and you risk making the view state in the page unnecessarily large unless you take preventative measures (and only include the ID set in your state, for example)  which are provided for you using StandardSetController constructed with a querylocator. QueryLocator supports all the constructs of SOQL - bind variables, dynamic, etc - so there's not much reason to avoid it unless it's not supported on the object you are querying.






I tried using QueryLocator to do paging, but it doesn't seem like it works for CampaignMembers for some reason. It works for contact, but it's saying 'List controllers are not supported for CampaignMember'.


This is controller that I have.

public with sharing class pagingController { // instantiate the StandardSetController from a query locator public ApexPages.StandardSetController con { get { if(con == null) { con = new ApexPages.StandardSetController( Database.getQueryLocator([ Select Id FROM CampaignMember Order By Id])); // sets the number of records in each page set con.setPageSize(5); } return con; } set; } // indicates whether there are more records after the current page set. public Boolean hasNext { get { return con.getHasNext(); } set; } // indicates whether there are more records before the current page set. public Boolean hasPrevious { get { return con.getHasPrevious(); } set; } // returns the page number of the current page set public Integer pageNumber { get { return con.getPageNumber(); } set; } // returns the first page of records public void first() { con.first(); } // returns the last page of records public void last() { con.last(); } // returns the previous page of records public void previous() { con.previous(); } // returns the next page of records public void next() {; } // returns the PageReference of the original page, if known, or the home page. public void cancel() { con.cancel(); } public List<CampaignMember> getContacts() { return (List<CampaignMember>) con.getRecords(); } }


The same above code works for contacts.  I am stuck. How can I do paging for CampaignMembers then?

If list controllers won't work for campaign members you'll have to do it the way Andrew mentioned.  On objects like repeat, dataTable, pageBlockTable etc that take a list as a variable, there is a set of parameters to limit which data to show in the table at that moment.  You can use these to paginate the data.


pageBlockTable properties:

first - the first element in the iteration (where the list starts displaying)

rows - the number of rows to show in the list


Using these two properties you can paginate the set of data you have.  I believe it is the same properties used in repeat and dataTable as well.