function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Jai Prakash GuptaJai Prakash Gupta 

count of checkboxes selected

Hi All,

I am new to coding and I have created a VF page with wrapper class and I want to count no of check box seleted.

VF Page:
<apex:page controller="myController2" sidebar="false">
      <apex:form >
         <apex:pageBlock >
           
            <apex:pageBlockTable value="{!Pubs}" var="p" id="table">
                <apex:column >
                    <apex:inputCheckbox value="{!p.selected}" onchange="updateSelectCount(this);"/>
                </apex:column>
                <apex:column value="{!p.con.Name}" />
                <apex:column value="{!p.con.Type__c}" />
                <apex:column value="{!p.con.Circulation__c}" />
                  
            </apex:pageBlockTable>
              
        </apex:pageBlock>
    </apex:form>
      
</apex:page>

Class:
public class myController2 {
 public list<pPublication> mypub{set;get;}
  public List<pPublication> getPubs() {
        if(mypub == null) {
            mypub = new List<pPublication>();
            for(Publication__c p: [select id,name,Circulation__c,Hit_Rate__c,Type__c from Publication__c limit 10 ]) {
                mypub.add(new pPublication(p));
            }
        }
        return mypub;
    }
    public PageReference processSelected() {
    List<Publication__c> selectedPubs = new List<Publication__c>();
    for(pPublication cPub: getPubs()) {
            if(cPub.selected == true) {
                selectedPubs.add(cPub.con);
                
            }
        }
       for(Publication__c con: selectedPubs) {
            system.debug(con);
        }
        mypub=null;
        return null;
        }
        public class pPublication {
        public Publication__c con {get; set;}
        public Boolean selected {get; set;}
                public pPublication(Publication__c p) {
            con = p;
            selected = false;
      }

}
     }

I am not able to count the selected checkboxes.

Please help.

Thanks in advance!!
Nayana KNayana K
<apex:page controller="myController2" sidebar="false">
      <apex:form id="frm">
         <apex:pageBlock >
           
            <apex:pageBlockTable value="{!lstPubs}" var="p" id="table">
                <apex:column >
                    <apex:inputCheckbox value="{!p.selected}"/>
                </apex:column>
                <apex:column value="{!p.con.Name}" />
                <apex:column value="{!p.con.Type__c}" />
                <apex:column value="{!p.con.Circulation__c}" />
                  
            </apex:pageBlockTable>
            
            <apex:pageBlockButtons>
                <apex:commandButton value="Process Selected" action="{!processSelected}" rerender="frm" />
            </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>
      
</apex:page>
public class myController2 
{
    public List<pPublication> lstPubs    {    get;set;    }
    
    public myController2()
    {
        lstPubs = new List<pPublication>();
        for(Publication__c p: [select id,name,Circulation__c,Hit_Rate__c,Type__c from Publication__c limit 10 ]) 
        {
            lstPubs.add(new pPublication(p));
        }
    }
    
    public PageReference processSelected() 
    {
        List<Publication__c> selectedPubs = new List<Publication__c>();
        for(pPublication cPub: lstPubs) 
        {
            if(cPub.selected) 
            {
                selectedPubs.add(cPub.con);  
            }  
        }
        for(Publication__c con: selectedPubs) 
        {
            system.debug(con);
        }
        
        /* Ypu can get selected checkbox count in 2 ways
            1. Use selectedPubs.size() 
                OR
            2. Use int variable and increment if cPub.selected == true
        */
        system.debug('======selected Checkbox count====='+ selectedPubs.size());
        // if you want to clear list then use : lstPubs.clear();
        
        return null;
    }public class pPublication 
    {
        public Publication__c con {get; set;}
        public Boolean selected {get; set;}
        public pPublication(Publication__c p) 
        {
            con = p;
            selected = false;
        }

    }
}
 
Jai Prakash GuptaJai Prakash Gupta
Thanks for your response Nayana!!

But I want to display count of selected checkboxes at the top of the top of the page like 'Selected Checkboxes:4'(OutputTest) whenever any checkbox is selected to deselected.

For Example if I select 1 checkbox, it should display 1, when i select next checkbox it should update to 2.

Thanks in Advance!!