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
Peter BölkePeter Bölke 

Pagination for custom controller

Hello,

i try to implement in a Visualforce-Page but i cant get it work.  It seems that pagination is for StandardControllers or Extensions only. Or there any change to get it with fully custom controller?

Page:
<apex:page title="Production Figures" id="thePage" sidebar="false" docType="html-5.0" applyBodyTag="true" controller="CtrlProduction">
	
	<head>
		<link type="text/css" rel="stylesheet" href="{!$Resource.CtrlProductionCss}" />
		<link type="text/css" rel="stylesheet" href="{!URLFOR($Resource.jQueryResource, '/artDialog/skins/twitter.css')}" />
		<style type="text/css">
			th {
				white-space: inherit !important;
			}
		</style>
		
		
		<apex:includeScript value="{!URLFOR($Resource.jQueryResource, '/jquery1.11/jquery-1.11.0.min.js')}" />
		<script src="{!URLFOR($Resource.jQueryResource, '/glob-cultures/globalize.js')}" type="text/javascript"></script>
		<script src="{!URLFOR($Resource.jQueryResource, lc.jsPath)}" type="text/javascript"></script>
		
		<script src="{!URLFOR($Resource.jQueryResource, '/Utils.js')}"></script>
		<script src="{!$Resource.CtrlProductionJs}" type="text/javascript"></script>
		<script src="{!URLFOR($Resource.jQueryResource, '/artDialog/artDialog.min.js')}"></script>
        <script src="{!URLFOR($Resource.jQueryResource, '/artDialog/artDialog.plugins.min.js')}"></script>
		<script type="text/javascript">
			;function setFocusOnLoad(){}
        	var CUSTOM_LABEL = {
        		'save' : '{!$Label.Save_Message}', 
        		'cancel' : '{!$Label.Production_Cancel_Message}',
        		'nopm': '{!$Label.No_Permission}'
        	};
			addListener(window, 'load', initDocument, {'locale':'{!lc.locale}'});
        </script>

	</head>
	<body>
		<apex:form >
			<apex:sectionHeader title="{!$Label.Production_Figures}" />
			<apex:inputHidden id="isGroup" value="{!Competence}"/>
			<apex:actionFunction name="save" action="{!save}" oncomplete="saveComplete()" reRender="bodyContentId, themsg" />
			
			<div id="dmsgid"><apex:pageMessages id="themsg"/></div>
			<div id="pageLoading" class=""></div>
			<div id="popupLoading" style="display: none;"></div>

			<div class="content" id="contentAll">
			
				<div class="headerButtonDiv tc">
					<input type="button" class="commonButton Btn_save" value="{!$Label.Save_Button}" />
					<input type="button" class="commonButton Btn_cancel" value="{!$Label.Cancel_Button}" />
				</div>
				
				<div class="tableDiv">
					<c:CompProductionBody pageCtrl="{!this}" id="comptableid"></c:CompProductionBody>
				</div>
				
				<div class="footerButtonDiv tc">
					<input type="button" class="commonButton Btn_save" value="{!$Label.Save_Button}" />
					<input type="button" class="commonButton Btn_cancel" value="{!$Label.Cancel_Button}" />
				</div>
				
			</div>
			            <apex:panelGrid columns="7">
                <apex:commandButton status="fetchStatus" reRender="pb" value="|<" action="{!first}" disabled="{!Not(hasPrevious)}" title="First Page"/>
                <apex:commandButton status="fetchStatus" reRender="pb" value="<" action="{!previous}" disabled="{!Not(hasPrevious)}" title="Previous Page"/>
                <apex:commandButton status="fetchStatus" reRender="pb" value=">" action="{!next}" disabled="{!Not(hasPrevious)}" title="Next Page"/>
                <apex:commandButton status="fetchStatus" reRender="pb" value=">|" action="{!last}" disabled="{!Not(hasPrevious)}" title="Last Page"/>
                <apex:outputText >{!(pageNumber * size)+1-size}-{!IF((pageNumber * size)>noOfRecords, noOfRecords,(pageNumber * size))} of {!noOfRecords}</apex:outputText>
                <apex:commandButton status="fetchStatus" reRender="pb" value="Refresh" action="{!refresh}" title="Refresh Page"/>
                <apex:outputPanel style="color:#4AA02C;font-weight:bold">
                    <apex:actionStatus id="fetchStatus" startText="Fetching..." stopText=""/>
                </apex:outputPanel>
            </apex:panelGrid>
		</apex:form>
	</body>

</apex:page>
Component with data:
<apex:component >
    <apex:attribute name="pageCtrl" type="CtrlProduction" required="true" description="the root page controller reference" />
    <apex:outputPanel layout="block" id="bodyContentId">
        <apex:inputHidden id="saveJsonList" value="{!pageCtrl.saveJson}"/>
        <div class="divcomponentbox">
            <table class=" table_fix oemTable" border="0" cellspacing="0" cellpadding="0">
                <colgroup class="currentColgroup">
                    <apex:repeat var="column" value="{!pageCtrl.columnlist}">
                        <col class="{!column.name}" />
                    </apex:repeat>             
                </colgroup> 
                
                <thead>
                    <tr>
                        <th class="thheight tc blcolor btcolor thbr"></th>
                        <th class="thheight tc btcolor brcolor" colspan="{!pageCtrl.columnyearmap['currentyear'].colspan}">{!pageCtrl.columnyearmap['currentyear'].value}</th>
                        <th class="thheight tc space"></th>
                        <th class="thheight tc btcolor" colspan="{!pageCtrl.columnyearmap['nextyear'].colspan}">{!pageCtrl.columnyearmap['nextyear'].value}</th>
                    </tr>
                    <tr >
                        <apex:repeat var="column" value="{!pageCtrl.columnlist}">
                            <th class="thheight1 thbr tc {!column.name}"> 
                                <span class="field" >{!column.displayName}</span>
                            </th>
                        </apex:repeat>
                    </tr>
                </thead>
                
                
                <tbody id="actbodyid">
                    <!-- START each account list -->
                    <apex:variable value="{!0}" var="indextr" />
                    
                    <apex:repeat var="act" value="{!pageCtrl.accountlist}">
                        <tr data-value="{!act.valueJson}" data-id="{!act.accid}" data-cid="{!act.currentpfid}" data-nid="{!act.nextpfid}" 
                            data-name="{!act.name}" class="{!if(mod(indextr, 2) == 1, 'trOddStyle', '')} trbodycls">
                            <apex:variable value="{!indextr + 1}" var="indextr" />
                            <apex:variable value="{!0}" var="index" />
                            <apex:repeat var="column" value="{!pageCtrl.columnlist}">
                                <td class="tdheight thbr {!if(index = 0, '', 'tr')} ov {!column.name}" data-field="{!column.fieldname}">
                                    <div>
                                        <!-- //updated by Steven Qiao<rui.qiao@oinio.com> 2016-09-27 -->
                                        <span class="textspan">{!if(index = 0, act.name, '')}</span>
                                        <apex:inputText rendered="{!column.isInput}" styleClass="{!column.name}_input tr inputw" />
                                        <span class="percentspan"></span>
                                    </div>
                                </td>
                                <apex:variable value="{!index + 1}" var="index" />
                            </apex:repeat>
                        </tr>
                    </apex:repeat>
                    <!-- END each account list -->
                </tbody>
            </table>
        </div>

    </apex:outputPanel>
</apex:component>


Can someone give me an advise?

thanks
Peter
 
Khan AnasKhan Anas (Salesforce Developers) 
Hi Peter,

Greetings to you!

You can use pagination with a custom controller. There are various options for pagination. Through Offset: we can use max 2000 records. Through StandardSetController: we can paginate up to 10,000 records. So, you can use the StandardSetController pagination which allows you to paginate over large datasets that have up to 10,000 records.

Below is the sample code for StandardSetController pagination which I have tested in my org and it is working fine. Kindly modify the code as per your requirement.

Visualforce:
<apex:page controller="SoqlPaginationSetController">
    <apex:form >
    	<apex:pageBlock id="pb" title="Contacts" >
            <apex:pageBlockTable value="{!Contacts}" var="c" id="pt">
            	<apex:column value="{!c.Name}" />
                <apex:column value="{!c.Phone}" />
            </apex:pageBlockTable>
            <apex:commandButton value="First" action="{!ssc.first}" disabled="{!!ssc.hasPrevious}" reRender="pt,pb" />
            <apex:commandButton value="Previous" action="{!ssc.previous}" disabled="{!!ssc.hasPrevious}" reRender="pt,pb" />
            <apex:commandButton value="Next" action="{!ssc.next}" disabled="{!!ssc.hasNext}" reRender="pt,pb" />
            <apex:commandButton value="End" action="{!ssc.last}" disabled="{!!ssc.hasNext}" reRender="pt,pb" />
        </apex:pageBlock>
	</apex:form>
</apex:page>

Controller:
public class SoqlPaginationSetController {
    
    public List<Contact> lstContact;
    Public Integer noOfRecords {get;set;}
    Public Integer size {get;set;}
    
    public SoqlPaginationSetController(){
        lstContact = new List<Contact>();
        size = 10;
    }
    
    public ApexPages.StandardSetController ssc{
        get{
        	if(ssc == null){
            	String queryResult = 'SELECT Id, Name, Phone FROM Contact ORDER BY Name';
            	ssc = new ApexPages.StandardSetController(Database.getQueryLocator(queryResult));
            	ssc.setPageSize(size);
            	noOfRecords = ssc.getResultSize();
            }
            return ssc;
        }
        set;
    }
    
    public List<Contact> getContacts(){
        return (List<Contact>)ssc.getRecords();
    }
}

Please refer to the below links which might help you further.

https://salesforce.stackexchange.com/questions/42702/how-to-paginate-10000-records-soql-offset-and-standardsetcontroller-too-limit

https://hisrinu.wordpress.com/2012/01/09/pagination-using-standardsetcontroller/

https://developer.salesforce.com/page/Paginating_Data_for_Force.com_Applications

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas