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
Nishant Shrivastava 30Nishant Shrivastava 30 

visualforce pagination

i am trying to do pagination in my records but not happening, i don't what is wrong i am doing. kindly help asap

my visualforce page codes are 

<apex:page sidebar="false" Controller="Invoice_details" showHeader="False" docType="html-5.0" readOnly = "true">
    <html xmlns="https://www.w3.org/2000/svg" xmlns:xlink="https://www.w3.org/1999/xlink">
  <head> 
      <body>
          
      
      <apex:slds />
      <style>
             .ui-corner-all{
             z-index: 1;
            top: 34px;
            left: 200px;
            display: block;
            width: 403px;
             background-color: lightgray;
             }
             .displayNone { 
        display:none; 
    }
          
    .displayBlock {
        display:block;
    }
    .ui-autocomplete-loading { 
        background: white url(/img/loading32.gif) right center no-repeat;
        background-size:15px 15px; 
    }
    .placeHolder {
        font-style: italic;
          margin-left : 50px;
          margin-top : 20px;
          Width : 50rem;
          
    }
          .Showall {
          font-style: bold;
          
          margin-left : 160px;
          margin-top : 120px;
          
          width : 50rem;
          }
          .space {
          font-style: italic;
          font-size : 25px;
          margin-left : 50px;
          margin-top : 20px;
          }
          .slds-form-element__label slds-m-bottom_none{
          font-size : 2 rem;
          }
          .nishant{
          font.size : 1 rem;
          }
         .formelement{
          <div class="slds-form-element">
  <label class="slds-form-element__label" for="checkbox-id-01">Form Element Label</label>
  <div class="slds-form-element__control">
    <span class="slds-checkbox slds-checkbox_standalone">
      <input type="checkbox" id="checkbox-id-01" name="label" value="option" disabled="" checked="" />
      <span class="slds-checkbox_faux"></span>
    </span>
  </div>
</div>
          }
          
          .Outputpanel{
          margin-bottom : 50px;
          margin-top : 20px;
          margin-left :20px;
          }
          
          
          
         </style>
     <apex:form styleClass="Showall" >
         <div class="slds-form-element" >
           <label class="slds-checkbox_toggle slds-grid" >
         <span class="slds-form-element__label slds-m-bottom_none" style = "font-size:.90rem" > Show All </span>
         <input type="checkbox" name="checkbox-toggle-15" value="checkbox-toggle-15" aria-describedby="checkbox-toggle-15" checked=""  />
         <span id="checkbox-toggle-15" class="slds-checkbox_faux_container" aria-live="assertive" style="margin-left:15px" >
           <span class="slds-checkbox_faux"></span>
           <span class="slds-checkbox_on">Enabled</span>
           <span class="slds-checkbox_off">Disabled</span>
    </span>
  </label>
</div>
      </apex:form>
      <apex:form id="frm" styleClass="placeHolder" >
          <div class="slds-grid slds-grid_align-space">
              <div class="slds-col">
            <span >Date From
          <apex:inputfield value="{!InvoiceInfo.kognoz1__Invoice_Date__c}" id="datefrom"/>
                </span>
              </div>
              <div class="slds-col" >
               <span >Date To 
                <apex:inputfield value="{!InvoiceInfo.kognoz1__Invoice_Date__c}" id="dateto" onSelect="display()" />
            </span>
            </div>
  </div>
       </apex:form>
          <apex:form styleClass="Outputpanel" >
        <apex:pageBlock id="display1" >
          <apex:pageBlockSection showHeader="false"  >
          <apex:pageBlockTable value="{!InvoiceList}" var="rs"  >
              <apex:selectCheckboxes value="{!rs.attachments}">
                  <selectoptions value="{!rs.attachments}"/>
                             </apex:selectCheckboxes>
              <apex:column style="formelement" />
              <apex:column value="{!rs.kognoz1__Invoice_Date__c }" headerValue="DATE"/>
              <apex:column value="{!rs.kognoz1__Account__c}" headerValue="Account"/>
                  <apex:column value="{!rs.kognoz1__Opportunity__c}" headerValue="Opportunity"/>
               
                      </apex:pageBlockTable>
              </apex:pageBlockSection>
                                              
            <apex:panelGrid columns="4" >
            <apex:commandLink action="{!Beginning}" reRender="getinvoicedetails">FIRST</apex:commandLink>
            <apex:commandLink action="{!Next}" reRender="getDisablePrevious" >NEXT</apex:commandLink>
            <apex:commandLink action="{!Previous}" reRender="getDisableNext" >PREVIOUS</apex:commandLink>
            <apex:commandLink action="{!End}" reRender="getTotalPages" >LAST</apex:commandLink>
            </apex:panelGrid>
                
            </apex:pageBlock>
          </apex:form>
          </body>
    </head>
 
    
        <script type="text/javascript">
    function changeValue(radioButton) {
        var chk = document.getElementsByClassName('selection');

        for (var i=0; i<chk.length; i++) {
            if(radioButton.value == chk[i].value) {
                chk[i].checked = true;
            } else {
                chk[i].checked = false;
            }
        }
    }
        </script>
    </html>


Controller class

Public without sharing class  Invoice_details {
    
    public List<kognoz1__Invoice2__c> InvoiceList {get;set;}
    public kognoz1__Invoice2__c InvoiceInfo { get;set;}
        public static String StartDate {get;set;}
    public List<kognoz1__Invoice2__c> getKognozdetails { get;set;}
    public static String EndDate {get;set;}
       public static string querystr;
        private integer counter = 0; 
        private integer list_size=20;
        private integer total_size = 20;
        
    public Invoice_details(){ 
        InvoiceInfo = new kognoz1__Invoice2__c ();
       InvoiceList = [SELECT Id,kognoz1__Invoice_Date__c, kognoz1__Account__c, kognoz1__Opportunity__c FROM kognoz1__Invoice2__c limit 1000];
        //InvoiceList = Database.query(querystr);
        //List<kognoz1__Invoice2__c> KI = [SELECT Id,kognoz1__Invoice_Date__c, kognoz1__Account__c, kognoz1__Opportunity__c FROM kognoz1__Invoice2__c ]; 
        
    }
    
    
    
       /* public PageReference runSearch(){
        InvoiceList.clear();
        if(InvoiceInfo.kognoz1__Invoice_Date__c != null & InvoiceInfo.kognoz1__Invoice_Date__c != null ){
        startDate=String.valueOf(InvoiceInfo.kognoz1__Invoice_Date__c).left(10);
         endDate=String.valueOf(InvoiceInfo.kognoz1__Invoice_Date__c).left(10);
        String querystr='SELECT Id,kognoz1__Invoice_Date__c, kognoz1__Account__c, kognoz1__Opportunity__c FROM kognoz1__Invoice2__c WHERE kognoz1__Invoice_Date__c ='+startDate+' AND kognoz1__Invoice_Date__c='+ endDate;
        InvoiceList = Database.query(querystr);
        }
        return null;
    } */ 
    
           /*public void Invoicedetails () {
        if(InvoiceInfo.kognoz1__Invoice_Date__c != null & InvoiceInfo.kognoz1__Invoice_Date__c != null ){
        String startDate=String.valueOf(InvoiceInfo.kognoz1__Invoice_Date__c).left(10);
        String endDate=String.valueOf(InvoiceInfo.kognoz1__Invoice_Date__c).left(10);
        String querystr='SELECT Id,kognoz1__Invoice_Date__c, kognoz1__Account__c, kognoz1__Opportunity__c FROM kognoz1__Invoice2__c WHERE kognoz1__Invoice_Date__c ='+startDate+' AND kognoz1__Invoice_Date__c='+ endDate;
        InvoiceList = Database.query(querystr); 
        }
        }
*/


        Public List <kognoz1__Invoice2__c> getinvoicedetails()
        {
           /*String querystr='SELECT Id,kognoz1__Invoice_Date__c, kognoz1__Account__c, kognoz1__Opportunity__c FROM kognoz1__Invoice2__c';
            InvoiceList = Database.query(querystr);
            system.debug(InvoiceList); */
            list<kognoz1__Invoice2__c> KON = [select Id,kognoz1__Invoice_Date__c, kognoz1__Account__c, kognoz1__Opportunity__c from kognoz1__Invoice2__c order by kognoz1__Invoice_Date__c limit : list_size offset : total_size ];
            return null;
            
        }
    
          public integer Beginning() { 
          counter = 0;
              List_size = 20;
          
          return null;
       }
    
       public integer Previous() { 
          counter -= list_size;
          
          return null;
       }
    
       public integer Next() { 
          counter += list_size;
          
          return null;
       }
    
     
       public integer End() { 
          counter = total_size - math.mod(total_size, list_size);
          if(counter==total_size){
              counter=counter-list_size;
          }
         
          return null;
       }
    
       public Boolean getDisablePrevious() { 
         if (counter>0) {
          return false;
          } else return true;
       }
    
       public Boolean getDisableNext() {
        if (counter + list_size < total_size){ 
            
            return false;} else return true;
        }
    
       public Integer getTotal_size() {
          return total_size;
       }
    
       public Integer getPageNumber() {
          return counter/list_size + 1;
       }
    
       public Integer getTotalPages() {
               if(total_size==null)
                   total_size=0;
               
            if (math.mod(total_size, list_size) > 0) {
             return total_size/list_size + 1;
            } else {
             return (total_size/list_size);
         }
       }
    }

i know, i click on next button, it does not take me anywhere..i messed up a lot out of frustation but your input will be helpful and valuable for me. Thanks in advance.
Deepali KulshresthaDeepali Kulshrestha
Hi Nishant,

I went through your problem, please refer through these codes for visualforce pagination.



VisualForce
...............................
<apex:page controller="Q5">  
    <apex:form >  
        <apex:pageBlock id="details" title="Details">
            
            <apex:dataList value="{!Account}" var="acc" > 
                <apex:outputText value="{!acc.Id}" /> &nbsp;&nbsp; 
            <apex:outputText value="{!acc.Name}"/> 
                </apex:dataList>
        
        <apex:pageBlockButtons>
            <apex:commandButton value="First" rerender="details" action="{!beginning}" disabled="{!prev}"/>  
            <apex:commandButton value="Previous" rerender="details" action="{!previous}" disabled="{!prev}"/>  
            <apex:commandButton value="Next" rerender="details" action="{!next}" disabled="{!nxt}"/>  
            <apex:commandButton value="Last" rerender="details" action="{!end}" disabled="{!nxt}"/> 
        </apex:pageBlockButtons>
    </apex:pageBlock>
</apex:form>  
</apex:page>



.............................................
Apex Class
.............................................

public class Q5 {
    private integer totalRecords = 0;       
    private integer index = 0;  
    private integer blockSize = 10;           
      
    public Q5() {  
        totalRecords = [select count() from Account limit 200];        
    }       
    public List<Account> getAccount() {  
        List<Account> accRecords = Database.Query('SELECT Id,Name FROM Account LIMIT : blockSize OFFSET :index');  
        System.debug('Values are ' + accRecords);  
        return accRecords;  
    }      
    public void beginning() {  
        index = 0;  
    }   
    public void previous() { 
        index = index - blockSize;  
    }  
    public void next() {  
        index = index + blockSize;  
    }  
    public void end() {  
        index = totalRecords - math.mod(totalRecords,blockSize);  
    }            
    public boolean getprev() {  
        if(index == 0)  
            return true;  
        else  
            return false;  
    }     
    public boolean getnxt() {  
        if((index + blockSize) > totalRecords)  
        return true;  
        else  
        return false;  
    }      
}



I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com
Nishant Shrivastava 30Nishant Shrivastava 30
sorry Deepali , it could not resolve my problem. please check my code if you find any error