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 

I am Sakthivel Madesh, a certified Salesforce Advanced Developer & Administrator working on Salesforce Technology since 2011. I am currently working in Techforce Services as Sr. Salesforce Developer. I have worked on multiple technologies Like PHP, Java, Salesforce, I am very passionate about Salesforce and to earn 15x Salesforce Certification. I love Trailhead for learning Salesforce Skill and Become a Trailhead 5x Ranger too. I Love to Learn & Share my Salesforce knowledge to Salesforce Learners/Beginner using My Blog and Salesforce Community.

Leave a Reply

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

*