Wrapper Class

Wrapper Class – A wrapper or container class is a class, a data structure, or an abstract data type whose instances are collections of other objects.

public with sharing class MultiDataDisplayCls {
    public List<Employee__c> lstE{get;set;}
    public List<Account> lstA{get;set;}
    public List<wrapInner> lstWI{get;set;}
   
    public MultiDataDisplayCls(){
        lstE = new List<Employee__c>();
        lstA = new List<Account>();
       
lstE = [select id,name,Employee_Name__c,employee_city__c from Employee__c limit 3];   
lstA = [select id,name,phone from Account limit 3];
               
        lstWI = new List<wrapInner>();
       
        for(Integer i=0;i<lstE.size();i++){
            wrapInner objWI = new wrapInner();
            objWI.objE = lstE[i];
objWI.objA = lstA[i];
            lstWI.add(objWI);
        }
       
    }
    public class wrapInner{ // step1
        public Employee__c objE{get;set;} // setp2
        public Account objA{get;set;}
    }
}

<apex:page showHeader="false" controller="MultiDataDisplayCls">
  <apex:form >
      <apex:pageBlock >
          <apex:pageBlockTable value="{!lstWI}" var="w">
              <apex:column headerValue="Employee ID" value="{!w.objE.Name}"/>
              <apex:column headerValue="Account Name" value="{!w.objA.Name}"/>
              <apex:column headerValue="Employee Name" value="{!w.objE.Employee_Name__c}"/>
              <apex:column headerValue="Account Phone" value="{!w.objA.Phone}"/>
          </apex:pageBlockTable>
      </apex:pageBlock>
  </apex:form>
</apex:page>

wrap records with a check box

  1. <apex:page controller="EmpDeleteCls">
  2.      <apex:form >
  3.         <!-- <c:companyLogo /> -->
  4.          <apex:pagemessages ></apex:pagemessages>
  5.          <apex:pageBlock >
  6.              <apex:pageBlockTable value="{!lstWI}" var="w">
  7.                  <apex:column headerValue="Action">
  8.                      <apex:inputcheckbox value="{!w.ischecked}"/>
  9.                  </apex:column>
  10.                  <apex:column headerValue="Employee ID" value="{!w.objE.Name}"/>
  11.                  <apex:column headerValue="Employee Name" value="{!w.objE.Employee_Name__c}"/>
  12.              </apex:pageBlockTable>
  13.              <apex:commandButton value="Delete" action="{!doDelete}"/>
  14.          </apex:pageBlock>
  15.        <!--  <c:companyLogo /> -->
  16.      </apex:form>
  17. </apex:page>
  1. public with sharing class EmpDeleteCls {

  2.     public List<Employee__c> lstEToDelete = new List<Employee__c>();
  3.     public Integer selCount =0;
  4.     public PageReference doDelete() {
  5.         try{
  6.             for(wrapEmployee objWE : lstWI){
  7.                 if(objWE.ischecked == true){
  8.                     selCount++;
  9.                     lstEToDelete.add(objWE.objE);
  10.                 }
  11.             }
  12.             if(selCount == 0){
  13.                 // http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_pages_message.htm
  14.                 ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.INFO, 'No Records Selected to Delete....');
  15.                 apexpages.addMessage(myMsg);
  16.                 return null;
  17.             }
  18.             
  19.             
  20.             if(lstEToDelete.size()>0)
  21.                 delete lstEToDelete;
  22.             return (new pagereference('/apex/wrapper2').setredirect(true));
  23.       }
  24.       catch(exception e){
  25.           return null;
  26.       }
  27.         
  28.     }


  29.     public List<Employee__c> lstE{get;set;}
  30.     public List<wrapEmployee> lstWI{get;set;}
  31.     public EmpDeleteCls(){
  32.         lstE = new List<Employee__c>();
  33.         lstE = [select id,name,Employee_Name__c from Employee__c];
  34.         
  35.         lstWI = new List<wrapEmployee>();
  36.         for(Integer i=0;i<lstE.size();i++){
  37.             wrapEmployee objWE = new wrapEmployee();
  38.             objWE.ischecked = false;
  39.             objWE.objE = lstE[i];
  40.             lstWI.add(objWE);
  41.         }
  42.     }
  43.     
  44.     
  45.     
  46.     public class wrapEmployee{
  47.         public Employee__c objE{get;set;}
  48.         public boolean ischecked{get;set;}
  49.     }
  50.     
  51. }

Comments