Wrapper Class in Salesforce
What is the Wrapper Class?
A wrapper or container class is a class, a data structure, or an abstract data type which contains different objects or collection of objects as its members.
For example, we want to delete one or more than one account using checkbox and button. So wrapper class looks like shown below-
Apex Class
public class Checking
{
public List<ContactWrp> cw {get;set;}
public Checking()
{
for(Contact con : [select id,name from Contact])
{
cw.add(new ContactWrp(con,false));
}
}
public PageReference deleteContact()
{
List<Contact> deleteList = new List<Contact>();
for(ContactWrp crp : cw)
{
if(crp.chk)
{
deleteList.add(crp.c);
}
}
delete deleteList;
PageReference pg = new PageReference('/apex/contactChecking');
pg.setRedirect(true);
return pg;
}
public class ContactWrp
{
public Contact c {get;set;} //Contact name and
public boolean chk {get;set;} //Checkbox
public ContactWrp(Contact ct, boolean b)
{
this.c = ct;
this.chk = b;
}
}
}
Visualforce Page (Name of the page is contactChecking)
<apex:page sidebar="false" showHeader="false" controller="Checking">
<apex:form >
<apex:repeat value="{!cw}" var="v">
<apex:inputCheckbox value="{!v.chk}"/> {!v.c.name} <br/>
</apex:repeat>
<apex:commandButton value="Delete" action="{!deleteContact}"/>
</apex:form>
</apex:page>
Visualforce Page (Name of the page is contactChecking)
<apex:page sidebar="false" showHeader="false" controller="Checking">
<apex:form >
<apex:repeat value="{!cw}" var="v">
<apex:inputCheckbox value="{!v.chk}"/> {!v.c.name} <br/>
</apex:repeat>
<apex:commandButton value="Delete" action="{!deleteContact}"/>
</apex:form>
</apex:page>
Comments
Post a Comment