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
learning1.3953795511514497E12learning1.3953795511514497E12 

Pageblocktable pagination is not working properly

Hi,

I have created a pageblocktable with pagination by refering to " http://www.redpointsolutions.com/add-pagination-to-your-visualforce-pages-using-the-soql-offset-clause ". My code is as below...

VF PAGE
==============

<apex:page title="Salesforce SOQL Offset Example Using Visualforce" controller="soql_offset_example" showHeader="false" sidebar="false" readOnly="true" cache="false">

<apex:sectionHeader subtitle="SOQL Offset Example" title="Square Root Table"/>

<apex:pageBlock >  
   <apex:pageBlockButtons location="top" >
   <apex:outputPanel id="myButtons">
   <apex:form >
        <apex:commandButton action="{!Beginning}" title="Beginning" value="<<" disabled="{!disablePrevious}" reRender="myPanel,myButtons"/>
        <apex:commandButton action="{!Previous}" title="Previous" value="<" disabled="{!disablePrevious}" reRender="myPanel,myButtons"/>       
        <apex:commandButton action="{!Next}" title="Next" value=">" disabled="{!disableNext}" reRender="myPanel,myButtons"/>
        <apex:commandButton action="{!End}" title="End" value=">>" disabled="{!disableNext}" reRender="myPanel,myButtons"/>       
   </apex:form>
   </apex:outputPanel>
   </apex:pageBlockButtons>

   <apex:pageBlockSection title="Numbers and their Square Roots (Total List Size: {!total_size})" collapsible="false">
   <apex:outputPanel id="myPanel">
   <apex:pageMessages id="theMessages" />
   <apex:pageBlockTable value="{!LinkedinCompanies}" var="l" align="center">
        <apex:column value="{!l.id}" />
        <apex:column value="{!l.name}" />
        <apex:facet name="footer">Showing Page # {!pageNumber} of {!totalPages}</apex:facet>
   </apex:pageBlockTable>
   </apex:outputPanel>
   </apex:pageBlockSection>

</apex:pageBlock>

</apex:page>

Controller
============
public with sharing class soql_offset_example {

   private integer counter=0;  //keeps track of the offset
   private integer list_size=5; //sets the page size or number of rows
   public integer total_size; //used to show user the total size of the list
   String linkedinResponse;
   public List<companyDetails> LinkedinCompanies{get;set;}
  
   public class companyDetails
    {
        public Integer id {get;set;}
        public String name {get;set;}
        public String universalName {get;set;}       
        Public String size {get;set;}
        Public String industry {get;set;}
        Public String ticker {get;Set;}
        public String logoURL{get;set;}
        public string websiteurl {get;set;}       
        public String description {get;set;}
        Public locationDetails locations{get;set;}
    }
   
    public class locationDetails
    {
        public integer lin_total{get;set;}
        public List<locationValues> values{get;set;}                        
    }
   
    public class locationValues
    {
        public addressDetails address{get;set;}
        public contactDetails contactInfo{get;set;}     
    }
   
    public class addressDetails
    {
        public String city{get;set;}  
        public String postalCode{get;set;}
        public String street1{get;set;}            
    }
   
    public class contactDetails
    {
        public String fax{get;set;}
        public String phone1{get;set;}     
    }   
       


   public soql_offset_example()
   {
       //total_size = 20; //set the total size in the constructor
       getNumbers();
   }

   public void  getNumbers() {
      try
      {
              
            linkedinResponse =  '{"companies": { "_count": 10, "_start": 0, "_total": 55573, "values": [ { "id": 10667, "industry": "Internet", "name": "Facebook" }, { "id": 3748947, "industry": "Marketing & Advertising", "name": "Community Management Facebook y Facebook Ads" }, { "id": 3219314, "industry": "Marketing & Advertising", "name": "Smart Facebook Likes" }, { "id": 2620756, "industry": "Information Technology & Services", "name": "Revolucionamos Facebook" }, { "id": 3326206, "industry": "Marketing & Advertising", "name": "Facebook-buy-fans.com" }, { "id": 2811427, "industry": "Marketing & Advertising", "name": "Empowers Facebook Marketing for Business" }, { "id": 2226401, "industry": "Marketing & Advertising", "name": "Facebook Advertising" }, { "id": 5059292, "industry": "Internet", "name": "Facebook Covers" }, { "id": 3100592, "industry": "Real Estate", "name": "Facebook Property Listing by propertybooster.com" }, { "id": 2453105, "industry": "Accounting", "name": "FaceBook Business Page Your Biz First" } ] }}';
             linkedinResponse =linkedinResponse.replace('"_', '"lin_');   
             System.debug('********** linkedinResponse *******' +linkedinResponse); 
                    
         
           Map<String, Object> linkedinRespCompanies = (Map<String, Object>) JSON.deserializeUntyped(linkedinResponse);                                           
           Map<String,Object> CompaniesMap = (Map<String,Object>)linkedinRespCompanies.get('companies');                   
           List<Object> CompaniesList = (List<Object>)CompaniesMap.get('values');                    
           String CompaniesListSerialized = JSON.serialize(CompaniesList);
           LinkedinCompanies = (List<companyDetails>) JSON.deserializeStrict(CompaniesListSerialized, List<companyDetails>.class);
          total_size = LinkedinCompanies.size();
      }
       catch (Exception e)
       {
         ApexPages.addMessages(e);  
        // return null;
      }
   }

   public PageReference Beginning() { //user clicked beginning
      counter = 0;
      return null;
   }

   public PageReference Previous() { //user clicked previous button
      counter -= list_size;
      return null;
   }

   public PageReference Next() { //user clicked next button
      counter += list_size;
      return null;
   }

   public PageReference End() { //user clicked end
      counter = total_size - math.mod(total_size, list_size);
      return null;
   }

   public Boolean getDisablePrevious() {
      //this will disable the previous and beginning buttons
      if (counter>0) return false; else return true;
   }

   public Boolean getDisableNext() { //this will disable the next and end buttons
      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 (math.mod(total_size, list_size) > 0) {
         return total_size/list_size + 1;
      } else {
         return (total_size/list_size);
      }
   }
}

Eventhough the button and footer are changing correctly,  but the actual data is not changing in table according the list_size mentioned. Please help...


Anu
Ramesh KallooriRamesh Kalloori
Please use the below code.
<apex:page controller="pagingControllerForUser">  
    <apex:form >  
        <apex:pageBlock >  
              
            <apex:pageMessages id="pgm"/>  
              
            <apex:pageBlockButtons >  
                <apex:commandButton value="Search" action="{!Search}"/>  
            </apex:pageBlockButtons>  
              
            <apex:pageBlockSection >  
                <apex:pageBlockSectionItem >  
                    <apex:outputLabel value="Companay Name"/>  
                    <apex:inputText value="{!cName}"/>  
                </apex:pageBlockSectionItem>  
            </apex:pageBlockSection>  
  
        </apex:pageBlock>  
  
   <apex:pageBlock rendered="{!IF(Entity!=null , true , false)}"> 
  
        <apex:outputPanel layout="block" styleClass="pSearchShowMore" id="otpNav2">  
              Total Records Found:
              
               <apex:outputText rendered="{!IF(EntitySize==25,true,false)}">10000 +</apex:outputText>
               <apex:outputText rendered="{!IF(EntitySize< 25,true,false)}">{!EntitySize}</apex:outputText>  
                  <apex:image url="/img/search_prevarrow_disabled.gif" styleClass="prevArrow" rendered="{!NOT(Con.HasPrevious)}"/>  
                  <apex:image url="/img/search_prevarrow.gif" title="Previous Page" styleClass="prevArrow" rendered="{!Con.HasPrevious}"/>  
                  <apex:commandLink action="{!Previous}" title="Previous Page" value="Previous Page" rendered="{!Con.HasPrevious}"/>  
                 <!-- <apex:outputPanel styleClass="pShowLess noLink" style="color:grey" rendered="{!NOT(Con.HasPrevious)}">Previous Page</apex:outputPanel>           
                  &nbsp;({!IF(Con.PageNumber == 1,1,((Con.PageNumber -1) * Con.PageSize)+1)}-{!IF(Con.resultSize < Con.PageSize,Con.resultSize,Con.PageNumber * Con.pageSize)})&nbsp;  -->
                  <apex:outputPanel styleClass="pShowLess noLink" style="color:grey" rendered="{!NOT(Con.HasNext)}">Next Page</apex:outputPanel>           
                  <apex:commandLink title="Next Page" value="Next Page" rendered="{!Con.HasNext}" action="{!Next}"/>&nbsp;  
                  <apex:image url="/img/search_nextarrow.gif" title="Next Page" styleClass="nextArrow" rendered="{!Con.HasNext}"/>  
                  <apex:image url="/img/search_nextarrow_disabled.gif" rendered="{!NOT(Con.HasNext)}"/>            
              </apex:outputPanel>
              
            <apex:pageBlockSection columns="1" id="sec1">  
                <apex:pageBlockTable value="{!Entity}" var="UR">  
                 <apex:column >
                    <apex:facet name="header"> 
                        <apex:inputCheckbox >
                            <apex:actionSupport event="onclick" action="{!GetSelected}" onsubmit="checkAll(this)" rerender="opp_table" status="newStatus"/>
                         </apex:inputCheckbox>
                     </apex:facet>
                    <!--<apex:inputCheckbox value="{!c.selected}" id="checkedone">
                        <apex:actionSupport event="onclick" action="{!GetSelected}" rerender="opp_table"/>
                    </apex:inputCheckbox>-->
                </apex:column>

                    <apex:column headerValue="Name" value="{!UR.Company_Name__c}"/>  
                    <apex:column headerValue="Email" value="{!UR.Highest_Price__c}"/>  
                    <apex:column headerValue="Phone" value="{!UR.Lowest_Price__c }"/>  
                  
                </apex:pageBlockTable>  
            </apex:pageBlockSection> 
              
     <apex:outputPanel layout="block" styleClass="pSearchShowMore" id="otpNav">  
              Total Records Found: <apex:outputText rendered="{!IF(EntitySize==5,true,false)}">5+</apex:outputText><apex:outputText rendered="{!IF(Con.resultSize < 10000,true,false)}">{!EntitySize}</apex:outputText>  
                  <apex:image url="/img/search_prevarrow_disabled.gif" styleClass="prevArrow" rendered="{!NOT(Con.HasPrevious)}"/>  
                  <apex:image url="/img/search_prevarrow.gif" title="Previous Page" styleClass="prevArrow" rendered="{!Con.HasPrevious}"/>  
                  <apex:commandLink action="{!Previous}" title="Previous Page" value="Previous Page" rendered="{!Con.HasPrevious}"/>  
                  <apex:outputPanel styleClass="pShowLess noLink" style="color:grey" rendered="{!NOT(Con.HasPrevious)}">Previous Page</apex:outputPanel>           
                  &nbsp;({!IF(Con.PageNumber == 1,1,((Con.PageNumber -1) * Con.PageSize)+1)}-{!IF(Con.resultSize < Con.PageSize,Con.resultSize,Con.PageNumber * Con.pageSize)})&nbsp;  
                  <apex:outputPanel styleClass="pShowLess noLink" style="color:grey" rendered="{!NOT(Con.HasNext)},otpNav2,otpNav">Next Page</apex:outputPanel>           
                  <apex:commandLink title="Next Page" value="Next Page" rendered="{!Con.HasNext}" action="{!Next}"/>&nbsp;  
                  <apex:image url="/img/search_nextarrow.gif" title="Next Page" styleClass="nextArrow" rendered="{!Con.HasNext}"/>  
                  <apex:image url="/img/search_nextarrow_disabled.gif" rendered="{!NOT(Con.HasNext)}"/>            
              </apex:outputPanel> 
  
       </apex:pageBlock> 
  
    </apex:form>  
</apex:page>
public class pagingControllerForUser  
{ 
    public Boolean c { get{return true;} set; }
 
public List<NSE__c> obj=new List<NSE__C>();
    public List<User> AllSearchUsers  
    {  
        get  
        {  
            if(con != null)  
            
                return (List<User>)con.getRecords();  
            else  
                return null ;  
        }  
        set;}  
      
    public User usr {get; set;}  
      public String cName{get;set;}
    //Controller  
    public pagingControllerForUser()  
    {  
ApexClass deletedClass = [SELECT id FROM ApexClass WHERE name = 'pagingControllerForUser'];
System.Debug(deletedClass.id);
     Entity=[select Company_Name__c,Highest_Price__c,Lowest_Price__c from NSE__c];
    con = new ApexPages.StandardSetController(Database.getQueryLocator([select Company_Name__c,Highest_Price__c,Lowest_Price__c from NSE__c]));  
       con.setPageSize(1);  
       EntitySize=Entity.size();
        AllSearchUsers = new List<User>() ; 
  // Entity=obj; 
        usr = new User() ;  
    }  
      
     public PageReference Search()  
    {     
        if(cName!= null)  
        
        {
         String SearchString='%'+cName+'%';  
            con = new ApexPages.StandardSetController(Database.getQueryLocator([select Company_Name__c,Highest_Price__c,Lowest_Price__c from NSE__c where Company_Name__c like :SearchString]));  
   
            // sets the number of records in each page set  
            con.setPageSize(1);  
        }  
        else  
        {  
            con = null;  
        }  
        return null ;  
    }  
      
    //Instantiate the StandardSetController  
    public ApexPages.StandardSetController con{get; set;}  
      
    //Boolean to check if there are more records after the present displaying records  
    public Boolean hasNext  
    {  
        get  
        {  
            return con.getHasNext();  
        }  
        set;  
    }  
   
    //Boolean to check if there are more records before the present displaying records  
    public Boolean hasPrevious  
    {  
        get  
        {  
            return con.getHasPrevious();  
        }  
        set;  
    }  
   
    //Page number of the current displaying records  
    public Integer pageNumber  
    {  
        get  
        {  
            return con.getPageNumber();  
        }  
        set;  
    }  
  
    //Returns the previous page of records  
    public void previous()  
    {  
        con.previous();  
    }  
   
    //Returns the next page of records  
    public void next()  
    {  
        con.next();  
    }  
    
    public List<NSE__c> Entity
    {
   get  
        {  
            if(con != null)  
            
                return (List<NSE__c>)con.getRecords();  
            else  
                return null ;  
        }  
      set;
    }
    
    
    public Integer EntitySize
    {
      get;
       /* {  
              List<NSE__c> obj=[select Company_Name__c,Highest_Price__c,Lowest_Price__c from NSE__c];

              return obj.size();
        }*/
        set;
    }
  public PageReference getSelected() {
  return null;
}
}