Friday, March 21, 2014

How to Write Pagination of visualforce page ?

MEthod 1:

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::VISUAL FORCE PAGE :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
<apex:page controller="Paginationclass">
  <apex:form >
        <apex:pageBlock id="pb">
            <apex:pageBlockTable value="{!opportunities}" var="o">
                <apex:column value="{!o.name}"/>
                <apex:column value="{!o.closedate}"/>
            </apex:pageBlockTable>
         
            <apex:commandButton rendered="{!setCon.hasPrevious}" value="first" action="{!setCon.first}" reRender="pb"/>
            <apex:commandButton rendered="{!setCon.hasPrevious}" value="Previous" action="{!setCon.previous}" reRender="pb"/>
            <apex:outputText rendered="{!(setCon.pageNumber * setCon.pageSize) < setCon.ResultSize}" value="{!setCon.pageNumber * setCon.pageSize} Of {!setCon.ResultSize}"></apex:outputText>
            <apex:outputText rendered="{!(setCon.pageNumber * setCon.pageSize) >= setCon.ResultSize}" value="{!setCon.ResultSize} Of {!setCon.ResultSize}"></apex:outputText>
         
            <apex:commandButton rendered="{!setCon.hasNext}" value="next" action="{!setCon.next}" reRender="pb"/>
         
            <apex:commandButton rendered="{!setCon.hasNext}" value="last" action="{!setCon.last}" reRender="pb"/>
           <apex:actionStatus id="status" >
              <apex:facet name="start">
                <apex:image value="/img/loading32.gif" style="height: 15px;"></apex:image>
              </apex:facet>
           </apex:actionstatus>
        </apex:pageBlock>
    </apex:form>
</apex:page>

======================CONTROLEER===============
public class Paginationclass{
    public ApexPages.StandardSetController setCon {
        get {
            if(setCon == null) {
                setCon = new ApexPages.StandardSetController(Database.getQueryLocator(
                      [select name,closedate from Opportunity]));
            }
            return setCon;
        }
        set;
    }

    // Initialize setCon and return a list of records
 
    public List<Opportunity> getOpportunities() {
         setCon.setpagesize(5);
         return (List<Opportunity>) setCon.getRecords();
    }
}


METHOD -2
==============VISUAL FORCE PAGE========================

<apex:page controller="paginationController">
 <apex:form >
   <apex:pageBlock >
    <apex:pageBlockButtons >
        <apex:commandButton value="Previous" action="{!Previous}" reRender="table" status="status"/>
          <apex:commandButton value="Next" action="{!Next}" reRender="table" status="status"/>
            <apex:actionStatus id="status" >
              <apex:facet name="start">
                <apex:image value="/img/loading32.gif" style="height: 15px;"></apex:image>
              </apex:facet>
           </apex:actionstatus>
    </apex:pageBlockButtons>
     <apex:pageblockTable value="{!lstAcct}" var="item" id="table">
        <apex:column value="{!item.name}"/>
        <apex:column value="{!item.Accountnumber}"/>
        <apex:column value="{!item.Phone}"/>
    </apex:pageblockTable>
  </apex:pageBlock>
 </apex:form>
</apex:page>
==================CONTROLLER==================
public class paginationController {
     Integer recordLimit = 10;
     Integer offSetLimit = 0;
    Public List<Account> lstAcct {
         get{
                 lstAcct = [select Id,name,Accountnumber,phone from Account  Limit :recordLimit OFFSET :offSetLimit ];
                 return lstAcct;
             }set;
     }
     
     public paginationController (){
       
     }
     
     Public pagereference Next(){
         if(lstAcct.size()!=offSetLimit ){
                   offSetLimit +=  4;
                   system.debug('@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@'+offSetLimit );
         }
       
         else{
          ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Warning,'No Records to Display'));
         }
         return null;
     }
     
     Public pagereference Previous(){
         if(offSetLimit > 0)
             offSetLimit = offSetLimit - 4;
         return null;
     }
}

No comments:

Post a Comment