Wrapper Class Example Using Visualforce Salesforce - Apex Basic | Visualforce Basic - Salesforce Funda



User Case :- we have user case we need to display all account Information with primitive data types Boolean as isSelected variable as Checkbox, so we will use wrapper class in apex and try to get all the data to visualforce 

Apex Class :-

public class AccountWrapperClassController {

    //wrapper List 
    public List<AccountWrapper> accountWrapperList{get;set;
   // Constructor
    public AccountWrapperClassController(){
        accountWrapperList = new List<AccountWrapper>();
        List<Account>accList = [Select Id, Name, Phone, Industry, Rating From Account];
        for(Account obj : accList){
            AccountWrapper  wrap = new AccountWrapper();
            wrap.accobj = obj;
            wrap.isSelected = false;
            accountWrapperList.add(wrap);
        }
    }
    
    //Wrapper Class
    public Class AccountWrapper{    
        public Account accobj {get;set;}
        public Boolean isSelected{get;set;}

     //Constructor
     public AccountWrapper(){   
       this.accobj = new Account();
       this.isSelected = false;
      }
    }
}

Visualforce Page :-
<apex:page controller="AccountWrapperClassController" >
    <apex:form>
      <apex:pageBlock title="Display Account Details" tabStyle="Account">
          <apex:pageBlockTable value="{!accountWrapperList}"  var="objwrapper">
              <apex:column>
                <apex:inputCheckbox value="{!objwrapper.isSelected}" />
              </apex:column>
              <apex:column value="{!objwrapper.accobj.Name}"/>
              <apex:column value="{!objwrapper.accobj.Phone}"/>
              <apex:column value="{!objwrapper.accobj.Industry}"/>
              <apex:column value="{!objwrapper.accobj.Rating}"/>
          </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>

it will look like this :-



Feature :-