+ Start a Discussion
Qadir KhanQadir Khan 

How to add Checkbox in vf page

Controller
public class lonchangeaccount{
        public id aid{get;set;}
        public boolean render{get;set;}
 
        List<selectoption> options=new List<selectoption>();
        public  List<Contact> lstcont{get;set;}
        public lonchangeaccount(){
                    lstcont=new List<contact>();
           
            }
      
        Map<id,List<contact>> maplstcont=new Map<id,List<contact>>();
        public List<SelectOption>  getValues(){
                options.add(new selectOption('--None--','--None--'));
                for(Account acc:[select id,name,(select id,name,email from contacts)from account]){
                    options.add(new selectOption(acc.id,acc.name));
                    maplstcont.put(acc.id,acc.contacts);
               
                }
                return options;
        }
        public void  Contactlst(){
                        lstcont.clear();
                       if(aid!=null){
                            render=true;
                            lstcont.addAll(maplstcont.get(aid));
                        }
         }
}

VF Page
<apex:page controller="lonchangeaccount">
<apex:form >
         <apex:pageBlock title="Select Account">
         <apex:actionFunction action="{!Contactlst}" name="change"/>
         <apex:selectList value="{!aid}" multiselect="false" size="1" onchange="change();">
                <apex:selectOptions value="{!values}"/>
            </apex:selectList>
            </apex:pageBlock>
            <apex:pageBlock title="Related Contacts" >
          <apex:outputPanel rendered="{!render}" id="balu">
           
              <apex:pageBlockTable value="{!lstcont}" var="c">
             
                      <apex:column value="{!c.name}"/>
                      <apex:column value="{!c.email}"/>
              </apex:pageBlockTable>
             <apex:commandButton value="Next"/>
         </apex:outputPanel>
   
    </apex:pageBlock>
    </apex:form>

</apex:page>
SandhyaSandhya (Salesforce Developers) 
Hi Qadir Khan,

You need to use the wrapper class to add the checkbox in visual force page.


A wrapper or container class is a class, data structure, or an abstract data type whose instances are collections of other objects.It is a custom object defined by Salesforce developer where he defines the properties of the wrapper class. Within Apex & Visualforce this can be extremely helpful to achieve many business scenarios within the Salesforce CRM software.

I suggest you refer below links to know more about it.
https://developer.salesforce.com/page/Wrapper_Class

 http://www.salesforcetutorial.com/wrapper-class-wrapper-class-example/

 https://developer.salesforce.com/page/Checkbox_in_DataTable?language=en 
 
Hope this helps you!

Please accept my solution as Best Answer if my reply was helpful. It will make it available for other as the proper solution. If you felt I went above and beyond, you can give me kudos.
 
Thanks and Regards
Sandhya

 
Narveer SinghNarveer Singh
Hi Qadir,

To add Check box in vf you can use below code :

<apex:column> 
    <apex:facet name="header">Your Header</apex:facet>
    <center>
        <apex:inputCheckbox value="{!c.yourVarriable}" />
    </center>
</apex:column>

you can use wrapper class as :

public class varWrapper {
    public Boolean VarriableName{get;set;}
}

And your controller will be : 

public with sharing class checkboxController {
    
   public List<varWrapper> wrapperList{get;set;}
  
   public List<varWrapper> getCheckBoxVarriable() {
               
        wrapperList = new List<varWrapper>();
        
        String query =  'SELECT Id,isRequired From Account'         
            
        for(Account acc :  Database.query(query1)){
            
            varWrapper twrap = new varWrapper();
            twrap.varriableName=True;
            
            wrapperList.add(twrap);
        }
    }
    return wrapperList;
}

Hope this helps you!

Please accept my solution as Best Answer if my reply was helpful. It will make it available for other as the proper solution.

Best Regards
Narveer
 
Priyank KotakPriyank Kotak
Hi! Qadir Khan,

To add a column of checkbox in your datatable, just write the below code inside you VFP datatable...

<apex:column headerValue="Select">
                     <apex:inputcheckbox />
                </apex:column>

Best Regards,
Priyank.
Suraj Tripathi 47Suraj Tripathi 47
Hi Qadir Khan,

You can add checkbox in your page using below line of code.
<apex:inputCheckbox value="{!c.yourValue}" />

If you find your Solution then mark this as the best answer.

Thank you!

Regards,
Suraj Tripathi