Monday, March 03, 2014

Pagination


  1. <apex:page controller="multiRow" tabStyle="Contact">
  2. <apex:form id="accountForm" >
  3. <apex:pageMessages />
  4. <apex:pageBlock title="Search Results" >
  5. <apex:pageBlockButtons >
  6. <apex:outputPanel rendered="{!totalPages > 1}" >
  7. <table border="0" style="width:100%; text-align: right">
  8. <tr>
  9. <td style="width:100%">&nbsp;</td>
  10. <td style="vertical-align: middle">
  11. <apex:actionStatus id="statusPaging">
  12. <apex:facet name="start">
  13. <apex:outputPanel >Please wait...</apex:outputPanel>
  14. </apex:facet>
  15. <apex:facet name="stop" />
  16. </apex:actionStatus>
  17. </td>
  18. <td style="text-align: center; white-space: nowrap">
  19. <b>Page:</b><br/>{!contactsSetController.PageNumber} of {!totalPages}
  20. </td>
  21. <td style="vertical-align: middle">
  22. <apex:commandButton value="|<" action="{!contactsSetController.First}"
  23. disabled="{!Not(contactsSetController.HasPrevious)}" rerender="accountForm" status="statusPaging"/>
  24. </td>
  25. <td style="vertical-align: middle">
  26. <apex:commandButton value="<" action="{!contactsSetController.Previous}"
  27. disabled="{!Not(contactsSetController.HasPrevious)}" rerender="accountForm" status="statusPaging"/>
  28. </td>
  29. <td style="vertical-align: middle">
  30. <apex:commandButton value=">" action="{!contactsSetController.Next}"
  31. disabled="{!Not(contactsSetController.HasNext)}" rerender="accountForm" status="statusPaging"/>
  32. </td>
  33. <td style="vertical-align: middle">
  34. <apex:commandButton value=">|" action="{!contactsSetController.Last}"
  35. disabled="{!Not(contactsSetController.HasNext)}" rerender="accountForm" status="statusPaging"/>
  36. </td>
  37. </tr>
  38. </table>
  39. </apex:outputPanel>
  40. </apex:pageBlockButtons>
  41. <apex:pageBlockTable value="{!Contacts}" var="contact" >
  42. <apex:column headerValue="Name">
  43. <apex:outputLink value="/{!contact.id}" id="contactLink">{!contact.name}</apex:outputLink>
  44. </apex:column>
  45. <apex:column value="{!contact.FirstName}"/>
  46. <apex:column value="{!contact.LastName}"/>
  47. </apex:pageBlockTable>
  48. </apex:pageBlock>
  49. </apex:form>
  50. </apex:page>
----------------------------------------------------------------------------------------------------

  1. public class multiRow {
  2. public Integer totalPages { get; set; }
  3. public ApexPages.StandardSetController contactsSetController { get; set; }
  4. public List<Contact> Contacts {
  5. get {
  6. return (List<Contact>) contactsSetController.getRecords();
  7. }
  8. }
  9.  
  10. public multiRow () {
  11. Decimal dtotalPages;
  12. Integer iRecordsPerPage = 5;
  13. Database.QueryLocator qLoc;
  14. // You can initialize the set controller using a SOQL query:
  15. qLoc = Database.getQueryLocator([Select Id, Name, AccountId, FirstName, LastName From Contact c]);
  16. contactsSetController = new ApexPages.StandardSetController(qLoc);
  17. // Or with a list of records, which is useful for SOSL searches or if you already have the records in a list.
  18. // List<Sobject> lContacts = [FIND 'smith*' IN ALL FIELDS RETURNING Contact (Id, Name, AccountId, FirstName, LastName )][0];
  19. // contactsSetController = new ApexPages.StandardSetController(lContacts);
  20. // Create the set controller
  21.  
  22. // Set the number of records to be displayed per page and calculate the number of pages.
  23. contactsSetController.setPageSize(iRecordsPerPage);
  24. dtotalPages = (contactsSetController.getResultSize() / contactsSetController.getPageSize());
  25. dtotalPages = Math.floor(dtotalPages) + ((Math.mod(contactsSetController.getResultSize(), iRecordsPerPage)>0) ? 1 : 0);
  26. totalPages = Integer.valueOf(dtotalPages);
  27. }
  28. }

No comments:

Post a Comment

Thanks for your comment