Sunday, September 13, 2015

Pagination

-----------------------------------------------------------------------------------------------------------------------
Pagination controller

--------------------------------------------------------------------------------------------------------------------------


public with sharing class PagingController {

    List<categoryWrapper> categories {get;set;}

    // instantiate the StandardSetController from a query locator
    public ApexPages.StandardSetController con {      
        get {
            if(con == null) {
                con = new ApexPages.StandardSetController(Database.getQueryLocator([Select Id, Name FROM Account Order By Name Asc limit 100]));
                // sets the number of records in each page set
                con.setPageSize(10);
            }
            return con;
        }
        set;
    }

    // returns a list of wrapper objects for the sObjects in the current page set
    public List<categoryWrapper> getCategories() {
        categories = new List<categoryWrapper>();
        for (Account category : (List<Account>)con.getRecords())
            categories.add(new CategoryWrapper(category));

        return categories;
    }

           
    // 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() {
         con.next();
     }

     // returns the PageReference of the original page, if known, or the home page.
     public void cancel() {
         con.cancel();
     }
       // displays the selected items
  /*   public PageReference process() {
         for (CategoryWrapper cw : categories) {
             if (cw.checked)
                 ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,cw.cat.name));
         }
         return null;
     } */
       

}
--------------------------------------------------------------------------------------------------------------------------
supporting wrapper class
---------------------------------------------------------------------------------------------------------------------

public class CategoryWrapper {

    public Boolean checked{ get; set; }
    public Account cat { get; set;}

    public CategoryWrapper(){
        cat = new Account();
        checked = false;
    }

    public CategoryWrapper(Account c){
        cat = c;
        checked = false;
    }

   

}


--------------------------------------------------------------------------------------------------------------------------
pagination page
-------------------------------------------------------------------------------------------------------------------------

<apex:page controller="PagingController" tabStyle="Account">
  <apex:form style="style='color: #78bf3a;'" title="Pagination">
    <apex:pageBlock title="Pagination"  >

      <apex:pageBlockButtons location="top">
     <!--   <apex:commandButton action="{!process}" value="Process Selected"/> -->
        <apex:commandButton action="{!cancel}" value="Cancel" style="color:#78bf3a;font-weight: normal;font-size:15px;height:30px;text-align:center;font-weight:bold;font-family:Arial,Helvetica,sans-serif;"/>
      </apex:pageBlockButtons>
      <apex:pageMessages />

      <apex:pageBlockSection title="Category Results -  Page #{!pageNumber}" columns="1">
        <apex:pageBlockTable value="{!categories}" var="c">
          <apex:column width="25px">
            <apex:inputCheckbox value="{!c.checked}"/>
          </apex:column>
          <apex:column value="{!c.cat.Name}" headerValue="Name"/>
        </apex:pageBlockTable>
      </apex:pageBlockSection>
    </apex:pageBlock>

    <apex:panelGrid columns="4">
    <apex:commandLink action="{!first}">First</apex:commandlink>
    <apex:commandLink action="{!previous}" rendered="{!hasPrevious}">Previous</apex:commandlink>
    <apex:commandLink action="{!next}" rendered="{!hasNext}">Next</apex:commandlink>
    <apex:commandLink action="{!last}">Last</apex:commandlink>
    </apex:panelGrid>

  </apex:form>
</apex:page>

No comments:

Post a Comment