Activity Forums Salesforce® Discussions How to select all checkbox option in Salesforce visualforce page without using javascript?

  • Shubham

    Member
    October 3, 2016 at 10:52 am

    Hi Pranav,

    I am assuming that you have a list of records and you want to 'select all' using single checkbox to do some sort of operation/action on the selected records. So to create 'check-uncheck all' functionality in your VF page, I think using Javascript would be the best option as JavaScript runs client side, 'check-uncehck' all functionality would be fast as compared to apex. If your requirement is to use apex . Here is a sample code : -

    VF page : 

    <apex:page controller="checkUncheckAllCntrl" tabStyle="Account">
    <apex:form id="theForm">
    <apex:pageBlock>
    <apex:pageBlockButtons>
    <apex:commandButton value="Do Some Action"/>
    </apex:pageBlockButtons>
    <apex:pageBlockSection title="List of Accounts">
    <apex:pageBlockTable value="{!wrapperAccList}" var="acc">
    <apex:column>
    <apex:facet name="header">
    <apex:inputCheckbox id="mainBox" value="{!mainBoxValue}" onChange="CallAction();return false;">
    <apex:actionSupport event="onclick" action="{!checkUncheckAll}" reRender="theForm"/>
    </apex:inputCheckbox>
    </apex:facet>
    <apex:inputCheckbox value="{!acc.flag}"/>
    </apex:column>
    <apex:column value="{!acc.accObj.Name}"/>
    </apex:pageBlockTable>
    </apex:pageBlockSection>
    </apex:pageBlock>
    </apex:form>
    </apex:page>

    Controller :

    public class CheckUncheckAllCntrl {
    public Boolean mainBoxValue{get;set;}
    public List<checkBoxWrapper> wrapperAccList{get;set;}

    public CheckUncheckAllCntrl(){
    wrapperAccList = new List<checkBoxWrapper>();
    for(Account acc :[Select Id,Name,OwnerId from Account limit 10]){
    wrapperAccList.add(new CheckBoxWrapper(false,acc));
    }
    }

    public void checkUncheckAll(){
    for(CheckBoxWrapper wrapObj : wrapperAccList){
    wrapObj.flag = mainBoxValue;
    }
    }

    public class CheckBoxWrapper{
    public Boolean flag{get;set;}
    public Account accObj{get;set;}

    public CheckBoxWrapper(Boolean flagValue,Account accObj){
    this.flag = flagValue;
    this.accObj = accObj;
    }
    }
    }

     

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos