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
Harjeet SinghHarjeet Singh 

Pagination not working plz help

 I have a vf page and controller.
My pagination is not working properly.I need to display 20 recors per page and then clicking on next will show next 20 and so on.similarly for previous also.I have put 4 command button for my pagination Next,Last,Previous,First. When I am clicking on next the same 20 records are appearing.It means only buttons are appearing on UI but there functionality is not working.
Below are the codes for my page and controller.
public class HarjeetWrapper{
             private Set<Id> selectedContactIds;
            public ApexPages.StandardSetController setCon {
            get {
                if(setCon == null) {
                    setCon = new ApexPages.StandardSetController(Database.getQueryLocator(
                      [select Id, Name,csord__Status__c, csord__Account__c from csord__Subscription__c where csord__Account__c=:accountid ]));
                             }
                        return setCon;
                }
        set;
    }
    public List<csord__Subscription__c > getWrapSubscriptionList() {
         
          setCon.setpagesize(20);
         setCon.setpageNumber(1);
         return (List<csord__Subscription__c >) setCon.getRecords();
    }
    public Integer getPageNumber(){
 
        return this.setCon.getPageNumber();
 
    }
    

 


   Public Integer getTotalPages(){
 
        Decimal totalSize = this.setCon.getResultSize();
        Decimal pageSize = this.setCon.getPageSize();
 
        Decimal pages = totalSize/pageSize;
 
        return (Integer)pages.round(System.RoundingMode.CEILING);
     }
 

    //Our collection of the class/wrapper objects wrapAccount
    public List<wrapSubscription> wrapSubscriptionList {get; set;}
    public List<csord__Subscription__c> selectedSubscriptions{get;set;}
    public Boolean isCancel{get;set;}
    public string accountid;
    
    
    public HarjeetWrapper(){
      accountid=ApexPages.currentPage().getParameters().get('id');
        if(wrapSubscriptionList == null) {
           wrapSubscriptionList= new List<wrapSubscription>();
            for(csord__Subscription__c s: [select Id, Name,csord__Status__c, csord__Account__c from csord__Subscription__c where csord__Account__c=:accountid LIMIT 20]) {
                // As each Account is processed we create a new wrapAccount object and add it to the wrapAccountList
                wrapSubscriptionList.add(new wrapSubscription(s));
            }
        }
    }
    
    
    public void processSelected() {
    selectedSubscriptions = new List<csord__Subscription__c>();
 
        for(wrapSubscription wrapSubscriptionObj : wrapSubscriptionList) {
            if(wrapSubscriptionObj.selected == true) {
                selectedSubscriptions.add(wrapSubscriptionObj.sub);
            }
        }
    }
    
    public PageReference cancel(){
        return new PageReference('/'+accountid);
    }
    
    public void next(){

    if(this.setCon.getHasNext())
        this.setCon.next();

}

 public void previous(){

    if(this.setCon.getHasPrevious())
        this.setCon.previous();

}


                         
 
    // This is our wrapper/container class. In this example a wrapper class contains both the standard salesforce object Account and a Boolean value
    public class wrapSubscription {
        public csord__Subscription__c sub {get; set;}
        public Boolean selected {get; set;}
 
        public wrapSubscription(csord__Subscription__c s) {
            sub = s;
            selected = false;
        }
    }
}
<apex:page controller="HarjeetWrapper" sidebar="false" showHeader="false">
    <script type="text/javascript">
        function selectAllCheckboxes(obj,receivedInputID){
            var inputCheckBox = document.getElementsByTagName("input");
            for(var i=0; i<inputCheckBox.length; i++){
                if(inputCheckBox[i].id.indexOf(receivedInputID)!=-1){
                    inputCheckBox[i].checked = obj.checked;
                }
            }
        }
    </script>
    <apex:form >
   
        <apex:pageBlock title="Select Subscription"  >
            <apex:pageBlockButtons location="bottom">
                <apex:commandButton value="Proceed " action="{!processSelected}" />
                <apex:commandButton value="Cancel" action="{!cancel}" immediate="true" />
            </apex:pageBlockButtons>
       <apex:pageblockSection title="Subscriptions" collapsible="false" columns="1" >
             <apex:pageBlockTable value="{!wrapSubscriptionList}" var="subWrap">
                    <apex:column >
                        <apex:facet name="header">
                            <apex:inputCheckbox onclick="selectAllCheckboxes(this,'inputId')" />
                        </apex:facet>
                        <!--<apex:inputCheckbox value="{!subWrap.selected}" id="inputId"/>-->
                                <apex:inputCheckbox value="{!subWrap.selected}" id="inputId" disabled="{!NOT(subWrap.sub.csord__Status__c=='Active')}" />
                    </apex:column>
                   
                    <!--<apex:column value="{!subWrap.sub.Name}" />-->
                    <apex:column headerValue="Subscription Name" >
                        <!--<apex:commandLink value="{!subWrap.sub.Name}" action="/{!subWrap.sub.Id}"/>-->
                       <apex:outputLink value="/{!subWrap.sub.Id}" target="_blank"> {!subWrap.sub.Name} </apex:outputLink>
                   </apex:column>
                     <apex:column value="{!subWrap.sub.csord__Status__c}" />
                    <apex:column value="{!subWrap.sub.csord__Account__c}" />
                </apex:pageBlockTable>
                </apex:pageblockSection>
             <apex:commandButton rendered="{!setCon.hasPrevious}" value="first" action="{!setCon.first}"/>
            <apex:commandButton rendered="{!setCon.hasPrevious}" value="Previous" action="{!setCon.previous}"/>
            <apex:outputLabel value=" (page {!pageNumber} of {!totalPages})"/>
            <apex:outputText rendered="{!(setCon.pageNumber * setCon.pageSize) < setCon.ResultSize}" value="{!setCon.pageNumber * setCon.pageSize} Of {!setCon.ResultSize}"></apex:outputText>
            <apex:outputText rendered="{!(setCon.pageNumber * setCon.pageSize) >= setCon.ResultSize}" value="{!setCon.ResultSize} Of {!setCon.ResultSize}"></apex:outputText>
            <apex:commandButton rendered="{!setCon.hasNext}" value="next" action="{!setCon.next}"/>
            <apex:commandButton rendered="{!setCon.hasNext}" value="last" action="{!setCon.last}"/>

     <apex:pageBlock />
               
               
                <apex:pageblockSection title="Selected Subscriptions" collapsible="false" columns="1" >
                   <apex:pageBlockTable value="{!selectedSubscriptions}" var="c" id="table2" title="Selected Subscriptions" width="100%">
                    <!--<apex:column value="{!c.Name}" headerValue="Subscription Name"/>-->
                    <apex:column headerValue="Subscription Name" >
                        <apex:commandLink value="{!c.Name}" action="/{!c.Id}" target="_blank"/>
                      </apex:column>
                    <apex:column value="{!c.csord__Status__c}" headerValue="Status"/>
                    <apex:column value="{!c.csord__Account__c}" headerValue="Account"/>
                </apex:pageBlockTable>
           

            </apex:pageblockSection>
        </apex:pageBlock>
    </apex:form>
 
</apex:page>


I  am attaching screen snaps to refer that also
screen shot
I have businees demo on Friday and need to be resolve this before that at any cost.
Many thanks in advance.
Hope this time also you people came up with solution for me...
 
Ravikant kediaRavikant kedia
Below is the good link to understand pagination it will help you
https://forcespider.wordpress.com/2013/02/06/dynamiccolumn/
Vijay NagarathinamVijay Nagarathinam
Use the following sample code, It will working good.
 
<apex:page controller="opportunityListCon">
    <apex:form >
        <apex:pageBlock >
            <apex:pageBlockTable value="{!Opportunities}" var="o">
                <apex:column value="{!o.name}"/>
                <apex:column headerValue="Amount">
                  <apex:inputField value="{!o.Amount}"/>
                </apex:column>
            </apex:pageBlockTable>
           
            <apex:commandButton rendered="{!setCon.hasPrevious}" value="first" action="{!setCon.first}"/>
            <apex:commandButton rendered="{!setCon.hasPrevious}" value="Previous" action="{!setCon.previous}"/>
            <apex:outputText rendered="{!(setCon.pageNumber * setCon.pageSize) < setCon.ResultSize}" value="{!setCon.pageNumber * setCon.pageSize} Of {!setCon.ResultSize}"></apex:outputText>
            <apex:outputText rendered="{!(setCon.pageNumber * setCon.pageSize) >= setCon.ResultSize}" value="{!setCon.ResultSize} Of {!setCon.ResultSize}"></apex:outputText>
           
            <apex:commandButton rendered="{!setCon.hasNext}" value="next" action="{!setCon.next}"/>
           
            <apex:commandButton rendered="{!setCon.hasNext}" value="last" action="{!setCon.last}"/>
        </apex:pageBlock>
    </apex:form>
</apex:page>
 
public class opportunityListCon 
{ 
    public ApexPages.StandardSetController setCon {
        get {
            if(setCon == null) {
                setCon = new ApexPages.StandardSetController(Database.getQueryLocator(
                      [select name,closedate,amount from Opportunity]));
            }
            return setCon;
        }
        set;
    }

    // Initialize setCon and return a list of records
   
    public List<Opportunity> getOpportunities() {
         setCon.setpagesize(20);
         return (List<Opportunity>) setCon.getRecords();
    }
   
}

 
Harjeet SinghHarjeet Singh
Hi Vijay,
the code which you are reffereing to i applied in my codes but still not able to get the result.So i pasted my codes here in order to get fine tune it.
Please look into my code and fine tune it wherever possible as i am not able to find out the blocker.
 
sandeep sankhlasandeep sankhla
Hi Harjeet,

You can refer this code for pagination, this will work perfect, this is paginatore class which you can extend from your controller adn initalize the necessary variables and then you will be able to use it..
 
public abstract class Paginator 
{
    public Integer FirstPage 	{get; set;} // first row number in the list.
    public Integer PageSize 	{get; set;} // number of rows per page. //previously currentPageCount
    public Integer LastPage 	{get; set;} // row index of the first record the in the last page
    public Integer page			{get; set;}
    public Integer TotalRows; // total rows in all pages.
   
    /*!
      *  @MethodName: previousPage
      *  @param None: None.
      *  @Description: This method will Navigate to previous page.
    */
    public void previousPage() 
    {
        if(FirstPage - PageSize >= 0) 
        {
            FirstPage -= PageSize ;
           	page--;
        }
       
    }
    
    /*!
      *  @MethodName: lastPage
      *  @param None: None.
      *  @Description: This method will Navigate to last page.
    */
    public void lastPage() 
    {    
        FirstPage = calcLastPage();
    }
    
    /*!
      *  @MethodName: calcLastPage
      *  @param None: None.
      *  @Description: This method will calculate page number of last page.
    */
    private Integer calcLastPage() 
    {
        if(TotalRows >= PageSize ) 
        {
            page=Math.mod(TotalRows, PageSize) == 0 ? TotalRows/PageSize : TotalRows/PageSize + 1;
            return ((page-1)*PageSize);
        }
        else 
        {
            return 0;
        }
    } 
    
    /*!
      *  @MethodName: nextPage
      *  @param None: None.
      *  @Description: This method will navigate to next page.
    */
    public void nextPage() 
    {
        if(TotalRows== 0 || PageSize == 0) return;
        
        if(FirstPage + PageSize < TotalRows) 
        {
            FirstPage += PageSize ;
            page++;
        }
    }
    
    /*!
      *  @MethodName: firstPage
      *  @param None: None.
      *  @Description: This method will navigate to firstPage.
    */
    public void firstPage() 
    {
        page=1;
        FirstPage = 0;
    }
}

you can extend this calss from your main controller and then you can initalize the variables from your controller constructor

call this initalize method from constructor to inatlize
 
private void initPagination() 
	{
	    page=1;//initally page is 1. 
     	
	    FirstPage = 0; //first page index is initially 0
	    
	   	TotalRows = lstWrapperClass.size();// total rows to be spanned across pages
	    
	    PageSize = 10;// no. of rows you want to show per page
	    
	    LastPage = Math.mod(TotalRows, PageSize) == 0 ? TotalRows / PageSize : TotalRows / PageSize + 1;// calculate the index of last page
        	
	}

in total rows you can assign the list which contains the records...

you can additionally add this method in controller to go to any page directly
 
/*!
      *  @MethodName: goToPage
      *  @param None: None.
      *  @Description: This method will redirect to the page number which is entered.
    */
	public void goToPage() 
	{
		LastPage = math.mod(lstWrapperClass.size(), PageSize) == 0 ? (lstWrapperClass.size()/ PageSize) : (lstWrapperClass.size()/ PageSize) + 1;
		
     	if(page > LastPage || page == 0) 
     	{
			page = LastPage;
			FirstPage = ((page-1) * PageSize);
		} 
      	else 
			FirstPage = ((page-1) * PageSize);
	}

now in page you can simply iterate this wrapper class and add button as below adn this will work perfect

page code
<apex:outputpanel rendered="{!(lstWrapperClass.Size > 0)}" id="mainOPTPanelId">
			<!-- 	Start pageblock -->
			<apex:pageblock id="pageBlockId" title="Quick Add List">
				<apex:pageBlockTable id="pageBlockTableId" value="{!lstWrapperClass}" var="CompanyList" first="{!FirstPage}" rows="{!PageSize}" >
					<apex:column width="10px">
						<apex:facet name="header" >
							<apex:inputCheckBox id="headerChkBox" onclick="checkAllRecords('{!$Component.pageBlockTableId}',this);">
<!-- 								<apex:actionSupport id="selectAll" event="onclick"  action="{!selectAllLists}" rerender="pageBlockId" />  -->
							</apex:inputCheckBox>
						</apex:facet>
	 						<apex:inputCheckBox title="check"  value="{!CompanyList.isChecked}" onclick="callCheckIfAllCheckBoxChecked();" rendered="{!!CompanyList.isReadOnly}"/>
	 						<apex:inputCheckBox title="check_ReadOnly" value="{!CompanyList.isChecked}" disabled="true" rendered="{!CompanyList.isReadOnly}"/>
<!-- 	 						<apex:actionSupport event="onclick" action="{!updateHeaderCheckbox}" rerender="pageBlockId"  /> -->
	 				</apex:column>
					<apex:column headerValue="List" style="text-align: left;">
						<apex:outputtext value="{!CompanyList.objCompanyList.Name}"/>
					</apex:column>
				</apex:pageBlockTable>
			<br/>
			</apex:pageblock>
			<!-- 	End page block -->
			<div class="floatR pagination paginationFix " style="width: 100%; text-align:center;">
            	<div style=" width:100%  text-align:center;margin-right: -41px" >
                	<apex:outputPanel id="paginationPanel">
                    	<table width="100%">
                        	<tr width="100%">
                        		<td width="25%"></td>
	                            <td width="8%">
	                                <apex:outputPanel rendered="{!(page==1)}">
	                                	<span style="font-size: 1.8em;font-weight: bold;">
				                          &laquo;
				                      	</span>
				                      	<span style="font-size: 1.8em;font-weight: bold;">
				                          &lsaquo; 
				                      	</span>
	                                </apex:outputPanel>
	                                <apex:outputPanel rendered="{!!(page==1)}">
	                                    <apex:commandLink action="{!firstPage}" style="text-decoration:none;font-size:1.8em;font-weight: bold;" rerender="paginationPanel, pageBlockId, pageBlockTableId"> 
	                                        &laquo;
	                                    </apex:commandLink>&nbsp; 
	                                    <apex:commandLink action="{!previousPage}" style="text-decoration:none;font-size:1.8em;font-weight: bold;" rerender="paginationPanel, pageBlockId, pageBlockTableId"> 
	                                        &lsaquo; 
	                                    </apex:commandLink>
	                                </apex:outputPanel>
	                            </td>
                            	<td width="8%">
	                            	<apex:outputPanel rendered="{!IF(lastPage == 1,true,false)}">
	                            		<input type="text" value="{!page}" style="width:30px;height:15px;border:0.1em solid gray;" disabled="disabled"/>
	                            	</apex:outputPanel>
	                                <apex:inputText rendered="{!IF(lastPage > 1,true,false)}" value="{!page}" onKeyPress="return onlyNum(event);" id="pageNumberId" size="1" style="width:30px;height:15px;border:0.1em solid gray;"/> 
									&nbsp;of {!lastPage}
	                                       <apex:actionFunction name="goToPage" action="{!goToPage}" rerender="paginationPanel,  pageBlockId, pageBlockTableId" />
                                </td>
                                <td width="8%">
	                                       <apex:outputPanel rendered="{!(page == lastPage)}">
		                                       	<span style="font-size: 1.8em;font-weight: bold;">
						                          &rsaquo;
						                      	</span>
						                      	<span style="font-size: 1.8em;font-weight: bold;">
						                          &raquo; 
						                      	</span>
	                                       </apex:outputPanel>
	                                       <apex:outputPanel rendered="{!!(page == lastPage)}"> 
	                                           <apex:commandLink action="{!nextPage}" style="text-decoration:none;font-size:1.8em;font-weight: bold;" rerender="paginationPanel,  pageBlockId, pageBlockTableId"> 
	                                        		&rsaquo;
	                                    	 </apex:commandLink>&nbsp;
		                                    <apex:commandLink action="{!lastPage}" style="text-decoration:none;font-size:1.8em;font-weight: bold;" rerender="paginationPanel, pageBlockId, pageBlockTableId"> 
		                                        &raquo;
		                                    </apex:commandLink> 
	                                </apex:outputPanel>
                            	</td>
                            	<td width="30%"></td>
                        	 </tr>
                    	</table>
                    	<script>
							document.getElementById('{!$Component.pageBlockId.pageBlockTableId:headerChkBox}').checked = false;
							callCheckIfAllCheckBoxChecked();
                		</script>
                	</apex:outputPanel>

above page code is mine old code..you can simply refer how i used list in repeat and show teh buttons for pagination..

please check above code and let me know if that work for you...

P.S. If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.

Thanks,
Sandeep
Salesforce Certified Developer 

 
Harjeet SinghHarjeet Singh
Thanks Sandeep for such a quick response.I will but i am afraid after implementing your code will my other functionality still exists.
Apart from checkbox greyed out and pagination i need to perform proceed action with the help of a proceed button which will display all the selected rows and a cancel button which will redirect the user to home page from where the whole process started.these things are working absolutely fine in current scenario.
sandeep sankhlasandeep sankhla
Hi Harjeet,

My above code is only for Pagination..it will not affect anything in your code..but you should implement it correctly...it is simple one .you can implement and use this..I will see if I will have time I will try to check your code adn will modify that if needed so you can use that code only..

but you can try this also and it will not affect anything..

just take backup and try..if you are unable to implement properly then you can revert everything as it is now..

Thanks,
Sandeep
Harjeet SinghHarjeet Singh
Sandeep i m using your code now,but as i stated tomorrow i am having business demo so will require everything according to my requirement.
For me in current scenario everything is working except that paginations button,but if i implement your code then i will end up with only pagination.
Thanks a ton for your non-stop help.I really appreciate your help.
sandeep sankhlasandeep sankhla
Hi harjeet,

Sure! You can keep everything as it..because except pagination everything is working for you..After you rdemo you can implement pagination or may be bfore that also you will be able to figure out the pagination issue

All the best for demo

Will check if i can find time and solve your current pagination logic..

Thanks,
Sandeep
Harjeet SinghHarjeet Singh
Thanks man..
Pls update my code according to my requiremnt if possible and you get time.