Pagination On Visual force Page Using StandardSetController
Requirement: Need to create a visual force page and add processed the records on selection of check boxes
Solution:
Class Name:
public class ContactController {
Public class WrapperContactCls
{
public Contact con{get;set;}
public boolean isSelected{get;set;}
public WrapperContactCls(Contact c,boolean isSelected)
{
this.con = c;
this.isSelected = isSelected;
}
}
public String size { get; set; }
//This is Our collection of the class/wrapper objects WrapperContactCls
Public List<WrapperContactCls> wrapperlist;
Public Integer noOfRecords{get; set;}
// Create a new Map to verify whether the contact is already added in the Map
Map <id,Contact> MapOfSelectedcontactIDToContactObj = 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<WrapperContactCls> getContact()
{
getSelectedContact();
// Initilaize the list to add the selected contact
wrapperlist = new List <WrapperContactCls>();
for(Contact cc : (List<contact>)Setcon.getRecords())
{
if( MapOfSelectedcontactIDToContactObj .ContainsKey(cc.id))
{
wrapperlist.add (new WrapperContactCls(cc,true));
}
else
{
wrapperlist.add(new WrapperContactCls(cc,false));
}
}
return wrapperlist;
}
public void getSelectedContact(){
if(wrapperlist!=null)
{
for(WrapperContactCls objWrap:wrapperlist)
{
if(objWrap.isSelected == true)
{
MapOfSelectedcontactIDToContactObj.put(objWrap.con.id,objWrap.con);
}
else
{
MapOfSelectedcontactIDToContactObj.remove(objWrap.con.id);
}
}
}
}
public void proceedRecords()
{
display = true;
getSelectedContact();
selectedList = MapOfSelectedcontactIDToContactObj.values();
}
public integer pageNumber
{
get
{
return Setcon.getPageNumber();
}
set;
}
}
VF Page
<apex:page controller="ContactController ">
<apex:form id="frmId">
<apex:pageblock >
<apex:pageBlockButtons location="Top">
<apex:commandButton value="Processed" action="{!proceedRecords}" /><br/>
<apex:outputText >Total Record :{!noOfRecords} </apex:outputText>
</apex:pageBlockButtons>
<apex:pageblocktable value="{!Contact}" var="objCon" >
<apex:column headerValue="Select Contact">
<apex:inputCheckbox value="{!objCon.isSelected }"/>
</apex:column>
<apex:column value="{!objCon.con.Name}"/>
<apex:column value="{!objCon.con.Accountid}"/>
<apex:column value="{!objCon.con.Email}"/>
<apex:column Value="{!objCon.con.MobilePhone}"/>
</apex:pageblocktable>
<apex:pageBlockButtons location="Bottom">
<apex:commandButton value="First" action="{!Setcon.First}" reRender="frmId" />
<apex:commandButton value="Previous" action="{!Setcon.Previous}" reRender="frmId" />
<apex:commandButton value="Next" action="{!Setcon.Next}" reRender="frmId" />
<apex:commandButton value="Last" action="{!Setcon.Last}" reRender="frmId" />
</apex:pageBlockButtons>
<apex:outputText >Page Number {!pageNumber} </apex:outputText>
</apex:pageblock>
<apex:pageblock >
<apex:outputPanel rendered="{!display}">
<apex:pageblocktable value="{!selectedList}" var="wrapObj" >
<apex:column value="{!wrapObj.Name}"/>
<apex:column value="{!wrapObj.Accountid}"/>
<apex:column value="{!wrapObj.Email}"/>
<apex:column value="{!wrapObj.MobilePhone}"/>
<apex:column value="{!wrapObj.LeadSource}"/>
</apex:pageblocktable>
</apex:outputPanel>
</apex:pageblock>
</apex:form>
</apex:page>
SnapShot:
Thanks,
Sumit Shukla
sumitshukla.mca@gmail.com
9711055997
Comments
Post a Comment