+ Start a Discussion
Shephali SwarnkarShephali Swarnkar 

how to write wrapper for multiple objects

Hello Friends.
The below is my wrapper class for one object(contact).but i want to get the same thing for one more object (basic__c) having fields(name,address1__c,state__c) within the same controller and this list i want to appear in same VF page.how to achieve this.??? 

public class Wrapcontactlist 
{
    public list<wrapcontact> wrapcontactList { get; set; }
    public list<contact> selectedContacts{get;set;}        
      public Wrapcontactlist ()
     {
          wrapcontactList =new list<wrapcontact>();
          for(contact a:[select id,name,phone from contact limit 10])
       {
           wrapcontactlist.add(new wrapcontact(a));
       }
  }
      
  //##THIS IS WRAPPER CLASS
   // contact and checkbox taken in wrapper class   
   public class wrapcontact
{    
    public contact accn{get;set;}
   // public boolean isSelected{get;set;}     
       public wrapcontact(contact a)
    {    
         accn=a;
         isselected=false;
       }
   }
}

Best Answer chosen by Shephali Swarnkar
Vishal_GuptaVishal_Gupta
Hi Shephali,

you can either use same wrapper or can create a seprate wrapper class for your second object, just like below code :

public class Wrapcontactlist 
{
    public list<wrapcontact> wrapcontactList { get; set; }
    public list<wrapcontact> wrapBasicList { get; set; }
    public list<contact> selectedContacts{get;set;}        
      public Wrapcontactlist ()
     {
          wrapcontactList =new list<wrapcontact>();
          for(contact a:[select id,name,phone from contact limit 10])
       {
           wrapcontactlist.add(new wrapcontact(a));
       }
       
        wrapBasicList = new  list<wrapcontact>();
        for(basic__c  b:[select id,name from basic__c limit 10])
       {
           wrapBasicList.add(new wrapcontact(b));
       }

  }
      
  //##THIS IS WRAPPER CLASS
   // contact and checkbox taken in wrapper class   
   public class wrapcontact
  {    
       public contact accn{get;set;}
       public Basic__c vBasic{get;set;}
     // public boolean isSelected{get;set;}     
       public wrapcontact(contact a)
       {    
         accn=a;
        isselected=false;
       }
       public wrapcontact(Basic__c vBas)
       {    
           vBasic=vBas;
          isselected=false;
       }

   }
}

Please let me know if I can help you more.

Thanks,
Vishal

 

All Answers

Abhishek BansalAbhishek Bansal
Hi Shephali,

Use the below code :

public class Wrapcontactlist
{
    public list<wrapcontact> wrapcontactList { get; set; }
    public List<BasisWrappper> basisWrapperList {get;set;}
    public list<contact> selectedContacts{get;set;}        
      public Wrapcontactlist ()
     {
          wrapcontactList =new list<wrapcontact>();
          for(contact a:[select id,name,phone from contact limit 10])
       {
           wrapcontactlist.add(new wrapcontact(a));
       }
       
       basisWrapperList = new List<BasisWrappper>();
       for(basic__c basis : [Select name,address1__c,state__c from basic__c]) {
            basisWrapperList.add(new BasisWrappper(basis));
       }
  }
      
  //##THIS IS WRAPPER CLASS
   // contact and checkbox taken in wrapper class   
   public class wrapcontact
{    
    public contact accn{get;set;}
   // public boolean isSelected{get;set;}     
       public wrapcontact(contact a)
    {    
         accn=a;
         isselected=false;
       }
   }
   
   public class BasisWrappper{
        public basic__c basis;
        public BasisWrappper(basic__c ba){
            this.basis = ba;
        }
    }
}

You can create mutiple wrapper class within a class.

Thanks,
Abhishek
Vishal_GuptaVishal_Gupta
Hi Shephali,

you can either use same wrapper or can create a seprate wrapper class for your second object, just like below code :

public class Wrapcontactlist 
{
    public list<wrapcontact> wrapcontactList { get; set; }
    public list<wrapcontact> wrapBasicList { get; set; }
    public list<contact> selectedContacts{get;set;}        
      public Wrapcontactlist ()
     {
          wrapcontactList =new list<wrapcontact>();
          for(contact a:[select id,name,phone from contact limit 10])
       {
           wrapcontactlist.add(new wrapcontact(a));
       }
       
        wrapBasicList = new  list<wrapcontact>();
        for(basic__c  b:[select id,name from basic__c limit 10])
       {
           wrapBasicList.add(new wrapcontact(b));
       }

  }
      
  //##THIS IS WRAPPER CLASS
   // contact and checkbox taken in wrapper class   
   public class wrapcontact
  {    
       public contact accn{get;set;}
       public Basic__c vBasic{get;set;}
     // public boolean isSelected{get;set;}     
       public wrapcontact(contact a)
       {    
         accn=a;
        isselected=false;
       }
       public wrapcontact(Basic__c vBas)
       {    
           vBasic=vBas;
          isselected=false;
       }

   }
}

Please let me know if I can help you more.

Thanks,
Vishal

 
This was selected as the best answer
Shephali SwarnkarShephali Swarnkar
Hi Abhishek Bansal and Vishal,
       Thanks for reply.
       Its working!!!
Shephali SwarnkarShephali Swarnkar
Hello  Abhishek and Vishal
         Can you please give VF page code to display the list of above Basic__c records with optionbutton. I mean i want to let the user select only one option outof all the records/row and then pass the selected option to object using controller.
thnks in advance
Abhishek BansalAbhishek Bansal
Hi Shephali :

Please modify your controller class as follows:

public class Wrapcontactlist
{
    public list<wrapcontact> wrapcontactList { get; set; }
    public List<BasisWrappper> basisWrapperList {get;set;}
    public list<contact> selectedContacts{get;set;}
   
    public List<SelectOption> getListOfBasis(){
        List<SelectOption> basisSelOp = new List<SelectOption>();
        
        for(BasisWrappper base : basisWrapperList){
            basisSelOp.add(new SelectOption(base.basis.Id,base.basis.Name));
        }
        return basisSelOp;
    }
    
    public String SelectedBasis { get; set; }

    
      public Wrapcontactlist ()
     {
          wrapcontactList =new list<wrapcontact>();
          for(contact a:[select id,name,phone from contact limit 10])
       {
           wrapcontactlist.add(new wrapcontact(a));
       }
       
       basisWrapperList = new List<BasisWrappper>();
       for(basic__c basis : [Select name,address1__c,state__c from basic__c]) {
            basisWrapperList.add(new BasisWrappper(basis));
       }
  }
      
  //##THIS IS WRAPPER CLASS
   // contact and checkbox taken in wrapper class   
   public class wrapcontact
{    
    public contact accn{get;set;}
   // public boolean isSelected{get;set;}     
       public wrapcontact(contact a)
    {    
         accn=a;
         isselected=false;
       }
   }
   
   public class BasisWrappper{
        public basic__c basis;
        public BasisWrappper(basic__c ba){
            this.basis = ba;
        }
    }
}

Add following code in your VF page :

<apex:selectList size="1" multiselect="false" value="{!SelectedBasis}">
              <apex:selectOptions value="{!listOfBasis}"></apex:selectOptions>
 </apex:selectList>


Let me know if you need more help on this

Thanks,
Abhishek
Shephali SwarnkarShephali Swarnkar
Hi Abhishek,
         The VF code you wrote here will generate a dropdown list.
Here in below code i have display the wrapperlist with checkbox.now i want to display the same with radiobutton so that user can choose only one instead of multiple selection we usually do with checkboxes.

<apex:pageBlockTable value="{!wrapcontactList}" var="waccl">            
           <apex:column >
             <apex:facet name="header">
               <apex:inputCheckbox />
             </apex:facet>
            <apex:inputCheckbox value="{!waccl.isSelected}" id="InputId"/>
           </apex:column>            
            <apex:column value="{!waccl.accn.name}"/>
            <apex:column value="{!waccl.accn.phone}"/>
         </apex:pageBlockTable>
Abhishek BansalAbhishek Bansal
Hi Shephali,

If you want user to select only one record than you can do it with the help of Javascript and use the same code as you are doing in your VF page.
Use the same code of Inputcheckbox and with the help of Javascript you can control the values of checkbox.
For eg : Call a js method onclick of inputCheckBox and than uncheck all other checkboxes except the one on which user has clicked
You cannnot add radio button in place of checkbox.

If you want to show radio button than you have to use the same controller code as mentioned in earlier post alongwith the below VF code :

<apex:selectRadio layout="pageDirection" value="{!SelectedContact}">
              <apex:selectOptions value="{!listOfContact}"></apex:selectOptions>
</apex:selectRadio >

Thanks,
Abhishek