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
SabrentSabrent 

Apex Page Message: count of checkboxes selected

I have a pageBlockTable with inputcheckbox. I want to count the number of records selected and display the count in the ApexPages.message.

 

Any idea how I can do this? Thanks!!

 

 

VFP


<apex:pageBlockTable value="{!priceist}" var="p" id="results">

<apex:column >
<apex:inputCheckbox value="{!p.selected}" onchange="updateSelectCount(this);"/>
</apex:column>


<apex:column >
...
</apex:column>

</apex:pageBlockTable>
   
   
</apex:pageBlock>

 

//Apex class


 public PageReference approveRecord(){

 

    // my logic and finally


  ApexPages.addMessage(new ApexPages.message(ApexPages.severity.Confirm,'All records successfully approved!'));    // Here instead of 'All' i want {!count}

}

vbsvbs
The logical way would be to iterate through the list and check if p.selected = true and add to a counter. I would be interested in a quicker way to achieve this. What problem are you facing anyways?
Arun MKArun MK

 

Hi,

 

The Other way is to use the jQuery to count the number of checkboxes.

 

$("input:checkbox:checked").length;

 

If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.

 

Regards,

Arun.

hpereirahpereira
Hello rov, I see you are calling JavaScript function "updateSelectCount" on the onchange event. I would try the following: 1) Declare a global JavaScript variable that controls the number of checked options; 2) Update that variable anytime anyone clicks or unclicks an option; 3) Pass the value of the global variable as a parameter into function "approveRecord".
SabrentSabrent

Can someone please check what i am doing worng when concatenating the countofapprovedrecords to the Apexpage message?

 

As suggested by @vbs, I am iterating through the list, checking if record.selected = true and adding to the counter.

My debug statement, shows me the number of selected records.

I am converting the integer to a string (debugs show correct converted value) and using this string in my Apex.messages, howver, the message doesn't display the string that i converted from ineteger value.

 

 

public integer countapprovedrecords=0;

for (myWrapper record : IncList){

 if(record.selected){

  countapprovedrecords++;
        }
}

if (!Apexpages.hasMessages()){
        string countstring = string.valueof(countapprovedrecords);
        system.debug('integer converted to string' +countstring);
        ApexPages.addMessage(new ApexPages.message(ApexPages.severity.Confirm,'countstring'+  'records successfully approved!'));
                           
}

Arun MKArun MK

 

Hi,

 

Try this one. countrstring  shouldn't within single quotes.

 

ApexPages.addMessage(new ApexPages.message(ApexPages.severity.Confirm, countstring+  'records successfully approved!'));

 

If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.

 

Regards,

Arun.

SabrentSabrent

Thanks!! that was it.

 

Thanks to everyone.. All the responses were helpful.