+ Start a Discussion
raj kiranraj kiran 

Standard set controller dynamic page sizing

Hi All,

I am using the standard set controller to dispaly a list of records and do the pagination.
Here i need to set the page size of the dispalyed records per page dynamically. eg like an selectlist values from vf
like select records/page dropdown to value having 10,20,50 and upon the selection by submiting some(update button) the number of records per page shoud get updated.

 i have seen a post here :
http://saramorgan.net/2014/11/11/pagination-and-the-standardsetcontroller-no-custom-controller-required/
 which has the same funcationality i am looking for however he has used the standard opptunity controller to acheive it. in my case i am using the custom controller and using the standard set controller to acheive it.

can any one let me know how can i acheive the dynamic paging size from vf using the stadard set controller ?

Regards
Raj
 
Best Answer chosen by raj kiran
MithunPMithunP
Hi Raj,

Below is the code with selectlist.
 
Page
=====

<apex:page controller="Pagination_min">
    <apex:form >
        <apex:pageBlock id="pb">
            <apex:pageBlockTable value="{!Accounts}" var="a">
                <apex:column value="{!a.Name}"/>
                <apex:column value="{!a.Type}"/>
                <apex:column value="{!a.BillingCity}"/>
                <apex:column value="{!a.BillingState}"/>
                <apex:column value="{!a.BillingCountry}"/>
            </apex:pageBlockTable>
            <apex:panelGrid columns="7">
                <apex:commandButton status="fetchStatus" reRender="pb" value="|<" action="{!setCon.first}" disabled="{!!setCon.hasPrevious}" title="First Page"/>
                <apex:commandButton status="fetchStatus" reRender="pb" value="<" action="{!setCon.previous}" disabled="{!!setCon.hasPrevious}" title="Previous Page"/>
                <apex:commandButton status="fetchStatus" reRender="pb" value=">" action="{!setCon.next}" disabled="{!!setCon.hasNext}" title="Next Page"/>
                <apex:commandButton status="fetchStatus" reRender="pb" value=">|" action="{!setCon.last}" disabled="{!!setCon.hasNext}" title="Last Page"/>
                <apex:outputText >{!(setCon.pageNumber * size)+1-size}-{!IF((setCon.pageNumber * size)>noOfRecords, noOfRecords,(setCon.pageNumber * size))} of {!noOfRecords}</apex:outputText>
                <apex:commandButton status="fetchStatus" reRender="pb" value="Refresh" action="{!refresh}" title="Refresh Page"/>
                <apex:actionFunction name="show" action="{!refresh}" reRender="pb" />
                <apex:selectList value="{!size}"  multiselect="false" size="1" onchange="show()">
                      <apex:selectOptions value="{!items}"/>
                   </apex:selectList>      
                <apex:outputPanel style="color:#4AA02C;font-weight:bold">
                    <apex:actionStatus id="fetchStatus" startText="Fetching..." stopText=""/>
                </apex:outputPanel>
            </apex:panelGrid>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Class
==============

public with sharing class Pagination_min {
    Public Integer noOfRecords{get; set;}
    Public Integer size{get;set;}
    public boolean test = false;
    public ApexPages.StandardSetController setCon {
        get{
            if(setCon == null){
                if(test == false){
                size = 10;
                }
                string queryString = 'Select Name, Type, BillingCity, BillingState, BillingCountry from Account order by Name';
                setCon = new ApexPages.StandardSetController(Database.getQueryLocator(queryString));
                setCon.setPageSize(size);
                noOfRecords = setCon.getResultSize();
                test = true;
            }
            return setCon;
        }set;
    }
     
    Public List<Account> getAccounts(){
        List<Account> accList = new List<Account>();
        for(Account a : (List<Account>)setCon.getRecords())
            accList.add(a);
        return accList;
    }
     
    public pageReference refresh() {
        setCon = null;
        getAccounts();
        setCon.setPageNumber(1);
        return null;
    }
    public List<SelectOption> getItems() {
            List<SelectOption> options = new List<SelectOption>();
            options.add(new SelectOption('10','10'));
            options.add(new SelectOption('20','20'));
            options.add(new SelectOption('30','30'));
            return options;
        }
}



Best Regards,
Mithun.

All Answers

Chidambar ReddyChidambar Reddy
Hi Raj,

you can set page size for standardsetcontroller
https://www.salesforce.com/us/developer/docs/pages/Content/apex_ApexPages_StandardSetController_methods.htm
using standardsetcontroller.setPageSize(Integer pageSize)
here, you can use this pagesize as a variable and assign it from the dropdown and refresh records to the standardsetcontroller


Thanks 
Choose it as best answer if it has helped you
MithunPMithunP
Hi Raj,

Below is the code with selectlist.
 
Page
=====

<apex:page controller="Pagination_min">
    <apex:form >
        <apex:pageBlock id="pb">
            <apex:pageBlockTable value="{!Accounts}" var="a">
                <apex:column value="{!a.Name}"/>
                <apex:column value="{!a.Type}"/>
                <apex:column value="{!a.BillingCity}"/>
                <apex:column value="{!a.BillingState}"/>
                <apex:column value="{!a.BillingCountry}"/>
            </apex:pageBlockTable>
            <apex:panelGrid columns="7">
                <apex:commandButton status="fetchStatus" reRender="pb" value="|<" action="{!setCon.first}" disabled="{!!setCon.hasPrevious}" title="First Page"/>
                <apex:commandButton status="fetchStatus" reRender="pb" value="<" action="{!setCon.previous}" disabled="{!!setCon.hasPrevious}" title="Previous Page"/>
                <apex:commandButton status="fetchStatus" reRender="pb" value=">" action="{!setCon.next}" disabled="{!!setCon.hasNext}" title="Next Page"/>
                <apex:commandButton status="fetchStatus" reRender="pb" value=">|" action="{!setCon.last}" disabled="{!!setCon.hasNext}" title="Last Page"/>
                <apex:outputText >{!(setCon.pageNumber * size)+1-size}-{!IF((setCon.pageNumber * size)>noOfRecords, noOfRecords,(setCon.pageNumber * size))} of {!noOfRecords}</apex:outputText>
                <apex:commandButton status="fetchStatus" reRender="pb" value="Refresh" action="{!refresh}" title="Refresh Page"/>
                <apex:actionFunction name="show" action="{!refresh}" reRender="pb" />
                <apex:selectList value="{!size}"  multiselect="false" size="1" onchange="show()">
                      <apex:selectOptions value="{!items}"/>
                   </apex:selectList>      
                <apex:outputPanel style="color:#4AA02C;font-weight:bold">
                    <apex:actionStatus id="fetchStatus" startText="Fetching..." stopText=""/>
                </apex:outputPanel>
            </apex:panelGrid>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Class
==============

public with sharing class Pagination_min {
    Public Integer noOfRecords{get; set;}
    Public Integer size{get;set;}
    public boolean test = false;
    public ApexPages.StandardSetController setCon {
        get{
            if(setCon == null){
                if(test == false){
                size = 10;
                }
                string queryString = 'Select Name, Type, BillingCity, BillingState, BillingCountry from Account order by Name';
                setCon = new ApexPages.StandardSetController(Database.getQueryLocator(queryString));
                setCon.setPageSize(size);
                noOfRecords = setCon.getResultSize();
                test = true;
            }
            return setCon;
        }set;
    }
     
    Public List<Account> getAccounts(){
        List<Account> accList = new List<Account>();
        for(Account a : (List<Account>)setCon.getRecords())
            accList.add(a);
        return accList;
    }
     
    public pageReference refresh() {
        setCon = null;
        getAccounts();
        setCon.setPageNumber(1);
        return null;
    }
    public List<SelectOption> getItems() {
            List<SelectOption> options = new List<SelectOption>();
            options.add(new SelectOption('10','10'));
            options.add(new SelectOption('20','20'));
            options.add(new SelectOption('30','30'));
            return options;
        }
}



Best Regards,
Mithun.
This was selected as the best answer
raj kiranraj kiran
MithunP,

Thanks that helped me a lot.
raj kiranraj kiran
Hi Mithun,

I have used the standard set controller and implented the pagination to the results. however i am looking forward to implmenet the search for the presented data also but i dont have any idea for search. Can you share any information on this.
Karl MitchellKarl Mitchell
So this is how pagination is implemented with this platform? Can I use this methof when the number of my website http://susanterrain1.wixsite.com/studentscommunity increases or for free site constructors there are inbuilt code already? Anyway, worth trying.