anusha(salesforce developer)

Thursday 23 June 2016

Display more than 3000 records in a 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);
        }
    } 
}

You can actually display 3000 records on a page if you need. The below code may not be exact and may need minor changes.
Controller:
public class MultiExample
{
  public List<MultiWrapper> wraps{get; set;}

  public MultiExample()
  {
    wraps = new List<MultiWrapper>();
    wraps.add(wrap);
    MultiWrapper wrap = new MultiWrapper();
    for (Account acc: [SELECT Id FROM Account LIMIT 3000])
    {
      if (wrap.size() == 1000)
      {
        wrap = new MultiWrapper();
        wraps.add(wrap);
      }
      wrap.addAcc(acc);
    }
  }

  public class MultiWrapper
  {
    public List<Account> accs{get; set;}

    public MultiWrapper()
    {
      accs = new List<Account>();
    }

    public void addAcc(Account acc)
    {
      accs.add(acc);
    }
  }
}
VF Page:
<apex:repeat value="{!wraps}" var="wrap">
  <apex:pageBlockTable value="{!wrap.accs}" var="{!acc}">
    <apex:column>
      <apex:outputField value="{!acc.Id}" />
    </apex:column>
  </apex:pageBlockTable>
</apex:repeat>
The downside is that it will create a new table every 1000 records, but they should be right on top of each other so it shouldn't be a big deal.

No comments:

Post a Comment