1

I added pagination code taken from here https://developer.salesforce.com/forums/?id=906F0000000DDAEIA4

When I click on the "next" button it doesn't re-render anything. The button is black so it's supposed to take me to the next page. This is my controller.

public class vf_CommunityPlanGoalPSetting_Cntrlr {

    //Set up variables and lists

    public List<Community_Plan_Goal_Setting__c> listGoalsCurrYear {get;set;}
    public List<Community_Plan_Goal_Setting__c> listGoalsPrevYears {get;set;}
    public String ft { get; set; }
    public String ccp { get; set; }
    public String fa { get; set; }
    public String fd { get; set; }
    public String ga { get; set; }
    public String gac { get; set; }    
    public String newRow {get;set;}
    public Integer rec { get; set; }
    public Integer tot { get; set; }


    public String currentYear = String.valueOf(System.Today().year()) ; 
    public String parentID;
    public   integer count= 0;
    public   integer LimitSize= 3;
    public   integer totalRecs = 0; 

    //Variable to hold add records to be deleted
    public List<Community_Plan_Goal_Setting__c> deleteList = new List<Community_Plan_Goal_Setting__c>();

    public vf_CommunityPlanGoalPSetting_Cntrlr(ApexPages.StandardController controller) 
    {    
        //Show warning that one must click on save/update button to save changes
        ApexPages.Message myMsgShowSaveInfo = new ApexPages.Message(ApexPages.Severity.INFO,'PLEASE NOTE: Any changes including adding or deleting rows only go into effect when the "Save/Update" button is clicked. Otherwise changes are lost when one leaves or refreshes the page.');
        ApexPages.addMessage(myMsgShowSaveInfo);
        listGoalsCurrYear = new Community_Plan_Goal_Setting__c[]{};
            listGoalsPrevYears = new Community_Plan_Goal_Setting__c[]{};    
                parentID = controller.getID();

       tot=totalRecs = [SELECT count() FROM Community_Plan_Goal_Setting__c
                                                   WHERE Community_Plan__r.id = :parentID];

        // get all records for all years        
        for(Community_Plan_Goal_Setting__c cpgs : [ SELECT id,Year__c,Funding_Type__c,Funding_Amount__c,Funding_Description__c,Funding_Comments__c,
                                                   Core_Current_Participation__c,
                                                   Goal_Amount__c,Goal_Amount_Comments__c,Community_Plan__c,Name,EditableOrReadOnly__c,Funding_Type_Description__c 
                                                   FROM Community_Plan_Goal_Setting__c
                                                   WHERE Community_Plan__r.id = :parentID
                                                   ORDER BY Year__c
                                                   LIMIT:limitsize 
                                                   OFFSET:count]){
                                                       // add all records to currYear section (top) which is editable

                                                       if(cpgs.Year__c == currentYear){
                                                           listGoalsCurrYear.add(cpgs);
                                                       }
                                                       else {
                                                           listGoalsPrevYears.add(cpgs);
                                                       }

                                                   }

    // Add how ever many previous years rows to the current year
     for (Community_Plan_Goal_Setting__c cpgsnew : listGoalsPrevYears)
        {
            //First check if any previous year data is entered in this years section
            //and if so, do not add more records as there were already done. 
            Boolean containsValue = Null;
                for (Community_Plan_Goal_Setting__c i : listGoalsCurrYear)
                {
                    for (Community_Plan_Goal_Setting__c j : listGoalsPrevYears)
                    {
                        String concat1 = listGoalsCurrYear[0].Funding_Description__c+listGoalsCurrYear[0].Funding_Comments__c+listGoalsCurrYear[0].Goal_Amount_Comments__c+listGoalsCurrYear[0].Funding_Type_Description__c;
                        String concat2 = listGoalsPrevYears[0].Funding_Description__c+listGoalsPrevYears[0].Funding_Comments__c+listGoalsPrevYears[0].Goal_Amount_Comments__c+listGoalsPrevYears[0].Funding_Type_Description__c;                        
                        if (concat1 == concat2)
                        {
                            containsValue = True;
                        }
                    }
                }
            if (containsValue <> True) AddRowForPreviousYearsData(cpgsnew.Funding_Type__c,cpgsnew.Core_Current_Participation__c,cpgsnew.Funding_Description__c,cpgsnew.Funding_Comments__c, cpgsnew.Goal_Amount_Comments__c);
        }

    }

    public Void AddRowForPreviousYearsData(String fundingType, String  coreCurrentParticipation, String  fundingDescription, String fundingComments, String goalAmountComments)
    {
        // Create new record for each row of previous years data
        Community_Plan_Goal_Setting__c cpgs = new Community_Plan_Goal_Setting__c(year__c = String.valueOf(System.Today().year()), Community_Plan__c = parentID,Funding_Type__c=fundingType,Core_Current_Participation__c=coreCurrentParticipation,Funding_Description__c=fundingDescription,Funding_Comments__c=fundingComments,Goal_Amount_Comments__c=goalAmountComments);

        //This next line allowes fields to be displayed in edit mode for new records
        if( cpgs.year__c == String.valueOf(System.Today().year()) ) newRow = 'nr';        

        listGoalsCurrYear.add(cpgs);

//        return null;
    }

    public pageReference AddRow()
    {
        // Create new record for each row
        Community_Plan_Goal_Setting__c cpgs = new Community_Plan_Goal_Setting__c(year__c = String.valueOf(System.Today().year()), Community_Plan__c = parentID);

        //This next line allowes fields to be displayed in edit mode for new records
        if( cpgs.year__c == String.valueOf(System.Today().year()) ) newRow = 'nr';        

        listGoalsCurrYear.add(cpgs);

        return null;
    }

    public pageReference SaveRecords()
    {
        ApexPages.Message myMsgSuccess = new ApexPages.Message(ApexPages.Severity.INFO,'Your changes were saved.');  
        //Save the list of records
        upsert listGoalsCurrYear;   

        // Delete records if necessary
        if(deleteList.size()>0)
        {
            delete deleteList;
        }

        // Show feedback
        ApexPages.addMessage(myMsgSuccess);

        return null;    
    }

    //Remove a record from the table.
    public pageReference removeRecord(){
        Integer indexVal = Integer.valueof(system.currentpagereference().getparameters().get('index'));

        //If existing then add it to the list to delete from the database
        if(listGoalsCurrYear[indexVal - 1].Id != null) deleteList.add(listGoalsCurrYear[indexVal - 1]);

        //Remove row from the table    
        listGoalsCurrYear.remove(indexVal - 1);   

        return null;
    }    

           public void updatePage() {
       listGoalsCurrYear.clear();
      limitsize=rec;
      listGoalsCurrYear=[SELECT id,Year__c,Funding_Type__c,Funding_Amount__c,Funding_Description__c,Funding_Comments__c,
                                                   Core_Current_Participation__c,
                                                   Goal_Amount__c,Goal_Amount_Comments__c,Community_Plan__c,Name,EditableOrReadOnly__c,Funding_Type_Description__c 
                                                   FROM Community_Plan_Goal_Setting__c
                                                   WHERE Community_Plan__r.id = :parentID

                                                   ORDER BY Year__c
                                                   LIMIT:rec
                                                    OFFSET:count];
    }
     public PageReference Firstbtn() {
     count=0;
        return null;
    }
      public PageReference prvbtn() {
      count=count-limitsize;
        return null;
    }

 public PageReference Nxtbtn() {
 count=count+limitsize;
        return null;
    }
 public PageReference lstbtn() {
 count= totalrecs - math.mod(totalRecs,LimitSize);
        return null;
    }


    public Boolean getNxt() {
        if((count+ LimitSize) > totalRecs)
       return true;
     else
       return false;

    }
     public Boolean getPrv() {
      if(count== 0)
          return true;
        else
          return false;


        }



}

This is the vf page

<apex:page standardController="Community_Yearly_Plan__c" extensions="vf_CommunityPlanGoalPSetting_Cntrlr" cache="false">

    <apex:form >
        <apex:pageblock >
            <apex:pageBlockSection columns="1" id="pbsCon" >
                <apex:outputPanel id="panelCon">
                    <div id="loading-curtain-div"/>

                    <apex:pageBlockTable id="pbtCon" value="{!listGoalsCurrYear}" var="cpgs">
                        <apex:variable value="{!0}" var="cnt"/>   
                        <apex:column headervalue="Action">
                            <apex:commandlink value="Remove" onclick="return validateFunction();" action="{!removeRecord}" immediate="true">
                                <!-- Pass the row number to the controller so that we know which row to remove -->
                                <apex:param name="index" value="{!cnt}"/>
                            </apex:commandlink> 
                            <apex:variable var="cnt" value="{!cnt+1}"/>  
                        </apex:column>  

                        <!--                        <apex:column value="{!cpgs.Year__c}" />                        
-->                        
                        <apex:column >
                            <apex:facet name="header">Year</apex:facet>                             
                            <apex:inputField id="hdr" value="{!cpgs.Year__c}" rendered="True" />
                        </apex:column>   
                        <apex:column >
                            <apex:facet name="header">Funding Type</apex:facet>                             
                            <apex:inputField id="ft" value="{!cpgs.Funding_Type__c}" rendered="True" />
                        </apex:column>   
                        <apex:column >
                            <apex:facet name="header">Description</apex:facet>                                                         
                            <apex:inputField id="ccp" value="{!cpgs.Core_Current_Participation__c}" rendered="{!IF(cpgs.Funding_Type__c!='Elective' && cpgs.Funding_Type__c!='Other','True','False')}" />
                            <apex:inputField id="ftd" value="{!cpgs.Funding_Type_Description__c}" rendered="{!IF(cpgs.Funding_Type__c='Elective' || cpgs.Funding_Type__c='Other','True','False')}" />                            

                        </apex:column>   
                        <!--                        <apex:column >
<apex:facet name="header">Funding Description</apex:facet>                                                         
<apex:inputField id="fd" value="{!cpgs.Funding_Description__c}" rendered="True" />
</apex:column>   
-->
                        <apex:column >
                            <apex:facet name="header">Goal Amount</apex:facet>                                                         
                            <apex:inputField id="ga" value="{!cpgs.Goal_Amount__c}" rendered="True" />
                        </apex:column>   
                        <apex:column >
                            <apex:facet name="header">Goal Amount Comments</apex:facet>                            
                            <apex:inputField id="gac" value="{!cpgs.Goal_Amount_Comments__c}" rendered="True" />
                        </apex:column>   
                        <apex:column >
                            <apex:facet name="header">Funding Amount</apex:facet>                                                         
                            <apex:inputField id="fa" value="{!cpgs.Funding_Amount__c}" rendered="True"  />
                        </apex:column>   

                    </apex:pageBlockTable>

                    <apex:commandButton style="align:center;" action="{!AddRow}" value="Add Funding/Goal" reRender="pbsCon" />
                    <apex:commandButton style="align:center;" action="{!SaveRecords}" value="Save/Update" reRender="pbsCon" />                    

                    <apex:pageMessage summary="PLEASE NOTE: Any changes including adding or deleting rows only go into effect when the 'Save/Update' button is clicked. Otherwise changes are lost when one leaves or refreshes the page." severity="warning" strength="3" />

                    <div align="center">
                        <apex:commandButton value="First Page" action="{!Firstbtn}" disabled="{!prv}" reRender="pbtCon"/>
                        <apex:commandButton value="PreviousPage" action="{!prvbtn}" disabled="{!prv}" reRender="pbtCon"/>
                        <apex:commandButton value="NextPage" action="{!Nxtbtn}" disabled="{!nxt}"  reRender="pbtCon"/>
                        <apex:commandButton value="LastPage" action="{!lstbtn}" disabled="{!nxt}" reRender="pbtCon"/>
                    </div>


                </apex:outputPanel>

                <apex:outputPanel id="panelCon2">
                    <div id="loading-curtain-div"/>

                    <apex:pageBlockTable id="pbtCon2" value="{!listGoalsPrevYears}" var="cpgs2">

                        <apex:column value="{!cpgs2.Year__c}" />                        
                        <apex:column >
                            <apex:facet name="header">Funding Type</apex:facet>                            
                            <apex:outputField value="{!cpgs2.Funding_Type__c}" rendered="{!IF(cpgs2.EditableOrReadOnly__c!='True','True','False')}" />                            

                        </apex:column>   
                        <apex:column >
                            <apex:facet name="header">Core Current Participation</apex:facet>                                                         
                            <apex:outputField value="{!cpgs2.Core_Current_Participation__c}" rendered="{!IF(cpgs2.EditableOrReadOnly__c!='True','True','False')}" />                            
                        </apex:column>   
                        <apex:column >
                            <apex:facet name="header">Funding Amount</apex:facet>                                                                                     
                            <apex:outputField value="{!cpgs2.Funding_Amount__c}" rendered="{!IF(cpgs2.EditableOrReadOnly__c!='True','True','False')}" />                            
                        </apex:column>   
                        <apex:column >
                            <apex:facet name="header">Funding Description</apex:facet>                                                                                     
                            <apex:outputField value="{!cpgs2.Funding_Description__c}" rendered="{!IF(cpgs2.EditableOrReadOnly__c!='True','True','False')}" />
                        </apex:column>   
                        <apex:column >
                            <apex:facet name="header">Goal Amount</apex:facet>                                                                                     
                            <apex:outputField value="{!cpgs2.Goal_Amount__c}" rendered="{!IF(cpgs2.EditableOrReadOnly__c!='True','True','False')}" />
                        </apex:column>   
                        <apex:column >
                            <apex:facet name="header">Goal Amount Comments</apex:facet>                                                        
                            <apex:outputField value="{!cpgs2.Goal_Amount_Comments__c}" rendered="{!IF(cpgs2.EditableOrReadOnly__c!='True','True','False')}" />
                        </apex:column>   

                    </apex:pageBlockTable>

                </apex:outputPanel>                
            </apex:pageBlockSection>
        </apex:pageblock>
    </apex:form>
    <script>    

    function validateFunction()
    {

        var r = confirm('Are you sure you want to remove this row?');
        if (r == true) 
        {
            return true
        } else 
        {
            return false;
        }
    }

    </script>
</apex:page>

1 Answer 1

0

The problem is when you update the value of offset you need to do this query again

function void updateresults()
{
 listGoalsCurrYear = new List<Community_Plan_Goal_Setting__c>();
 listGoalsPrevYears = new List<Community_Plan_Goal_Setting__c>(); 
     for(Community_Plan_Goal_Setting__c cpgs : [ SELECT id,Year__c,Funding_Type__c,Funding_Amount__c,Funding_Description__c,Funding_Comments__c,
                                                       Core_Current_Participation__c,
                                                       Goal_Amount__c,Goal_Amount_Comments__c,Community_Plan__c,Name,EditableOrReadOnly__c,Funding_Type_Description__c 
                                                       FROM Community_Plan_Goal_Setting__c
                                                       WHERE Community_Plan__r.id = :parentID
                                                       ORDER BY Year__c
                                                       LIMIT:limitsize 
                                                       OFFSET:count]){
                                                           // add all records to currYear section (top) which is editable

                                                           if(cpgs.Year__c == currentYear){
                                                               listGoalsCurrYear.add(cpgs);
                                                           }
                                                           else {
                                                               listGoalsPrevYears.add(cpgs);
                                                           }

                                                       }
  }

The example link you have given, they are using getter method and when you rerender the section getter method is called again. So you need to clear old value from list and put this query code in some method and call that method again method.

PS: This offset will work only with 200 records. If you more records but less then 10000 you can use StandardSetContoller if you have more then 10000 records you need to use custom pagination.

2
  • 1
    Very good. I added updatepage() in the next button and it works. Can you tell me why the back button doesn't get black when I click on next?
    – Apex N-u-b
    Commented Oct 9, 2016 at 15:11
  • @ApexN-u-b you need to rerender buttons as well. Try rerender panelCon Commented Oct 9, 2016 at 15:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .