Salesforce - Pagination for custom lists without using StandarSetController (Using Maps)
Salesforce!! - Something I'm not currently working on.. but would love to learn more of it.. I'm posting a piece of code i wrote for pagination on custom lists (say, a list of a custom class). This may not be a very efficient way, but it does work!!
Pagination - A technique used for displaying a list of items in pages, and navigate through the pages using "Next","Previous","First" and "Last" buttons(or links).
The most common way of adding pagination to your visualforce pages is to use StandardSetController. An example of Pagination using StandardSetController is given at the following link :
http://boards.developerforce.com/t5/Visualforce-Development/StandardSetController-and-Pagination/td-p/200267
This requires you define the list using a QueryLocator. This basically adds pagination to a list returned by a query.
The sample I'm going to provide deals with Custom lists (Lists of a custom class defined in our code and not a list that can be populated using a query)
To do this, we would need the following:
The list which has to be displayed in pages
A Map of the list items which will be used to determine pages
Another list to hold the items to be displayed on a single page
An integer to hold the page size
Two integers to mark the first and last numbers in a page
Next(), Previous(), First() and Last() methods to navigate through the records.
A method to populate the list to be displayed based on first and last numbers
And, here goes the code...
public class myCustomClass { // Your custom class definition } //Lists List<myCustomClass> listAllRecords = new List<myCustomClass>(); //List of items to be displayed in a page List<myCustomClass> listDisplay = new List<myCustomClass>(); //Map of All records Map<Integer,myCustomClass> mapAllRecords = new Map<Integer,myCustomClass>(); //Variable to hold the page size Integer pageSize = 25 ; //Change page size according to requirements //Variable to hold the size of the custom list Integer resultSize ; // Variables to point to the first and last item in a page Integer first, last; // Your method where the custom list is populated. public void yourMethod() { /*The custom list is populated here.. Now you have the data in your custom list (listAllRecords) */ // Initialization listDisplay.clear(); mapAllRecords.clear(); first = 1; last= pageSize; //Populate the map Integer temp=1; for(myCustomClass record : listAllRecords) { mapAllRecords.put(temp,record); temp=temp+1; } resultSize= mapAllRecords.keySet().size(); if(resultSize < pageSize) Last = tempSize; listDisplay = getListDisplay(); } /* Pagination Methods - Starts */ /*This method is used to populate the records to be shown for the current page. */ public List<myCustomClass> getListDisplay() { List<myCustomClass> listTempRecords = new List<myCustomClass>(); /* Get the records from first to last from the map. first and last holds the keys for first and last records in the map for a particular page. These variables are appropriately updated based on the page size to point to the first and last records in a page as per the commad (previous, next, first and last) */ for(Integer i=first ; i<=last ; i++) { listTempRecords.add(mapAllRecords.get(i)); } return listTempRecords; } // Next page public PageReference nextPage() { Integer tempSize = mapAllRecords.keySet().size(); //Set 'first' and 'last' if(last < tempSize ) first= last +1; last = first +(pageSize-1); if(last > tempSize) last = tempSize; // get the records between first and last listDisplay = getListDisplay(); return null; } //Previous page public PageReference previousPage() { Integer tempSize = mapAllRecords.keySet().size(); if(first > pageSize) { last = first-1; first = last - (pageSize-1); } else { if(tempSize < pageSize) last = tempSize; } listDisplay = getListDisplay(); return null; } //First page public PageReference firstPage() { Integer tempSize = mapAllRecords.keySet().size(); first = 1; if(tempSize < pageSize) last = tempSize; else last = pageSize; listDisplay = getListDisplay(); return null; } //Last page public PageReference lastPage() { Integer tempSize = mapAllRecords.keySet().size(); if(tempSize > pageSize) { Integer temp = math.mod(tempSize,pageSize); if(temp > 0) { last = tempSize; first = (last - temp) +1; } else { last = tempSize; first = last - (pageSize-1); } } else { last= tempSize; first= 1; } listDisplay = getListDisplay(); return null; } /* Pagination Methods - Ends */
Once you have all these methods in your controller, you can use them in your visualforce page to implement pagination. Use listDisplay list to populate your pageBlockTable. Add four buttons to your page : First, Previous, Next and Last, and set their actions to firstPage(), previousPage(), nextPage() and lastPage() respectively. After each action, rerender your pageBlockTable to refresh its contents.
And... Yes!! You have successfully implemented Pagination!! :)













