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
hy.lim1.3897974166558203E12hy.lim1.3897974166558203E12 

need help on my pagination visualforce page

Hi, i have created a visualforce page with page navigation. I am able to shows the records but when i click next nothing happen. below is my controller:

public class sampleOutput5Cases {
    private List<Case> ca;
    private Contact cntact;
    public ApexPages.StandardSetController setController{get; set;}
     
    public sampleOutput5Cases (ApexPages.StandardController controller) {
        this.cntact= (Contact)controller.getRecord();
    }
    
    public List<Case> getCa()
    //public void getCa()
    {       
        ca = [Select id, CaseNumber, Origin, Status, Subject from Case where ContactId = :cntact.id];
        
        setController = new ApexPages.StandardSetController(ca); 
        setController.setPageSize(10);
        renderTable = true;
        
        return (List<Case>)setController.getRecords();
        //return ca;
        
    }
    
    public Boolean renderTable {
        get{
            if(renderTable == null)
                return false;
            return renderTable;
            
        }
        set;
    }
    
    public void Next()
    {
        setController.Next();
    }
    
    public void Previous()
    {
        setController.Previous();
    }
    
    /*public ApexPages.StandardSetController setController
    {
        get
        {   
            if(cntact == null){
                List<Case> caseList = new List<Case>();
                return new ApexPages.StandardSetController(caseList);
            }else
            
            return setController;
            
        }
        set;
    }*/
   
    public Boolean getHasNext()
    {
        if(setController==null)
            return false;
        return setcontroller.getHasNext();
    }
 
    public Boolean getHasPrevious()
    {
        if(setController==null)
            return false;       
        return setcontroller.getHasPrevious();
    }
   
    public String getPageRangeText()
    {
        if(setController == null)
            return '';
        else
        {
            Integer startP = ((setController.getPageNumber() - 1) * setController.getPageSize()) + 1;
            Integer endP;
            if(setController.getPageNumber()*setController.getPageSize() > setcontroller.getResultSize())
                endP = setcontroller.getResultSize();
            else
                endP = setController.getPageNumber()*setController.getPageSize();
            return 'Showing Records : ' + startP + ' - ' + endP + ' of total ' + setcontroller.getResultSize();
        }
    }
   
 
    public Boolean getShowPageRangeText()
    {
        if (setController==null)
            return false;       
        if(setcontroller.getResultSize() > 0)
            return true;
        else
            return false;
    } 
    
    
    
}

and my VF page:

<apex:page standardController="Contact" extensions="sampleOutput5Cases">
     
     
    <apex:includeScript value="/support/console/29.0/integration.js"/>
    <script type="text/javascript">
     
        // Opens a subtab when a related Case Number is clicked
      function OpenNewCaseDetailTab(theId, name) {
       sforce.console.openPrimaryTab(null, '/' + theId, true, name );
    }
     </script>
  
  <apex:form >
  <apex:pageBlock >
  <apex:pageBlockTable value="{!ca}" var="oCase" >
          <apex:column headerValue="CaseNumber" >
              <a HREF="#" onClick="OpenNewCaseDetailTab('{!oCase.Id}','{!oCase.CaseNumber}')" >{!oCase.CaseNumber}</a>
              <!--apex:commandLink value="{!oCase.CaseNumber}" action="/{!oCase.Id}" target="_blank"/-->
              <!--apex:commandlink style="margin-left:0px;color:#000;font-size:11px;" onclick="openPrimaryTab();return false;" value="{!oCase.CaseNumber}" /-->
          </apex:column>
          <apex:column value="{!oCase.Subject}"/>
          <apex:column value="{!oCase.Origin}"/>      
         <apex:column value="{!oCase.Status}"/>      
 </apex:pageBlockTable>
 </apex:pageBlock>
 <apex:commandButton id="btnPrev" value="Prev" action="{!Previous}" rendered="{!HasPrevious}" />
    <apex:commandButton id="btnNext" value="Next" action="{!Next}" rendered="{!HasNext}" />
    <br />
    <br />
    <apex:outputLabel id="opPageRangeText" styleClass="brandTertiaryBgr pbSubheader tertiaryPalette" rendered="{!ShowPageRangeText}" value="{!PageRangeText}" />
 </apex:form>
     
    
</apex:page>

Jean-NoelJean-Noel
You'll need to add a rerender clause with yout button to refresh the page.
AshlekhAshlekh
Hi,

You can see this blog  post for standard controller
http://forceguru.blogspot.in/2011/04/pagination-in-salesforce.html

http://www.redpointsolutions.com/add-pagination-to-your-visualforce-pages-using-the-soql-offset-clause

Thanks

Carolina Ruiz MedinaCarolina Ruiz Medina
Hi hy.lim1.3897974166558203E12,

Your problem is in your controller class. You don't really need the rerender parameter in your VF component. 

Your method getCa is alwys doing the same behaviour, it means that you will query always and you will asign the first 2 always. Then what you need to do is something like this:
public List<Case> getCa()
{  
     if(ca!=null && ca.isEmpty())
        {
    // only enter here the first time :-) 
            ca = [Select id, CaseNumber, Origin, Status, Subject from Case where ContactId = :cntact.id];
            
            setController = new ApexPages.StandardSetController(ca); 
            setController.setPageSize(2);
            renderTable = true;
            
            return (List<Case>)setController.getRecords();
         }
         else{
          // here is where you should enter once the list is already inicialized and also you SetController.
            return (List<Case>)setController.getRecords();
         }
}
;)

Hope it helps.

Hasta pronto,
Carolina.
hy.lim1.3897974166558203E12hy.lim1.3897974166558203E12
Hi Caoline,

I tried your method but it says 'System.NullPointerException: Attempt to de-reference a null object' at line 16
Carolina Ruiz MedinaCarolina Ruiz Medina
Dear hy.lim1.3897974166558203E12,

You are right , there is one line that I didn't send to you, as it was in the part where initialize variables.

private List<Case> ca = new List<Case>();  --> instead private List<Case> ca; what you have at the moment. 

However, you can also check that is that line if you "debug" the code using monitoring debug logs. The log will let you know the exact line that is giving you the error therfore you will be able to fix the issue. But I'm really confident is that. 


Hope it helps, let me know :) 

Regards,
Carolina.