Displaying the check box in a data table or page block table is a general requirement in every project. with the help of wrapper class we can display the checkboxes in a data table. For the select all checkbox we need to add small javascript so that if we select the header checkbox it will select all the checkboxes.
Apex Class:

public class Checkbox_Class  { 
    List accountList = new List();
    List selectedAccounts = new List();       
    public List getAccounts()  {
        for(Account a: [select Id, Name, AccountNumber, Phone from Account limit 5])
        accountList.add(new accountwrapper(a));
        return accountList;
    }
   
    public PageReference getSelected()   {
        selectedAccounts.clear();
        for(accountwrapper accwrapper: accountList)
        if(accwrapper.selected == true)
        selectedAccounts.add(accwrapper.acc);
        return null;
    }
   
    public List GetSelectedAccounts()  {
        if(selectedAccounts.size()>0)
        return selectedAccounts;
        else
        return null;
    }  
   
    public class accountwrapper  {
        public Account acc{get; set;}
        public Boolean selected {get; set;}
        public accountwrapper(Account a)
        {
            acc = a;
            selected = false;
        }
    }
}

VisualForce Page:












a.acc.Name}” />














Demo : https://sfdevforce-developer-edition.na12.force.com/vfCheckbox

Written by 

Salesforce MVP | Lightning Champion | 17x Salesforce Certified | Application Architect | 7x Trailhead Ranger | Techforce Services | Australia

Leave a Reply

Your email address will not be published. Required fields are marked *

*