Select All Checkbox Using Apex in Visualforce Page
Controller:
public class Sample { public List<ContactWrapper> allContacts { get; set; } public Boolean allChecked { get; set; } public Sample() { allContacts = new List<ContactWrapper>(); allChecked = false; for(Contact con: [SELECT Name, Title, Department, Email From Contact LIMIT 10]){ allContacts.add(new ContactWrapper(con)); } } public PageReference CheckAll(){ for(ContactWrapper con : allContacts){ con.selected = allChecked; } return null; } public PageReference ProcessSelectedContacts(){ for (ContactWrapper contactWrapper : allContacts ){ if(contactWrapper.selected == true){ /* Do Some Processing */ } } return null; } public class ContactWrapper { public Contact con{get; set;} public Boolean selected {get; set;} public ContactWrapper(Contact c){ con = c; selected = false; } } }
Visualforce Page:
<apex:page controller="Sample"> <apex:form> <apex:dataTable value="{!allContacts}" var="c" id="contactsTable"> <apex:column > <apex:facet name="header"> <apex:inputCheckbox value="{!allChecked}"> <apex:actionSupport event="onclick" action="{!CheckAll}" rerender="contactsTable"/> </apex:inputCheckbox> </apex:facet> <apex:inputCheckbox value="{!c.selected}"/> </apex:column> <apex:column value="{!c.con.Name}" headervalue="Full Name"/> <apex:column value="{!c.con.Title}" headervalue="Title"/> <apex:column value="{!c.con.Department}" headervalue="Department"/> <apex:column value="{!c.con.Email}" headervalue="Email"/> </apex:dataTable> <apex:commandButton action="{!ProcessSelectedContacts}" value="Process Selected Contacts"/> </apex:form> </apex:page>