[SalesForce] How to set page size for standard list controller without using extension class

I have a requirement in which I want to avoid involving extension class. The page shows list of account record present in org. I want to set page size for the list of accounts but I don't want to use extension class for this.
Here goes my code

<apex:page standardController="account" recordSetVar="accs" tabstyle="account" >      
<apex:form >
    <apex:param name="q" value="7" assignTo="{!PageSize}"/>
    <apex:pageBlock id="contacts_list">
        <apex:outputPanel style="float:right">
        Filter: 
        <apex:selectList value="{! filterId }" size="1">
            <apex:selectOptions value="{!listViewOptions}"/>
            <apex:actionSupport event="onchange" reRender="contacts_list"/>
        </apex:selectList><br /><br />
        </apex:outputPanel>

        <apex:pageBlockTable value="{!accs}" var="ac" >   
            <apex:column headerValue="name">
                <apex:inputField value="{!ac.name}" />  
            </apex:column>

            <apex:column headerValue="Account Number">
                <apex:inputField value="{!ac.AccountNumber}" />   
            </apex:column>

            <apex:column headerValue="account source">
                <apex:inputField value="{!ac.AccountSource}" />  
            </apex:column>

            <apex:column headerValue="Site">
                <apex:inputField value="{!ac.site}" />
            </apex:column>
        </apex:pageBlockTable>

        <!-- Pagination --><br />
        <table style="width: 100%"><tr> 
            <td>
                Page: <apex:outputText value=" {!IF(CEILING(ResultSize/PageSize)==0,0,PageNumber)} of {!CEILING(ResultSize/PageSize) }"/>               
            </td>            

            <td align="center">
                <apex:commandButton value="|<" action="{!first}" disabled="{!!hasPrevious}"  />         
                <apex:commandButton value="<<" action="{!previous}" disabled="{!!hasPrevious}" />   
                <apex:commandButton value=">>" action="{!next}" disabled="{!!hasNext}"/>            
                <apex:commandButton value=">|" action="{!last}" disabled="{!!hasNext}" />     
            </td>

            <td align="right">
                <apex:outputText value="Records per page :" rendered="{!CEILING(ResultSize/PageSize)!=0}" />
                <apex:selectList value="{! PageSize }" size="1" rendered="{!CEILING(ResultSize/PageSize)!=0}">
                    <apex:selectOption itemValue="5" itemLabel="5"/>
                    <apex:selectOption itemValue="20" itemLabel="20"/>
                    <apex:actionSupport event="onchange" reRender="contacts_list"/>
                </apex:selectList>
            </td>
            <td align="right">
                <apex:outputText value="No Records" rendered="{!CEILING(ResultSize/PageSize)==0}" /> 
            </td>           
        </tr></table>
        <apex:pageBlockButtons style="align:left;"> 
            <apex:commandButton value="save" action="{!save}" /> 
        </apex:pageBlockButtons>    
    </apex:pageBlock>
</apex:form>    

As you can see I have used <apex:param name="q" value="7" assignTo="{!PageSize}"/> but this is not serving my purpose. Please Help.Thanks in advance 🙂

Best Answer

You might want to make a note of this from the docs.

The <apex:param> component can only be a child of the following components: <apex:actionFunction> <apex:actionSupport> <apex:commandLink> <apex:outputLink> <apex:outputText>

I guess you are trying to default the pageSize to 7 without any button click.

Here is one approach wherein the contacts list is not visible on the page (style.display=hidden), on page load we set the page size to 7 via actionFunction and when that is complete (onComplete attribute of actionFunction is executed) we display the contacts_list pageBlock.

Wrap the <apex:param> with an actionFunction tag

  <apex:actionFunction  name="setPageSizeAsSeven" rerender="contacts_list" onComplete="onCompleteHandler()">
        <apex:param name="q" value="7" assignTo="{! PageSize }"/>
    </apex:actionFunction>

Wrap the contacts_list pageBlock tag with the following div

<div id="contactsListDiv" style="display:none;">

Invoke the actionFunction on pageLoad using javascript:

 <script> var previousOnload = window.onload; 
      window.onload = function() { if (previousOnload) { previousOnload(); }
     setPageSizeAsSeven(); } 
 function onCompleteHandler() { var contactsListDivElem = document.getElementById('contactsListDiv'); contactsListDivElem.style.display ='block'; }

This works but not sure if this is the best approach.

Related Topic