You are using an older browser that might negatively affect how this site is displayed. Please update to a modern browser to have a better experience. Sorry for the inconvenience!

Pagination in Salesforce with Maintaining Selected Records


By: Santhosh

Pagination in salesforce can be achieved , by using Standard Set Controller in salesforce; however, we cannot maintain the stage of the selected records in the pagination. The following code explains how we can maintain the state of the selected records in pagination, and also how to process those selected records.

Take a demo at this page.

Public class WrapperContactWrapper

{

Public Contact con{get;set;}

Public boolean bool{get;set;}

public WrapperContactWrapper(Contact c,boolean bool)

{

this.con = c;

this.bool = bool;

}

}

Public class ContactTable

{

public String size { get; set; }

//This is Our collection of the class/wrapper objects WrapperContactWrapper

Public List<WrapperContactWrapper> wrapperlist;

Public Integer noOfRecords{get; set;}

// Create a new Map to verify whether the contact is already added in the Map

Map <id,Contact> SelectedcontactMap = new Map <id,Contact>();

public boolean display{get;set;}

public list<Contact> selectedList {get;set;}

// instantiate the StandardSetController from a query locator

public ApexPages.StandardSetController Setcon

{

get

{

if(Setcon == Null)

{

Setcon = new ApexPages.StandardSetController(Database.getQueryLocator([Select Name,Accountid,Email,MobilePhone,LeadSource from Contact]));

// sets the number of records in each page set

setCon.setpagesize(10);

noOfRecords = setCon.getResultSize();

}

return Setcon;

}

set;

}

//Returns a list of wrapper objects for the sObjects in the current page set

Public List<WrapperContactWrapper> getContact()

{

getSelectedContact();

// Initilaize the list to add the selected contact

wrapperlist = new List <WrapperContactWrapper>();

for(Contact cc : (List<contact>)Setcon.getRecords())

{

if( SelectedcontactMap .ContainsKey(cc.id))

{

wrapperlist.add (new WrapperContactWrapper(cc,true));

}

else

{

wrapperlist.add(new WrapperContactWrapper(cc,false));

}

}

return wrapperlist;}

public void getSelectedContact(){

if(wrapperlist!=null)

{

for(WrapperContactWrapper wr:wrapperlist)

{

if(wr.bool == true)

{

SelectedcontactMap.put(wr.con.id,wr.con); // Add the selected contact id in to the SelectedcontactMap.

}

else

{

SelectedcontactMap.remove(wr.con.id); // If you uncheck the contact, remove it from the selectedcontactMap

}

}

}

}

public void clickMe()

{

display = true;

getSelectedContact();


selectedList = SelectedcontactMap.values();

}

public integer pageNumber

{

get

{

return Setcon.getPageNumber();

}

set;

}

}

VF Page

<apex:page controller="ContactTable">

<apex:form id="myform">

<apex:pageblock >

<apex:pageBlockButtons location="Top">

<apex:commandButton value="Processed" action="{!clickMe}" />

</apex:pageBlockButtons>

<!-- In our table, we are displaying the Contact records -->

<apex:pageblocktable value="{!Contact}" var="cc" >

<apex:column headerValue="Choose Contact">

<!-- This is our selected Boolean property in our wrapper class -->

<apex:inputCheckbox value="{!cc.bool}"/>

</apex:column>

<!-- This is how we access the contact values within our Contact container/wrapper -->

<apex:column value="{!cc.con.Name}"/>

<apex:column value="{!cc.con.Accountid}"/>

<apex:column value="{!cc.con.Email}"/>

<apex:column Value="{!cc.con.MobilePhone}"/>

<apex:column value="{!cc.con.LeadSource}"/>

</apex:pageblocktable>

<apex:pageBlockButtons location="Bottom">

<apex:commandButton value="First" action="{!Setcon.First}" reRender="myform" />

<apex:commandButton value="Previous" action="{!Setcon.Previous}" reRender="myform" />

<apex:commandButton value="Next" action="{!Setcon.Next}" reRender="myform" />

<apex:commandButton value="Last" action="{!Setcon.Last}" reRender="myform" />

</apex:pageBlockButtons>

<apex:outputText >Page Number {!pageNumber} </apex:outputText>

</apex:pageblock>

<apex:pageblock >

<apex:outputPanel rendered="{!display}">

<apex:pageblocktable value="{!selectedList}" var="w" >

<apex:column value="{!w.Name}"/>

<apex:column value="{!w.Accountid}"/>

<apex:column value="{!w.Email}"/>

<apex:column value="{!w.MobilePhone}"/>

<apex:column value="{!w.LeadSource}"/>

</apex:pageblocktable>

</apex:outputPanel>

</apex:pageblock>

</apex:form>

</apex:page>

 

OutPut:

 

pagination in salesforce
what is pagination in salesforce
pagination in salesforce

Reference Link:

https://www.salesforce.com/us/developer/docs/pages/Content/apex_pages_standardsetcontroller.htm