Wrapper class is a very powerful concept that can be used in multiple scenarios. Putting aside the word "Wrapper" it is nothing but just a class.Just like what we have in a class ,we have methods,Properties etc in a wrapper class too.Here is an use case for wrapper class
Requirement : A Visual force page has to display all the Contacts available for an Account .Contacts should be listed out in the table row wise(default being column wise) . Only created "date"(Strip the time part) of the contact should be displayed .
Sample page:
Apex Code :
Visual force page :
Result :
Huh!! now please don't catch a bug here that no values are displayed for phone. I admit i missed them..Pretty tired ...accept my apology and take it for granted :) :)
Power of wrapper is seamless .It can be used to process only the records that are selected in vf page.Click on the link for example.
http://wiki.developerforce.com/index.php/Wrapper_Class
You Want More Information about Salesforce.com Please
Requirement : A Visual force page has to display all the Contacts available for an Account .Contacts should be listed out in the table row wise(default being column wise) . Only created "date"(Strip the time part) of the contact should be displayed .
Sample page:
Apex Code :
- public class displayContacts
- {
- //Get the respective Account id
- ID accountId;
- //Store the wrapped Contacts
- public List<contactWrapper> conWrapper{get;set;}
- //COnstructor
- public displayContacts()
- {
- accountId= string.escapeSingleQuotes(ApexPages.currentPage().getParameters().get('id'));
- //Initialize list
- conWrapper = new List<contactWrapper>();
- for(Contact tempCon :[select FirstName,LastNAme,Email,phone,createddate from contact where accountid=:accountid])
- {
- //create the object for the wrapper class
- contactWrapper tempWrapper = new contactWrapper(tempCon);
- conwrapper.add(tempWrapper);
- }
- } //End of Constructor
- //Wrapper class for Contact
- public class contactWrapper
- {
- public String contactName{get;set;}
- public String createdDate{get;set;}
- public string phoneC{get;set;}
- public string emailC{get;set;}
- public contactWrapper(Contact con)
- {
- contactName=con.FirstName+' '+con.LastName;
- createdDate=con.createddate.month() +'/'+ con.createddate.day() +'/'+ con.createddate.year();
- phoneC=con.phone;
- emailC=con.email;
- }//End of Wrapper constructor
- }//End of wrapper class
- } //End of Class
Visual force page :
- <apex:page controller="displayContacts">
- <apex:pageBlock>
- <apex:pageBlockSection title="Contacts Available">
- <apex:dataTable value="{!conWrapper}" var="item1" border="1" columnsWidth="65px,100px" rowClasses="odd,even" styleClass="tableClass">
- <apex:column width="200px">
- <table width="250" border="0"> <tr><td><b>Name :</b></td><td>{!item1.contactName }</td></tr> <tr><td><b>Phone :</b></td><td>{!item1.phonec}</td></tr> <tr><td><b>Email :</b></td><td>{!item1.emailc} </td></tr> <tr><td><b>Created Date :</b></td><td>{!item1.createdDate} </td></tr> </table> </apex:column>
- </apex:dataTable>
- </apex:pageBlockSection>
- </apex:pageBlock>
- </apex:page>
Result :
Huh!! now please don't catch a bug here that no values are displayed for phone. I admit i missed them..Pretty tired ...accept my apology and take it for granted :) :)
Power of wrapper is seamless .It can be used to process only the records that are selected in vf page.Click on the link for example.
http://wiki.developerforce.com/index.php/Wrapper_Class
You Want More Information about Salesforce.com Please
nice article
ReplyDelete