Wrapper Class
A Wrapper class is a class whose instances are collection of other objects. It is used to display different objects on a Visual Force page in same table.
Wrapper Class Example
Wrapper class for displaying Checkbox and String Data types in a single table.
Create a Class with the name “WrapperIntStringDisplayClass” :
public with sharing class WrapperIntStringDisplayClass {
// Creating lists for the object Testing__c and DataLoadTest__c.
List<Testing__c> lsttest = new List<Testing__c>();
List<DataLoadTest__c> lstdlt = new List<DataLoadTest__c>();
// Creating List for Wrapper class
public List<wrapper> lstw = new List<wrapper>();
// Get method calling from PageBlockTable and return the list of wrapper to Table
public List<wrapper> getLstwrapperIntString() {
lsttest = [select name,city__c from Testing__c];
lstdlt = [select country__c,phone__c from DataLoadTest__c];
for(Integer i=0;i<lstdlt.size();i++){
lstw.add(new wrapper(lsttest[i].name,lsttest[i].city__c,lstdlt[i].country__c,lstdlt[i].phone__c));
}
return lstw;
}
// Wrapper Class Construction
public class wrapper{
public String Tname{get;set;}
public String Tcity{get;set;}
public String Dcountry{get;set;}
public String Dphone{get;set;}
// Wrapper class constructor
public wrapper(String Tname,String Tcity,String Dcountry,String Dphone){
this.Tname=Tname;
this.Tcity=Tcity;
this.Dcountry=Dcountry;
this.Dphone=Dphone;
}
}
}
Create a VF Page with the controller
<!-- Creating this page for dispaly Testing__c and DataLoadTesting__c object in single table with check box -->
<apex:page sidebar="false" controller="WrapperIntStringDisplayClass">
<apex:form >
<apex:pageBlock >
<apex:pageBlockSection >
<apex:pageBlockTable value="{!lstwrapperIntString}" var="w">
<apex:column headervalue="Action">
<apex:inputcheckbox />
</apex:column>
<apex:column headervalue="TestingName">
{!w.Tname}
</apex:column>
<apex:column headerValue="TestingCity">
{!w.Tcity}
</apex:column>
<apex:column headervalue="DataLoadCountry">
{!w.Dcountry}
</apex:column>
<apex:column headerValue="DataLoadPhone">
{!w.Dphone}
</apex:column>
</apex:pageBlockTable>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
very helpful article. Thanks.
please show the output of above example
Give more examples for wrapper class
in constructor you have pass parameter……. from where you pass this parameter…. not getting well…………. plz explain code…
public class vAccountContactWrapper
{
Public List vAccList = [Select Name,Phone,Industry, (select FirstName,LastName from Contacts) from Account where Id in (select AccountId from Contact)];
Public List vAccConList = new List();
public List getvAccConList()
{
for(Account vAcc:vAccList)
{
for(Contact vCon:vAcc.Contacts)
{
vAccConList.Add(new vAccCon(vAcc.Name,vAcc.Phone,vAcc.Industry,vCon.FirstName,vCon.LastName));
}
}
return vAccConList;
}
public class vAccCon
{
public String vAccName{get;set;}
Public String vAccPhone{get;set;}
Public String vAccIndustry{get;set;}
Public String vConFirstName{get;set;}
Public String vConLastName{get;set;}
public vAccCon( String vAccName, String vAccPhone, String vAccIndustry, String vConFirstName, String vConLastName)
{
this.vAccName = vAccName;
this.vAccPhone = vAccPhone;
this.vAccIndustry= vAccIndustry;
this.vConFirstName= vConFirstName;
this.vConLastName =vConLastName;
}
}
}
public with sharing class WrapperIntStringDisplayClass{
List lsttest = new List();
List lstdlt = new List();
public List lstw = new List();
public List getLstwrapperIntString(){
lsttest = [select name,city__c from Testing__c];
lstdlt = [select country__c, phone__c from DataLoadTest__c];
for(Integer i=0; i<lstdlt.size(); i++){
lstw.add(new wrapper(lsttest[i].name, lsttest[i].city__c, lsttest[i].country__c, lstdlt[i].phone__c));
}
return lstw;
}
public class wrapper{
public String Tname{get;set;}
public String Tcity{get;set;}
public String Dcountry{get;set;}
public String DPhone{get;set;}
/*line 26*/. public wrapper(String Tname,String Tcity,String Dcountry,String Dphone){
this.Tname = Tname:
this.Tcity = Tcity;
this.Dcountry = Dcountry;
this.Dphone = Dphone;
}
}
}
in this code error is:
line 26: unexpected syntax: 'missmatch input' expecting semicoln.
line 11: variable does not exist: country__c
Please give me solution for above code?
why do we require a constructor for wrapper we can manage without that too.
by using wrapper class how we insert both sobjects Account and Contact at a time (by using visual force page)
how to write please the solution
Create some records on both custom objects to see the visualforce page contents