Select all check box on visualforce page

Create a visualforce Page with display list of records with check box. you want to enable the functionality to select all check box. You can do this with the help of java script in visualforce page.
Example:
controller
Public with sharing class checkAllcolumnDemocontroller {
Public List<wrapperclass> wrapList {get;set;}
Public boolean checked{get;set;}
Public checkAllcolumnDemocontroller(){
checked = false;
}
Public void selectallnone(){
if(!checked)
checked = true;
else
checked = false;
}
Public List<wrapperclass> getWrapperList(){
wrapList = New List<wrapperclass>();
for(Account acc:[Select name,accountnumber,id,annualrevenue,description from account limit 5]){
if(!checked)
wrapList.add(New wrapperclass(acc,false));
else
wrapList.add(New wrapperclass(acc,true));
}
return wrapList;
}
Public class wrapperclass{
Public account accRec{get;set;}
Public boolean checkFlag{get;set;}
Public wrapperclass(account acc,boolean flag){
accRec = acc;
checkFlag = flag;
}
}
}
Visualforce Page
<apex:page controller="checkAllcolumnDemocontroller">
<!-- check all columns demo page using action function- it has slow respose-->
<apex:form >
<apex:pageblock id="pgblck">
<apex:pageblocktable value="{!WrapperList}" var="wraprec">
<apex:column value="{!wraprec.accRec.name}"/>
<apex:column value="{!wraprec.accRec.accountnumber}"/>
<apex:column value="{!wraprec.accRec.annualrevenue}"/>
<apex:column >
<apex:facet name="header">
<apex:inputCheckbox onclick="selectall()"/>
</apex:facet>
<apex:inputCheckbox value="{!wraprec.checkflag}" />
</apex:column>
</apex:pageblocktable>
</apex:pageblock>
<apex:actionFunction name="selectallmethod" action="{!selectallnone}" rerender="pgblck"/>
</apex:form>
function selectall() {
selectallmethod();
}
</apex:page>

Comments