Display more than 3000 records in a VF page

I have a requirement to display around 3000 records in a table on VF page. There might be around 10-15 columns on the table. I also need to perform update operations on the records. It is a list of wrappers(i.e., sObject + chkbox), which have to be displayed Let me know if we can achieve it through pageblocktable or if in any other case, please provide sample code.

Answer

It’s difficult to give a full code. Below is the format which I’m using in one of my pages working fine. Get the idea and try your own.
VF Page

<apex:pageBlock id="resultBlock" mode="maindetail">                         
    <apex:outputPanel >                             <apex:pageMessages />

         <!-- Page navigation -->
         <div align="left" style="padding-top: 5px; padding-right: 5px; padding-bottom: 5px; padding-left: 5px;">         
              <font size="1pt">Page #:&nbsp;
                   <apex:outputLabel value="{!pageNumber}"/>&nbsp;out of&nbsp;
                   <apex:outputLabel value="{!totalPages}"/>&nbsp; &nbsp; &nbsp; &nbsp;
              </font>                                                                                        
              <apex:commandButton action="{!Beginning}" title="Beginning" value="<<" disabled="{!disablePrevious}" reRender="resultBlock" />
              <apex:commandButton action="{!Previous}" title="Previous" value="<" disabled="{!disablePrevious}" reRender="resultBlock" />        
              <apex:commandButton action="{!Next}" title="Next" value=">" disabled="{!disableNext}" reRender="resultBlock" />
              <apex:commandButton action="{!End}" title="End" value=">>" disabled="{!disableNext}" reRender="resultBlock" />                                                              
         </div>       

  </apex:outputPanel>  

  <apex:outputPanel id="resultsPanel" rendered="{!isResultTableVisible}">
    <apex:pageBlockTable id="resultsTbl" value="{!scheduleListUI}" var="item" cellspacing="1px" >
        <!-- Your Columns here -->
    </apex:pageBlockTable>
    </apex:outputPanel>
</apex:pageBlock>

Controller

public with sharing class Controller{

    private integer offset; // Keeps track of the offset     
    private integer pageSize;   // Page size or number of rows           
    public integer totalRecords;    // Total number of records   
    public Boolean isResultTableVisible{get;set;} //use this to control the visibility of your pageBlockTable
    //define your wrapper list as well here.

    public Controller(){
        isResultTableVisible = false;   
        offset = 0;
        pageSize = 10;
        totalRecords = 0;
    }

    public void generateYourTableContentList(){
        // Here use the offset variable to filter your wrapper list
    }

    /**
    * User clicked in beginning
    *   Set the offset to zero so that 1st record onwards will be retrieved
    */ 
    public PageReference Beginning() {
        offset = 0;
        //refresh your 
        return null;
    }


   /**
    * User clicked previous button
    *   reduce offset by one time of page size
    */ 
    public PageReference Previous() {
        offset -= pageSize;  
        generateScheduleListUI();      
        return null;
    }


   /**
    * User clicked next button
    *   increase offset by one time of page size
    */ 
    public PageReference Next() {
        offset += pageSize;   
        generateScheduleListUI();   
        return null;
    }


   /**
    * User clicked end
    *   retrive the final records that can be displayed in the defined page size
    */ 
    public PageReference End() {

        integer modValue = math.mod(totalRecords, pageSize);                
        offset = totalRecords - math.mod(totalRecords, pageSize);

        if((modValue == 0) && (totalRecords > pageSize)) {
           offset = (totalRecords - pageSize);
        }
        generateScheduleListUI();
        return null;
    }

    /**
    * This will disable the next and end buttons
    */ 
    public Boolean getDisableNext() {
        return (offset + pageSize < totalRecords)? false: true;
    }

    /**
    * This will disable the previous and beginning buttons
    */ 
    public Boolean getDisablePrevious() {
        return (offset>0)? false: true;
    }

    /**
    * Get current page number 
    */ 
    public Integer getPageNumber() {
        return offset/pageSize + 1;
    }

    /**
    * Get total number of records pages 
    */ 
    public Integer getTotalPages() {
        if (math.mod(totalRecords, pageSize) > 0) {
            return totalRecords/pageSize + 1;
        } else {
            return (totalRecords/pageSize);
        }
    } 
}

Attribution
Source : Link , Question Author : sunny , Answer Author : highfive

Leave a Comment