[SalesForce] Sum of attachment’s file size on click of inputCheckBox

I have a requirement where I need to calculate the sum of all the attachment file size on selection of inputCheckBox. The VF page looks like below. I have used a wrapper class to display Attachment record with checkboxes.

Visualforce Page
The Apex code looks like below:—
This part of the code is invoked by the constructor so that I could show Attachment related to the case on page load:

for(Attachment a : [Select Id, Body, BodyLength, Name, Description, ParentId, ContentType from Attachment where ParentId = :cases.Id and BodyLength < :value])

    caseAttachments.add(new caseAttachmentWrapper(a));

Wrapper is:

public class caseAttachmentWrapper{
public Attachment att{get;set;}
public boolean check{get;set;}
public caseAttachmentWrapper(Attachment att){
this.att = att;
check = false;
}
}

Visualforce page code looks like this:
There is a pageblock table that displays attachments like this:

VF code

<apex:pageBlock title="{!$Label.Case_attachment_related}" id="block1" mode="edit">
      <apex:pageBlockSection title="{!$Label.Case_select_attachment} - {!AttachmentSize} Attachment Found" collapsible="true" columns="1" id="section1">
       <b>Total File Size : - {!totalFileSize} MB</b>
       <apex:pageBlockTable value="{!caseAttachments}" var="attached" id="table1">
         <apex:column headerValue="Select" id="column1"> <apex:inputCheckbox value="{!attached.check}" id="check">
            <!-- <apex:actionsupport event="onchange"/>-->
            </apex:inputCheckbox>
          </apex:column>
           <apex:column headerValue="Name" value="{!attached.att.Name}"/>
            <apex:column headerValue="Size" value="{!attached.att.BodyLength}"/>
           <apex:column headerValue="File Type" value="{!attached.att.ContentType}"/>
          <apex:column headerValue="View File"> <apex:outputLink value="/servlet/servlet.FileDownload?file={!attached.att.Id}" target="_blank">View File</apex:outputLink>
         </apex:column> 
       </apex:pageBlockTable>
    </apex:pageBlockSection>
<apex:pageBlock title="{!$Label.Case_attachment_related}" id="block1" mode="edit">
<apex:pageBlockSection title="{!$Label.Case_select_attachment} - {!AttachmentSize} Attachment Found" collapsible="true" columns="1" id="section1">
*<b>Total File Size : - {!totalFileSize} MB</b>*
<apex:pageBlockTable value="{!caseAttachments}" var="attached" id="table1"><apex:column headerValue="Select" id="column1"> <apex:inputCheckbox value="{!attached.check}" id="check">
<!-- <apex:actionsupport event="onchange"/>-->
</apex:inputCheckbox>
</apex:column>
<apex:column headerValue="Name" value="{!attached.att.Name}"/>
<apex:column headerValue="Size" value="{!attached.att.BodyLength}"/>
<apex:column headerValue="File Type" value="{!attached.att.ContentType}"/>
<apex:column headerValue="View File"> <apex:outputLink value="/servlet/servlet.FileDownload?file={!attached.att.Id}" target="_blank">View File</apex:outputLink>
</apex:column> 
</apex:pageBlockTable>
</apex:pageBlockSection>

— Now when I check the boxes, the corresponding file size should be added in the total file size. Similarly, if I uncheck, the sum should decrease.

One way I was thinking is, since inputCheckBox will set the value in "check" attribute of the wrapper, may be we could utilize the same to calculate the total file size.

Please help me in this. Any help would be appreciated. Let me know if there are any questions.

Best Answer

<apex:inputCheckbox value="{!attached.check}">
  <apex:actionSupport event="onchange" action="{!calculateTotalFileSize}">
     <apex:param name="attachmentID" value="{!attached.att.Id}"
                            assignTo="{!selectedAttachmentId}"/>
    </apex:actionSupport>
</apex:inputCheckbox>

public pageRefrence calculateTotalFileSize(){
    // do your addition and subtraction based on condition
}

remove the Body field from SOQL Query,

Query Like this:

for(Attachment a : [Select Id, BodyLength, Name, Description, ParentId, ContentType 
                    From Attachment 
                    Where ParentId = :cases.Id 
                    And BodyLength < :value])